Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter."— Presentation transcript:

1 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

2 A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Asymptotic order of growth A way of comparing functions that ignores constant factors and small input sizes  O(g(n)): class of functions f(n) that grow no faster than g(n)‏  Θ (g(n)): class of functions f(n) that grow at same rate as g(n)‏  Ω (g(n)): class of functions f(n) that grow at least as fast as g(n)‏

3 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Establishing order of growth using the definition Definition: f(n) is in O(g(n)) if order of growth of f(n) ≤ order of growth of g(n) (within constant multiple), i.e., there exist positive constant c and non-negative integer n 0 such that f(n) ≤ c g(n) for every n ≥ n 0 f(n) ≤ c g(n) for every n ≥ n 0Examples:  10n is O(n 2 )‏  5n+20 is O(n)‏

4 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Big-oh

5 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Big-omega

6 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Big-theta

7 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Some properties of asymptotic order of growth  f(n)  O(f(n))  f(n)  O(g(n)) iff g(n)  (f(n))  If f (n)  O(g (n)) and g(n)  O(h(n)), then f(n)  O(h(n)) Note similarity with a ≤ b  If f 1 (n)  O(g 1 (n)) and f 2 (n)  O(g 2 (n)), then f 1 (n) + f 2 (n)  O(max{g 1 (n), g 2 (n)}) f 1 (n) + f 2 (n)  O(max{g 1 (n), g 2 (n)})

8 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Establishing order of growth using limits lim T(n)/g(n) = 0T(n)g(n) 0 order of growth of T(n) < order of growth of g(n) c > 0T(n)g(n) c > 0 order of growth of T(n) = order of growth of g(n) ∞T(n)g(n) ∞ order of growth of T(n) > order of growth of g(n) Example:Example: n(n-1)/2 vs. n 2 n→∞

9 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 L’Hôpital’s rule and Stirling’s formula L’Hôpital’s rule: If lim n  f(n) = lim n  g(n) =  and the derivatives f´, g´ exist, then the derivatives f´, g´ exist, then Stirling’s formula: n!  (2  n) 1/2 (n/e) n f(n)‏ g(n)‏ lim n  = f ´(n)‏ g ´(n)‏ lim n 

10 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Orders of growth of some important functions  All logarithmic functions log a nbelong to the same class  (log n)no matter what the logarithm’s base a > 1 is  All logarithmic functions log a n belong to the same class  (log n) no matter what the logarithm’s base a > 1 is  All polynomials of the same degree k belong to the same class: a k n k + a k-1 n k-1 + … + a 0   (n k )  Exponential functions a n have different orders of growth for different a’s  order log n 0) 0) < order a n < order n! < order n n

11 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Basic asymptotic efficiency classes 1constant log n logarithmic nlinear n log n n-log-n n2n2n2n2quadratic n3n3n3n3cubic 2n2n2n2nexponential n!n!n!n!factorial

12 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Values of some important functions as n  

13 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Time efficiency of nonrecursive algorithms General Plan for Analysis  Decide on parameter n indicating input size  Identify algorithm’s basic operation  Determine worst, average, and best cases for input of size n  Set up a sum for the number of times the basic operation is executed  Simplify the sum using standard formulas and rules (see Appendix A)‏

14 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Useful summation formulas and rules  l  i  u 1 = 1+1+…+1 = u - l + 1 In particular,  l  i  u 1 = n - 1 + 1 = n   (n) In particular,  l  i  u 1 = n - 1 + 1 = n   (n)  1  i  n i = 1+2+…+n = n(n+1)/2  n 2 /2   (n 2 )  1  i  n i 2 = 1 2 +2 2 +…+n 2 = n(n+1)(2n+1)/6  n 3 /3   (n 3 )  0  i  n a i = 1 + a +…+ a n = (a n+1 - 1)/(a - 1) for any a  1 In particular,  0  i  n 2 i = 2 0 + 2 1 +…+ 2 n = 2 n+1 - 1   (2 n ) In particular,  0  i  n 2 i = 2 0 + 2 1 +…+ 2 n = 2 n+1 - 1   (2 n )  (a i ± b i ) =  a i ±  b i  ca i = c  a i  l  i  u a i =  l  i  m a i +  m+1  i  u a i

15 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Example 1: Maximum element Measure of input size - ? Basic Operation - ? (operation performed most often)‏ How many times is the basic operation executed? C(n)=  i=1 to n-1 1 = n-1   (n)‏

16 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Example 2: Element uniqueness problem Measure of input size - ? Basic Operation - ? (operation performed most often)‏ How many times is the basic operation executed? Note: Are worst case and best case the same?

17 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Example 2 – Worst Case Analysis  2 cases – Both equally bad 1.The last two elements are the only two that are the same 2.No two elements are the same  How many comparisons are made for each of these cases? C worst (n) =  i=0 to n-2  j=i+1 to n-1 1C worst (n) =  i=0 to n-2  j=i+1 to n-1 1 =  i=0 to n-2 [(n-1)-(i+1)+1] =  i=0 to n-2 [(n-1)-(i+1)+1] =  i=0 to n-2 (n-1-i)‏ =  i=0 to n-2 (n-1-i)‏ = (n-1) + (n-2) + (n-3) + … + 1 = (n-1) + (n-2) + (n-3) + … + 1 by (S2)‏ by (S2)‏ = ((n-1)n)/2 = (n 2 – n) / 2 = ((n-1)n)/2 = (n 2 – n) / 2 approx. = n 2 /2  O(n 2 )‏ approx. = n 2 /2  O(n 2 )‏ 1-17

18 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Example 3: Matrix multiplication Measure of input size - ? Basic Operation - ? (operation performed most often)‏ How many times is the basic operation executed? Note: Are worst case and best case the same?

19 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Worst, Best, and Average  C (n) =  i=0 to n-1  j=0 to n-1  k=0 to n-1 1  C (n) =  i=0 to n-1  j=0 to n-1 n  C (n) =  i=0 to n-1 n 2  C (n) = n 3  O(n 3 )‏ 1-19

20 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Example 4: Counting binary digits It cannot be investigated in exactly the way the previous examples are.  What’s the most often done operation?  How many times does it happen (summations don’t apply here)‏ n is divided in half each time so it should happen about log 2 n times n is divided in half each time so it should happen about log 2 n times

21 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Plan for Analysis of Recursive Algorithms  Decide on a parameter indicating an input’s size.  Identify the algorithm’s basic operation.  Check whether the number of times the basic op. is executed may vary on different inputs of the same size. (If it may, the worst, average, and best cases must be investigated separately.)  Set up a recurrence relation with an appropriate initial condition expressing the number of times the basic op. is executed.  Solve the recurrence (or, at the very least, establish its solution’s order of growth) by backward substitutions or another method.

22 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Example 1: Recursive evaluation of n! Definition: n ! = 1  2  …  (n-1)  n for n ≥ 1 and 0! = 1 Recursive definition of n!: F(n) = F(n-1)  n for n ≥ 1 and F(0) = 1 F(0) = 1Size: Basic operation: Recurrence relation:

23 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Solving the recurrence for M(n)‏ M(n) = M(n-1) + 1, M(0) = 0 recurrence, initial condition Backward Substitution to Solve M(n) = M(n-1) + 1 = [M(n-2)+1]+1 = M(n-2)+2 = [M(n-2)+1]+1 = M(n-2)+2 = [M(n-3)+1]+2 = M(n-3)+3 = [M(n-3)+1]+2 = M(n-3)+3 See a pattern? M(n) = M(n-1)+1 = … = M(n-i)+i = … M(n-n)+n = n M(n) = M(n-1)+1 = … = M(n-i)+i = … M(n-n)+n = n Lots of work for the obvious answer – O(n)


Download ppt "Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter."

Similar presentations


Ads by Google