Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms Chapter 2.2 1 Asymptotic Notations* Dr. Ying Lu August 30, 2012 RAIK.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms Chapter 2.2 1 Asymptotic Notations* Dr. Ying Lu August 30, 2012 RAIK."— Presentation transcript:

1 Design and Analysis of Algorithms Chapter 2.2 1 Asymptotic Notations* Dr. Ying Lu ylu@cse.unl.edu August 30, 2012 http://www.cse.unl.edu/~ylu/raik283 RAIK 283: Data Structures & Algorithms *slides refrred tohttp://www.aw-bc.com/info/levitin

2 Design and Analysis of Algorithms Chapter 2.2 2 Review: algorithm efficiency indicator order of growth of an algorithm’s basic operation count the algorithm’s time efficiency

3 Design and Analysis of Algorithms Chapter 2.2 3 Asymptotic growth rate b A way of comparing functions that ignores constant factors and small input sizes b O(g(n)): class of functions t(n) that grow no faster than g(n) b Θ (g(n)): class of functions t(n) that grow at same rate as g(n) b Ω(g(n)): class of functions t(n) that grow at least as fast as g(n)

4 Design and Analysis of Algorithms Chapter 2.2 4 Big-oh  c > 0,  n 0  0,  n  n 0, t(n)  cg(n) t(n)  O(g(n))

5 Design and Analysis of Algorithms Chapter 2.2 5 Small-oh  c > 0,  n 0  0,  n  n 0, t(n) < cg(n) t(n)  o(g(n))

6 Design and Analysis of Algorithms Chapter 2.2 6 Big-omega t(n)   (g(n))

7 Design and Analysis of Algorithms Chapter 2.2 7 Big-omega  c > 0,  n 0  0,  n  n 0, t(n)  cg(n) t(n)   (g(n))

8 Design and Analysis of Algorithms Chapter 2.2 8 Small-omega  c > 0,  n 0  0,  n  n 0, t(n) > cg(n) t(n)   (g(n))

9 Design and Analysis of Algorithms Chapter 2.2 9 Big-theta t(n)   (g(n))  c 1 >c 2 >0,  n 0  0,  n  n 0, c 2 g(n)  t(n)  c 1 g(n)

10 Design and Analysis of Algorithms Chapter 2.2 10 Big theta The reverse statement of t(n)   (g(n))  c 1 >c 2 >0,  n 0  0,  n  n 0, c 2 g(n)  t(n)  c 1 g(n)

11 Design and Analysis of Algorithms Chapter 2.2 11 Big theta t(n)   (g(n))  c 1 >c 2 >0,  n 0  0,  n  n 0, t(n) c 1 g(n)

12 Design and Analysis of Algorithms Chapter 2.2 12 Establishing rate of growth: Method 1 – using definition b t(n) is O(g(n)) if order of growth of t(n) ≤ order of growth of g(n) (within constant multiple) b There exist positive constant c and non-negative integer n 0 such that t(n) ≤ c g(n) for every n ≥ n 0 t(n) ≤ c g(n) for every n ≥ n 0Examples: b 10n  O(2n 2 ) b 5n+20  O(10n)

13 Design and Analysis of Algorithms Chapter 2.2 13 ABOΩΘ 1ln 2 nn YesNoNo 2nknk cncnYesNoNo 3n sinnNoNoNo 42n2n 2 n/2NoYesNo 5n lgc c lgnYesYesYes 6lg(n!)lg(n n )YesYesYes 2 n   (2 n/2 ) 2 n   (2 n/2 ) Establishing rate of growth: Method 1 – using definition Examples:

14 Design and Analysis of Algorithms Chapter 2.2 14 ABOΩΘ 1ln 2 nn YesNoNo 2nknk cncnYesNoNo 3n sinnNoNoNo 42n2n 2 n/2NoYesNo 5n lgc c lgnYesYesYes 6lg(n!)lg(n n )YesYesYes  O(n sinn )   (n sinn ) Establishing rate of growth: Method 1 – using definition Examples:

15 Design and Analysis of Algorithms Chapter 2.2 15 Establishing rate of growth: Method 2 – using limits lim n→∞ t(n)/g(n) t(n)g(n) 0 order of growth of t(n) < order of growth of g(n) t(n)  o(g(n)),t(n)  O(g(n)) t(n)  o(g(n)), t(n)  O(g(n)) t(n)g(n) c>0 order of growth of t(n) = order of growth of g(n) t(n)   (g(n)),t(n)  O(g(n)), t(n)   (g(n)) t(n)   (g(n)), t(n)  O(g(n)), t(n)   (g(n)) t(n)g(n) ∞ order of growth of t(n) > order of growth of g(n) t(n)   (g(n)),t(n)   (g(n)) t(n)   (g(n)), t(n)   (g(n)) =

16 Design and Analysis of Algorithms Chapter 2.2 16 Establishing rate of growth: Method 2 – using limits Examples: log b n vs. log c n log b n = log b c log c n lim n→∞ ( log b n / log c n) = lim n→∞ (log b c) = log b c log b n  (log c n)

17 Design and Analysis of Algorithms Chapter 2.2 17 Exercises: establishing rate of growth – using limits b ln 2 n vs. lnn 2 b 2 n vs. 2 n/2 b 2 n-1 vs. 2 n b log 2 n vs. n

18 Design and Analysis of Algorithms Chapter 2.2 18 L’Hôpital’s rule If b lim n→∞ t(n) = lim n→∞ g(n) = ∞ b The derivatives f´, g´ exist, Then t(n) g(n) lim n→∞ = t ´(n) g ´(n) lim n→∞ Example: log 2 n vs. n Example: log 2 n vs. n

19 Design and Analysis of Algorithms Chapter 2.2 19 ABOΩΘ 1ln 2 nn YesNoNo 2nknk cncnYesNoNo 3n sinnNoNoNo 42n2n 2 n/2NoYesNo 5n lgc c lgnYesYesYes 6lg(n!)lg(n n )YesYesYes Establishing rate of growth Examples:

20 Design and Analysis of Algorithms Chapter 2.2 20 Stirling’s formula

21 Design and Analysis of Algorithms Chapter 2.2 21 n! v.s. n n lg(n!) v.s. lg(n n ) Examples using stirling’s formula   O

22 Design and Analysis of Algorithms Chapter 2.2 22 n!  o(n n ) lg(n!) v.s. lg(n n ) ??? lg(n!)  o(lg(n n )) Examples using stirling’s formula   O

23 Design and Analysis of Algorithms Chapter 2.2 23 n!  o(n n ) lg(n!) v.s. lg(n n ) lg(n!)   (lg(n n )) Examples using stirling’s formula   O

24 Design and Analysis of Algorithms Chapter 2.2 24 Special attention b n!  o(n n ) However, lg(n!)  o(lg(n n ))lg(n!)   (lg(n n )) b sinn   (1/2)sinn  O(1/2)sinn   (1/2) However, n 1/2   (n sinn )n 1/2  O(n sinn )n 1/2   (n sinn )

25 Design and Analysis of Algorithms Chapter 2.2 25 Asymptotic notation properties b Transitivity: f(n) =  (g(n)) && g(n) =  (h(n))  f(n) =  (h(n)) f(n) = O(g(n)) && g(n) = O(h(n))  f(n) = O(h(n)) f(n) = Ω(g(n)) && g(n) = Ω(h(n))  f(n) = Ω(h(n)) b If t 1 (n)  O(g 1 (n)) and t 2 (n)  O(g 2 (n)), then t 1 (n) + t 2 (n) 

26 Design and Analysis of Algorithms Chapter 2.2 26 Asymptotic notation properties b Transitivity: f(n) =  (g(n)) && g(n) =  (h(n))  f(n) =  (h(n)) f(n) = O(g(n)) && g(n) = O(h(n))  f(n) = O(h(n)) f(n) = Ω(g(n)) && g(n) = Ω(h(n))  f(n) = Ω(h(n)) b If t 1 (n)  O(g 1 (n)) and t 2 (n)  O(g 2 (n)), then t 1 (n) + t 2 (n)  O(max{g 1 (n), g 2 (n)})

27 In-Class Exercises b Exercises 2.2: Problem 1, 2, 3 & 12 b Problem 1: Use the most appropriate notation among O, , and  to indicate the time efficiency class of sequential search: a. in the worst casea. in the worst case b. in the best caseb. in the best case c. in the average case ( Hint: C(n) = p*(n+1)/2 + (1-p)*n )c. in the average case ( Hint: C(n) = p*(n+1)/2 + (1-p)*n ) b Problem 2: Use the informal definitions of O, , and  to determine whether the following assertions are true or false. a. n(n+1)/2  O(n 3 )b. n(n+1)/2  O(n 2 )a. n(n+1)/2  O(n 3 )b. n(n+1)/2  O(n 2 ) c. n(n+1)/2   (n 3 )d. n(n+1)/2   (n)c. n(n+1)/2   (n 3 )d. n(n+1)/2   (n) Design and Analysis of Algorithms Chapter 2.2 27

28 Announcement b 40-minute quiz next Tuesday problems based on materials covered in Chapter 2.2 (Asymptotic Notations and Basic Efficiency Classes)problems based on materials covered in Chapter 2.2 (Asymptotic Notations and Basic Efficiency Classes) Design and Analysis of Algorithms Chapter 2.2 28

29 In-Class Exercises b Establish the asymptotic rate of growth (O, , and  ) of the following pair of functions. Prove your assertions. a. 2 n vs. 3 n b. ln(n+1) vs. ln(n)a. 2 n vs. 3 n b. ln(n+1) vs. ln(n) b Problem 3: For each of the following functions, indicate the class  (g(n)) the function belongs to. (Use the simplest g(n) possible in your answers.) Prove your assertions. a. (n 2 + 1) 10 b.a. (n 2 + 1) 10 b. c. 2nlg(n+2) 2 + (n+2) 2 lg(n/2)c. 2nlg(n+2) 2 + (n+2) 2 lg(n/2) d. 2 n+1 + 3 n-1 e.  log 2 n d. 2 n+1 + 3 n-1 e.  log 2 n  Design and Analysis of Algorithms Chapter 2.2 29


Download ppt "Design and Analysis of Algorithms Chapter 2.2 1 Asymptotic Notations* Dr. Ying Lu August 30, 2012 RAIK."

Similar presentations


Ads by Google