1 / 43

Runtime Analysis

Runtime Analysis. CSC 172 SPRING 2004 LECTURE 3. RUNNING TIME. A program or algorithm has running time T( n ), where n is the measure of the size of the input. For sorting algorithms, we typically choose n to be the number of elements sorted Best case Average case Worst case

Download Presentation

Runtime 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. Runtime Analysis CSC 172 SPRING 2004 LECTURE 3

  2. RUNNING TIME A program or algorithm has running time T(n), where n is the measure of the size of the input. • For sorting algorithms, we typically choose n to be the number of elements sorted • Best case • Average case • Worst case There is an unknowable (machine dependent) constant factor.

  3. Problems with Runtime Measurement Algorithms can vary depending on the input. • Worst case QuickSort: T(n) = n2 • Average case QuickSort: T(n) = n log n Machine speed increases • Faster machines enable bigger problems • Bigger problems bring us closer to the asymptote Constant factor are important • Anything is ok when input is small Benchmarking as an alternative • You have to build it to benchmark it

  4. The effect of the constant factors

  5. VERY QUICK MATH REVEIW

  6. The Constant Function f(n) = c

  7. The Linear Function f(n) = n

  8. The Quadratic Function f(n) = n2

  9. The Cubic Function f(n) = n3

  10. The “Polynomial” Function f(n) = a0+a1n+a2n2+…+adnd

  11. The Exponential Function f(n) = bn (ba)c = bac babc=ba+c ba/bc = ba-c

  12. The Logarithm Function f(n) = logbn

  13. x = logbn If and only if bx = n

  14. Logartihm Rules • logbac = logba + logbc • logb(a/c) = logba – logbc • logbac = clogba • logba = (logda)/(logdb) • blogda = alogdb

  15. The N-Log-N Function f(n) = n log n

  16. Big-Oh • Big-Oh is a notation that abstracts away the unknowable constant factors and focuses on growth rate. • We say “T(n) is O(f(n))” if for large n, T(n) is no more than proportional to f(n) • Formally • There exists constants n0 and c > 0 such that for all n>=n0, we have T(n) <= cf(n) • n0 and c are called “witnesses” to the fact that T(n) is O(f(n))

  17. Some Others

  18. Example Big-Oh 10n2+50n+100 is O(n2) Pick witnesses n0==10 c == 16 Then for any n >= 10, 10n2+50n+100 <= 16n2

  19. 10n2+50n+100 and n2

  20. 10n2+50n+100 and n2 n0==10

  21. 10n2+50n+100 and n2 and 16n2 n0==10

  22. Example, alternate 10n2+50n+100 is O(n2) Pick one witness n0==1 10n2+50n+100 <= Cn2 10n2+50n+100 <= 10n2 + 50n + 100n 10n2+50n+100 <= 10n2 + 150n 10n2+50n+100 <= 10n2 + 150n2 10n2+50n+100 <= 160n2 c == 160

  23. Example n10 is O(2n) n10<2n Is the same as saying log2(n10) < log2(2n) 10 log2n < n False for n = 32, true n = 64 So, with c==1 and n0 == 64, n10 < 1 * 2n

  24. 10log2(n) and n

  25. General Rules • Any Polynomial is big-oh of its leading term with coefficient of 1 • The base of a logarithm doesn’t matter. logan is O(logbn) for any bases a and b because logan= logbn logab , (i.e. n0==1, c = logab) • Logs grow slower than powers [log n is O(n1/10)] • Exponentials (cn, c>1) grow faster than poly n10 is O(1.0001n) • Generally, polynomial time is tolerable • Generally, exponential time is intolerable

  26. Disproving Big-Oh • Proof by contradiction n3 is not O(n2) If it were then n0 and c would exist n3 < cn2 Choose n1 at least as large as n0 Choose n1 at least as large as 2c If n13 <= cn12 then n1 <= c But n1 >= 2c

  27. Runtime of programs Measure some “size” of the input the value of some integer The length of a string or array

  28. Program Analysis • Basis cases (simple statements) constant time • Assignments • Break, continue, return • System.in.println() • Induction • Loops • Branching (if, if-else) • Blocks {….}

  29. Structure Tree • Nodes are complex statements • Children are constituent statements

  30. Example • Write a method that converts positive integers to binary strings • You have 5 min. • Hang it in at the end of class public String int2bin(int n) {}

  31. public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; }

  32. 1 2 3 4 5 6 7 8 9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; }

  33. 1 2 3 4 5 6 7 8 9 1-9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; }

  34. 1 2 3 4 5 6 7 8 9 1-9 1 2-8 9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; }

  35. 1 2 3 4 5 6 7 8 9 1-9 1 2-8 9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } ? times 3-7

  36. 1 2 3 4 5 6 7 8 9 1-9 1 2-8 9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } ? times 3-7 3-6 7

  37. 1 2 3 4 5 6 7 8 9 1-9 1 2-8 9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } ? times 3-7 3-6 7 O(1) + max 4 6

  38. 1 2 3 4 5 6 7 8 9 1-9 1 2-8 9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1” n=/2; } return rval; } ? times 3-7 3-6 7 O(1) + max 4 6 O(1) O(1)

  39. 1 2 3 4 5 6 7 8 9 1-9 1 2-8 9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } ? times 3-7 O(1) O(1) O(1) 3-6 7 O(1) + max 4 6 O(1) O(1)

  40. 1 2 3 4 5 6 7 8 9 1-9 1 2-8 9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } log2n times 3-7 O(1) O(1) O(1) 3-6 7 O(1) + max 4 6 O(1) O(1)

  41. 1 2 3 4 5 6 7 8 9 O(log2n) 1-9 O(1) O(log2n) O(1) 1 2-8 9 public String int2bin(int n) { String rval; while (n>0) { if((n%2) == 1) rval += “0”; else rval +=“1”; n=/2; } return rval; } log2n times 3-7 O(1) O(1) O(1) 3-6 7 O(1) + max 4 6 O(1) O(1)

More Related