Docsity

Algorithm Analysis: Understanding the Efficiency of Algorithms - Prof. David J. Galles, Exams of Data Structures and Algorithms

Partial preview of the text

Download Algorithm Analysis: Understanding the Efficiency of Algorithms - Prof. David J. Galles and more Exams Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms^ CS245-2009S-02Algorithm Analysis

David GallesDepartment of Computer ScienceUniversity of San Francisco

02-0:^ Algorithm Analysis When is algorithm A better than algorithm B?

02-2:^ Algorithm Analysis When is algorithm A better than algorithm B?^ Algorithm A runs faster^ Algorithm A requires less space to run

02-3:^ Algorithm Analysis When is algorithm A better than algorithm B?^ Algorithm A runs faster^ Algorithm A requires less space to runSpace / Time Trade-off^ Can often create an algorithm that runs faster, byusing more spaceFor now, we will concentrate on time efficiency

02-5:^ Best Case vs. Worst Case How long does the following function take to run: boolean find(int A[], int element) {for (i=0; i<A.length; i++) {if (A[i] == elem)return true;} return false;} It depends on if – and where – the element is in the list

02-6:^ Best Case vs. Worst Case^ Best Case – What is the fastest that the algorithmcan run^ Worst Case – What is the slowest that thealgorithm can run^ Average Case – How long, on average, does thealgorithm take to runWorst Case performance is almost always important. Usually , Best Case performance is unimportant (why?) Usually , Average Case = Worst Case (but not always!)

02-8:^ Measuring Time Efficiency How long does an algorithm take to run?^ Implement on a computer, time using a stopwatch.

02-9:^ Measuring Time Efficiency How long does an algorithm take to run?^ Implement on a computer, time using a stopwatch.Problems:^ Not just testing algorithm – testingimplementation of algorithm^ Implementation details (cache performance,other programs running in the background, etc)can affect results^ Hard to compare algorithms that are not testedunder^ exactly the same conditions

02-11:^ Competing Algorithms^ Linear Search^ for (i=low; i <= high; i++)if (A[i] == elem) return true;return false;^ Binary Search^ int BinarySearch(int low, int high, elem) {if (low > high) return false;mid = (high + low) / 2;if (A[mid] == elem) return true;if (A[mid] < elem)return BinarySearch(mid+1, high, elem);elsereturn BinarySearch(low, mid-1, elem);

02-12:^ Linear vs Binary^ Linear Search^ for (i=low; i <= high; i++)if (A[i] == elem) return true;return false;^ Time Required, for a problem of size

n^ (worst

case):

02-14:^ Linear vs Binary^ Binary Search^ int BinarySearch(int low, int high, elem) {if (low > high) return false;mid = (high + low) / 2;if (A[mid] == elem) return true;if (A[mid] < elem)return BinarySearch(mid+1, high, elem);elsereturn BinarySearch(low, mid-1, elem);}^ Time Required, for a problem of size

n^ (worst

case):

02-15:^ Linear vs Binary^ Binary Search^ int BinarySearch(int low, int high, elem) {if (low > high) return false;mid = (high + low) / 2;if (A[mid] == elem) return true;if (A[mid] < elem)return BinarySearch(mid+1, high, elem);elsereturn BinarySearch(low, mid-1, elem);}^ Time Required, for a problem of size

n^ (worst

case):^ c∗^ lg(n)^ for some constant^2

c^2

02-17:^ Constants

Do Not^ Matter!

Length of list^ Time Required for

Time Required for Linear Search Binary Search 10 0.001 seconds

0.3 seconds 100 0.01 seconds

0.66 seconds 1000 0.1 seconds

1.0 seconds 10000 1 second

1.3 seconds 100000 10 seconds

1.7 seconds 1000000 2 minutes

2.0 seconds 10000000 17 minutes

2.3 seconds

10 10 11 days

3.3 seconds

15 10 30 centuries

5.0 seconds

20 10 300 million years

6.6 seconds

02-18:^ Growth Rate We care about the^ Growth Rate

of a function – how

much more we can do if we add more processingpowerFaster Computers^6 =

Solving Problems Faster

Faster Computers^ =

Solving Larger Problems Modeling more variables Handling bigger databases Pushing more polygons