lecture08 - 3/18/2009 Today COMPUTER SCIENCE 51 Spring 2009

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

View Full Document Right Arrow Icon
3/18/2009 1 COMPUTER SCIENCE 51 Spring 2009 cs51.seas.harvard.edu Prof. Greg Morrisett Prof. Ramin Zabih Today Complexity of algorithms asymptotic growth, big-O notation recurrences High-Level Goal 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: 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 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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. we’ll write g >> f to reflect this. Example: I claim that λ n.10*n 2 +3 O( λ n.n 2 ) But not in O( λ n.n*lg 2 n) Arguing big-O Claim that λ n.10*n 2 +3 O( λ n.n 2 ) must find a constant c such that for all n > 0, 10*n 2 +3 ≤ c * n 2 When n=1: ( λ n.10*n 2 +3) 1 = 10*1 + 3 = 13, so need 13 ≤ c When n=2: (
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

lecture08 - 3/18/2009 Today COMPUTER SCIENCE 51 Spring 2009

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online