Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

College of Information Technology & Design
MATH 224 – Discrete Mathematics
Fundamentals of Python: From First Programs Through Data Structures
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Chapter 1 – Basic Concepts
1 ICS 353 Design and Analysis of Algorithms Spring Semester (062) King Fahd University of Petroleum & Minerals Information & Computer Science.
Introduction to Analysis of Algorithms
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Algorithm Efficiency and Sorting
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (1) Asymptotic Complexity 10/28/2008 Yang Song.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Chapter 6 Algorithm Analysis Bernard Chen Spring 2006.
Lecture 2 MAS 714 Hartmut Klauck
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Analysis of Algorithms
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
Data Structure Introduction.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Algorithm Analysis Part of slides are borrowed from UST.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 1. Complexity Bounds.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Lecture 4 Jianjun Hu Department of Computer Science and Engineerintg University of South Carolina CSCE350 Algorithms and Data Structure.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
LECTURE 2 : fundamentals of analysis of algorithm efficiency Introduction to design and analysis algorithm 1.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Algorithm Analysis 1.
Theoretical analysis of time efficiency
ADT Implementation: Recursion, Algorithm Analysis, and Standard Algorithms Chapter 10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second.
Analysis of Algorithms
Analysis of algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency
Algorithm Analysis (not included in any exams!)
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Algorithm Efficiency Chapter 10.
CS 201 Fundamental Structures of Computer Science
Chapter 2.
Algorithm Analysis and Design
Fundamentals of the Analysis of Algorithm Efficiency
Searching, Sorting, and Asymptotic Complexity
Algorithms: the big picture
Analysis of algorithms
Fundamentals of the Analysis of Algorithm Efficiency
Analysis of Algorithms
Algorithms and Data Structures Lecture II
Presentation transcript:

Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What is a step of an algorithm? Analysis of algorithms

Chapter 1: Algorithm Correctness Chapter 2: Analysis of Algorithms

Algorithm Correctness

Computational Problem Examples: –Find the sum of n given integers –Find the minimum of n elements –Sort n numbers –Search Problem: Does the list L of n items contain the element x? n: Input size

Instance Sort the numbers {3, 2, 67, 54, 57, 23} Find the minimum element in the array {2,4, 67, 89, 23, 45, 67, 890, 2, 1} Does 3 belong to {4, 6, 78, 56, 3, 45, 2,109}?

Specification of a problem Min Problem: Precondition: S is a finite, non-empty set of integers Postcondition: m is a minimum element of S

Algorithm Step-by-step procedure to solve a well- specified problem You are given two pieces of rope. You know each of them burns in exactly one hour. But they do not burn uniformly. Find a way to measure ¼ of an hour. This is an instance of a more general problem. Generalize it. Examples: Find Min Sequential search Binary Search Selection Sort Insertion Sort

Iterative Algorithms Loop Invariant Before iteration i: m is the smallest element among S[1], …, S[i] Find Min Precondition: S a non-empty list of integers Postcondition: m a minimum element Algorithm: m = S[1] for i=2 to n if m > S[i] then m = S[i] endif endfor

Recursive Algorithm Find Min Precondition: S a non-empty list of integers Postcondition: m a minimum element Algorithm: FindMin[S,n] = Min[S,1,n] Min[S,b,e] if e = b return S[b] m = Min[S, b+1, e] if m > S[b] then m = S[b] endif return m Invariant Min[S,i,j] returns a smallest element among S[i], …, S[j]

Correctness Proofs For iterative algorithms: by induction, use loop invariant For recursive algorithms: by induction, use the function invariant (pre/post conditions) Example: prove that the iterative and recursive algorithms for finding the min are correct

Analysis of Algorithms

Step Arithmetic operations Comparisons Etc. Do not count in the analysis: implementation overhead (push parameters on stack, compare with loop variable, etc.)

Analysis of Sequential Search Search(x,L,n) Search x in the list L of size n, return position if x is in S; otherwise 0 for i=1 to n if x = L[i] return I Return 0 for i=1 to n 1 comparison per step Time: = n

Analysis of Binary Search Search(x,L,b,e) Search x in the sorted list L from b to e, return position if b=e and x=L[b] return b m=(b+e)/2 if x < L[m] Search[x,L,b,m] else Search[x,L,m,e] T(n) (n = e-b = nr. of elements) Time to search in a list of size n Worst case time: T(n) = T(n/2) + 1 Recurrence Relation T(n) = log n

Which is better? Sequential Search: n steps Binary Search: log n steps Hidden constants in implementing recursive algorithm: -each recursive call pushes data on a stack -Returning from a recursive call pops data from stack More realistically: Sequential Search: 3n Binary Search: 10 log n

Evaluating Efficiency using Big Oh notation Criterion: if lim a(n)/b(n) is a constant, then we say a(n) = O(b(n)) We mean: a is better or at least as good as b Example for search algorithms: lim (10 log n) / (3 n) = 0 Binary search is at least as good as Sequential Search

Two implementations of Sequential Search A takes 10n B takes 27n-25 lim (10 n) / (27 n - 25) = 10/27 The two implementations are comparable, ignoring constants Running B on a faster computer than A may be faster. On the same computer, same programming language, same programmer, etc. A will probably be faster.

Evaluating Efficiency using small Oh notation Criterion: if lim a(n)/b(n) is the zero constant, then we say a(n) = o(b(n)) We mean: a is much better than b Example for search algorithms: lim (10 log n) / (3 n) = 0 Binary search is much better than Sequential Search

Ignoring small cases A (Binary Search) runs in 100 log n B (Sequential Search) runs in 3n A is asymptotically better lim (100 log n) / (3 n) = 0 But on small values of n, sequential search might be better How to find the break-even point? Testing and profiling a program: compare the running times on small data sets, see when one becomes better.

Homework exercises Small programming assignment: implement sequential search and binary search. Find when binary search becomes better in your implementation Limit computation: a small list of simple functions. Tell when one is Big Oh or small Oh of the other. Posted on the web by tonight, due in one week, before class.

If time allows (if not, read it from the book in preparation for next lecture): Towers of Hanoi: recursive algorithm and time analysis