1 / 26

CSE 326: Data Structures Lecture #3 Asymptotic Analysis

This lecture covers asymptotic analysis and the concept of Silicon Downs. It discusses the different types of analysis and their applications in analyzing algorithms.

sernest
Download Presentation

CSE 326: Data Structures Lecture #3 Asymptotic Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 326: Data StructuresLecture #3Asymptotic Analysis Steve Wolfman Winter Quarter 2000

  2. Today’s Outline • Silicon Downs • Asymptotic Analysis • Return Quiz

  3. Silicon Downs Post #1 n3 + 2n2 n0.1 n + 100n0.1 5n5 n-152n/100 82log n mn3 Post #2 100n2 + 1000 log n 2n + 10 log n n! 1000n15 3n7 + 7n 2mn Winner O(n2) O(log n) TIE O(n) O(n5) O(n15) O(n6) IT DEPENDS

  4. Race I n3 + 2n2 vs. 100n2 + 1000

  5. Race II n0.1 vs. log n

  6. Race III n + 100n0.1 vs. 2n + 10 log n

  7. Race IV 5n5 vs. n!

  8. Race V n-152n/100 vs. 1000n15

  9. Race VI 82log(n) vs. 3n7 + 7n

  10. FBI Finds Silicon Downs Fixed • The fix sheet (typical growth rates in order) • constant: O(1) • logarithmic: O(log n) (logkn, log n2 O(log n)) • poly-log: O(logk n) • linear: O(n) • log-linear: O(n log n) • superlinear: O(n1+c) (c is a constant > 0) • quadratic: O(n2) • cubic: O(n3) • polynomial: O(nk) (k is a constant) • exponential: O(cn) (c is a constant > 1)

  11. Terminology Given an algorithm whose running time is T(n) • T(n) O(f(n)) if there are constants c and n0 such that T(n)  c f(n) for all n  n0 • 1, log n, n, 100n  O(n) • T(n) (f(n)) if there are constants c and n0 such that T(n) c f(n) for all n  n0 • n, n2, 100 . 2n, n3 log n  (n) • T(n)  (f(n)) if T(n)  O(f(n)) and T(n)  (f(n)) • n, 2n, 100n, 0.01 n + log n  (n) • T(n)  o(f(n)) if T(n)  O(f(n)) and T(n)  (f(n)) • 1, log n, n0.99  o(n) • T(n)  (f(n)) if T(n)  O(f(n)) and T(n)  (f(n)) • n1.01, n2, 100 . 2n, n3 log n  (n)

  12. Types of analysis Orthogonal axes • bound flavor • upper bound (O, o) • lower bound (, ) • asymptotically tight () • analysis case • worst case (adversary) • average case • best case • “common” case • analysis quality • loose bound (any true analysis) • tight bound (no better bound which is asymptotically different)

  13. Analyzing Code • C++ operations - constant time • consecutive stmts - sum of times • conditionals - sum of branches, condition • loops - sum of iterations • function calls - cost of function body Above all, use your head!

  14. More Examples Than You Can Shake a Stick At (#1) for i = 1 to n do for j = 1 to n do sum= sum+ 1

  15. METYCSSA (#2) for i = 1 to n do for j = i to n do sum= sum+ 1

  16. METYCSSA (#3) • Conditional if C then S1 else S2 • Loops while C do S

  17. METYCSSA (#4) • Recursion • Almost always yields a recurrence • Recursive max T(0) <= b T(n) <= c + T(n - 1) ifn > 0 • Analysis T(n) <= c + c + T(n - 2) (by substitution) T(n) <= c + c + c + T(n - 3) (by substitution, again) T(n) <= kc + T(n - k) (extrapolating 0 < k n) T(n) <= nc + T(0) = nc + b (for k = n) • T(n)

  18. METYCSSA (#5): Mergesort • Mergesort algorithm • split list in half, sort first half, sort second half, merge together • T(1) <= b T(n) <= 2T(n/2) + cnifn > 1 • Analysis T(n) <= 2T(n/2) + cn <= 2(2T(n/4) + c(n/2)) + cn = 4T(n/4) + cn + cn <= 4(2T(n/8) + c(n/4)) + cn + cn = 8T(n/8) + cn + cn + cn <= 2kT(n/2k) + kcn(extrapolating 1 < k n) <= nT(1) + cn log n(for 2k = n or k = log n) • T(n)

  19. METYCSSA (#6): Fibonacci • Recursive Fibonacci: int Fib(n) if (n == 0 or n == 1) return 1 else return Fib(n - 1) + Fib(n - 2) • Lower bound analysis • T(0), T(1) >= b T(n) <= T(n - 1) + T(n - 2) + c ifn > 1 • Analysis let  be (1 + 5)/2 which satisfies 2 =  + 1 show by induction on n that T(n) >= bn - 1

  20. Example #6 continued • Basis: T(0)  b > b-1 and T(1)  b = b0 • Inductive step: Assume T(m)  bm - 1 for all m < n T(n)  T(n - 1) + T(n - 2) + c  bn-2 + bn-3 + c  bn-3( + 1) + c = bn-32 + c  bn-1 • T(n) • Why? Same recursive call is made numerous times.

  21. Example #6: Learning from Analysis • To avoid recursive calls • store all basis values in a table • each time you calculate an answer, store it in the table • before performing any calculation for a value n • check if a valid answer for n is in the table • if so, return it • This strategy is called “memoization” and is closely related to dynamic programming • How much time does this version take? Ask Zasha about me!

  22. Final Example (#7):Longest Common Subsequence • Problem: given two strings (m and n), find the longest sequence of characters which appears in order in both strings • lots of applications, DNA sequencing, blah, blah, blah • Example: • “search me” and “insane method” = “same”

  23. Asymptotic Analysis Summary • Determine what characterizes a problem’s size • Express how much resources (time, memory, etc.) an algorithm requires as a function of input size using O(•), (•), (•) • worst case • best case • average case • common case • overall

  24. Remind Darth Wolfman to Hand Back the Quizzes • Well, what are you waiting for?

  25. To Do • Write up homework (for Wed. beginning of class) • Finalize Project I teams • Finish Project I (for Friday 10PM) • Read chapter 6 in the book • Keep thinking about whether you like this homework format

  26. Coming Up • Priority Qs • Self-balancing trees • First homework assignment due (January 12th) • First project due (January 14th) • A day off (January 17th)!

More Related