1 / 24

Analysis of Algorithms CS 477/677

Analysis of Algorithms CS 477/677. Instructor: Monica Nicolescu Lecture 2. Algorithm Analysis. The amount of resources used by the algorithm Space Computational time Running time: The number of primitive operations (steps) executed before termination Order of growth

elom
Download Presentation

Analysis of Algorithms CS 477/677

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. Analysis of AlgorithmsCS 477/677 Instructor: Monica Nicolescu Lecture 2

  2. Algorithm Analysis • The amount of resources used by the algorithm • Space • Computational time • Running time: • The number of primitive operations (steps) executed before termination • Order of growth • The leading term of a formula • Expresses the behavior of a function toward infinity CS 477/677 - Lecture 2

  3. Asymptotic Notations • A way to describe behavior of functions in the limit • How we indicate running times of algorithms • Describe the running time of an algorithm as n grows to  • O notation: asymptotic “less than”: f(n) “≤” g(n) •  notation: asymptotic “greater than”: f(n) “≥” g(n) •  notation: asymptotic “equality”: f(n) “=” g(n) CS 477/677 - Lecture 2

  4. Logarithms • In algorithm analysis we often use the notation “log n” without specifying the base Binary logarithm Natural logarithm CS 477/677 - Lecture 2

  5. Asymptotic Notations - Examples • For each of the following pairs of functions, either f(n) is O(g(n)), f(n) is Ω(g(n)), or f(n) is Θ(g(n)). Determine which relationship is correct. • f(n) = log n2; g(n) = log n + 5 • f(n) = n; g(n) = log n2 • f(n) = log log n; g(n) = log n • f(n) = n; g(n) = log2 n • f(n) = n log n + n; g(n) = log n • f(n) = 10; g(n) = log 10 • f(n) = 2n; g(n) = 10n2 • f(n) = 2n; g(n) = 3n f(n) =  (g(n)) f(n) = (g(n)) f(n) = O(g(n)) f(n) = (g(n)) f(n) = (g(n)) f(n) = (g(n)) f(n) = (g(n)) f(n) = O(g(n)) CS 477/677 - Lecture 2

  6. Asymptotic notations • O-notation • Intuitively: O(g(n)) = the set of functions with a smaller or same order of growth as g(n) CS 477/677 - Lecture 2

  7. Examples • 2n2 = O(n3): • n2 = O(n2): • 1000n2+1000n = O(n2): • n = O(n2): 2n2≤ cn3  2 ≤ cnc = 1 and n0= 2 n2≤ cn2 c ≥ 1 c = 1 and n0= 1 1000n2+1000n ≤ 1000n2+ 1000n2 = 2000n2 c=2000 and n0 = 1 n≤ cn2 cn ≥ 1 c = 1 and n0= 1 CS 477/677 - Lecture 2

  8. Asymptotic notations (cont.) •  - notation • Intuitively: (g(n)) = the set of functions with a larger or same order of growth as g(n) CS 477/677 - Lecture 2

  9. Examples • 5n2 = (n) • 100n + 5 ≠(n2) • n = (2n), n3 = (n2), n = (logn)  cn  5n2  c = 1 and n0 = 1 •  c, n0such that: 0  cn  5n2  c, n0 such that: 0  cn2  100n + 5 100n + 5  100n + 5n ( n  1) = 105n cn2  105n  n(cn – 105)  0  n  105/c Since n is positive  cn – 105  0  contradiction: n cannot be smaller than a constant CS 477/677 - Lecture 2

  10. Asymptotic notations (cont.) • -notation • Intuitively(g(n)) = the set of functions with the same order of growth as g(n) CS 477/677 - Lecture 2

  11. Examples • n2/2 –n/2 = (n2) • ½ n2 - ½ n ≤ ½ n2n ≥ 0  c2= ½ • ½ n2 - ½ n ≥ ½ n2 - ½ n * ½ n ( n ≥ 2 ) = ¼ n2 c1= ¼ • n ≠ (n2): c1 n2≤ n ≤ c2 n2  only holds for: n ≤ 1/c1 CS 477/677 - Lecture 2

  12. Examples • 6n3 ≠ (n2): c1 n2≤ 6n3 ≤ c2 n2  only holds for: n ≤ c2 /6 • n ≠ (logn): c1logn≤ n ≤ c2 logn  c2 ≥ n/logn,  n≥ n0 – impossible CS 477/677 - Lecture 2

  13. More on Asymptotic Notations • There is no unique set of values for n0 and c in proving the asymptotic bounds • Prove that 100n + 5 = O(n2) • 100n + 5 ≤ 100n + n = 101n ≤ 101n2 for all n ≥ 5 n0 = 5 and c = 101is a solution • 100n + 5 ≤ 100n + 5n = 105n ≤ 105n2for all n ≥ 1 n0 = 1 and c = 105is also a solution Must findSOMEconstants c and n0 that satisfy the asymptotic notation relation CS 477/677 - Lecture 2

  14. Comparisons of Functions • Theorem: f(n) = (g(n))  f = O(g(n)) and f = (g(n)) • Transitivity: • f(n) = (g(n))andg(n) = (h(n))  f(n) = (h(n)) • Same for O and  • Reflexivity: • f(n) = (f(n)) • Same for O and  • Symmetry: • f(n) = (g(n)) if and only if g(n) = (f(n)) • Transpose symmetry: • f(n) = O(g(n)) if and only if g(n) = (f(n)) CS 477/677 - Lecture 2

  15. Asymptotic Notations in Equations • On the right-hand side • (n2) stands for some anonymous function in (n2) 2n2 + 3n + 1 = 2n2 + (n) means: There exists a function f(n)  (n) such that 2n2 + 3n + 1 = 2n2 + f(n) • On the left-hand side 2n2 + (n) = (n2) No matter how the anonymous function is chosen on the left-hand side, there is a way to choose the anonymous function on the right-hand side to make the equation valid. CS 477/677 - Lecture 2

  16. Some Simple Summation Formulas • Arithmetic series: • Geometric series: • Special case: x < 1: • Harmonic series: • Other important formulas: CS 477/677 - Lecture 2

  17. Mathematical Induction • Used to prove a sequence of statements (S(1), S(2), … S(n)) indexed by positive integers • Proof: • Basis step: prove that the statement is true for n = 1 • Inductive step: assume that S(n) is true and prove that S(n+1) is true for all n ≥ 1 • Find case n “within” case n+1 CS 477/677 - Lecture 2

  18. Example • Prove that: 2n + 1 ≤ 2n for all n ≥ 3 • Basis step: • n = 3: 2  3 + 1 ≤ 23 7 ≤ 8 TRUE • Inductive step: • Assume inequality is true for n, and prove it for (n+1) Assume: 2n + 1 ≤ 2n Must prove: 2(n + 1) + 1 ≤ 2n+1 2(n + 1) + 1 = (2n + 1 ) + 2 ≤ 2n + 2 ≤  2n + 2n = 2n+1, since 2 ≤ 2nfor n ≥ 1 CS 477/677 - Lecture 2

  19. More Examples CS 477/677 - Lecture 2

  20. 1 2 3 4 5 6 7 8 2 3 5 7 9 10 11 12 mid lo hi Recurrent AlgorithmsBINARY – SEARCH • for an ordered array A, finds if x is in the array A[lo…hi] Alg.:BINARY-SEARCH (A, lo, hi, x) if (lo > hi) return FALSE mid  (lo+hi)/2 if x = A[mid] return TRUE if ( x < A[mid] ) BINARY-SEARCH (A, lo, mid-1, x) if ( x > A[mid] ) BINARY-SEARCH (A, mid+1, hi, x) CS 477/677 - Lecture 2

  21. 1 2 3 4 5 6 7 8 1 1 2 2 3 3 4 4 5 5 7 7 9 9 11 11 mid = 4, lo = 5, hi = 8 mid = 6, A[mid] = x Found! Example • A[8] = {1, 2, 3, 4, 5, 7, 9, 11} • lo = 1 hi = 8 x = 7 CS 477/677 - Lecture 2

  22. 1 2 3 4 5 6 7 8 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 7 7 7 9 9 9 11 11 11 mid = 4, lo = 5, hi = 8 mid = 6, A[6] = 7, lo = 5, hi = 5 mid = 5, A[5] = 5, lo = 6, hi = 5 NOT FOUND! Example • A[8] = {1, 2, 3, 4, 5, 7, 9, 11} • lo = 1 hi = 8 x = 6 CS 477/677 - Lecture 2

  23. Analysis of BINARY-SEARCH Alg.:BINARY-SEARCH (A, lo, hi, x) if (lo > hi) returnFALSE mid  (lo+hi)/2 ifx = A[mid] return TRUE if ( x < A[mid] ) BINARY-SEARCH (A, lo, mid-1, x) if ( x > A[mid] ) BINARY-SEARCH (A, mid+1, hi, x) • T(n) = c + • T(n) – running time for an array of size n constant time: c1 constant time: c2 constant time: c3 same problem of size n/2 same problem of size n/2 T(n/2) CS 477/677 - Lecture 2

  24. Readings • Chapter 3 • Apendix A CS 477/677 - Lecture 2

More Related