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

A part of the university of san francisco's cs245 course on data structures and algorithms. It covers the topic of algorithm analysis, focusing on when one algorithm is better than another based on time efficiency and space requirements. The document also discusses best, worst, and average case scenarios, measuring time efficiency, and the importance of constants in algorithm analysis.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-emz
koofers-user-emz ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 60

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures and Algorithms
CS245-2009S-02
Algorithm Analysis
David Galles
Department of Computer Science
University of San Francisco
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c

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