{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture08

# lecture08 - Today COMPUTER SCIENCE 51 Spring 2009...

This preview shows pages 1–3. Sign up to view the full content.

3/18/2009 1 COMPUTER SCIENCE 51 Spring 2009 cs51.seas.harvard.edu Prof. Greg Morrisett Prof. Ramin Zabih Today Complexity of algorithms space & time asymptotic growth, big-O notation recurrences tail-calls & tail-recursion High-Level Goal Compare algorithms & data structures: why is merge-sort “better” than insertion-sort? why is a rb-tree “better” than an association list? why is it “better” to use foldl than foldr to sum a list? One possible comparison: measure the running time & space used problems? Problems with Measuring If you measured the running time as a function of the input list length n, you’d see that: insertion-sort runs in worst-case time k 1 * n 2 + k 2 for some constants k 1 and k 2 . merge-sort runs in worst-case time c 1 *n*lg 2 n + c 2 for constants c 1 and c 2 . So times are different for different inputs. The constants depend upon many factors: The language & compiler used The machine used What other processes are running The temperature, your altitude, ... Plotting it out T ms (n) = 2*n*lg 2 n + 30 T is (n) = 1*n 2 + 0 Shifting the Curves T ms (n) = 4 *n*lg 2 n + 30 T is (n) = 1*n 2 + 0

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
3/18/2009 2 Asymptotic Complexity Given the two functions: T is (n) = k 1 * n 2 + k 2 and T ms (n) = c 1 *n*lg 2 n + c 2 as n grows towards infinity T is grows “faster” than T ms regardless of the constants. in contrast, T(n) = q 1 *n 2 + q 2 *n + q 3 does not grow faster than T is independent of the constants. If we can formalize “faster”, then we have a robust way to compare algorithms. Won’t depend upon language, compiler, machine, OS, etc. as long as these only affect the constants. Our Goals Examine a piece of code and determine it’s worst case running time (or space) as a function of its inputs. without having to measure it. obviously, in terms of symbolic constants. e.g., T ms (n) = c 1 *n*lg 2 n + c 2 Be able to compare the symbolic running time (space) of two functions and determine which one is asymptotically better. without resorting to plots. For this, we will utilize big-O notation. Formal Big-O notation Given f : number number: O(f) is the set of all functions g such that: for all n > 0, there exists a number c such that g(n) ≤ c * f(n) If f O(g) but g is not in O(f), then g grows faster than f.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

lecture08 - Today COMPUTER SCIENCE 51 Spring 2009...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online