Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Growth of Functions: Selected Exercises Goals Introduce big-O & big-Omega Show how to estimate the size of functions using this notation.

Similar presentations


Presentation on theme: "The Growth of Functions: Selected Exercises Goals Introduce big-O & big-Omega Show how to estimate the size of functions using this notation."— Presentation transcript:

1 The Growth of Functions: Selected Exercises Goals Introduce big-O & big-Omega Show how to estimate the size of functions using this notation.

2 Copyright © Peter Cappello2 Preface You may use without proof that: The functions below increase asymptotically from top to bottom: f( n ) = k, for some constant k. f( n ) = log k n, for all k  N & any constant log base ≥ 2. f( n ) = n q, for all q  Q + f( n ) = k n < (k+1) n, for all k  N f( n ) = n!

3 Copyright © Peter Cappello3 Preface continued The book says that f(n) is O( g( n ) ) when  k  c  n ( n > k  | f( n ) |  c | g( n ) | ) In computational complexity, we deal exclusively with functions whose domains & ranges are positive. We thus may simplify the definition of O( ) as follows:  k  c  n > k f( n )  c g( n ). You are not responsible for knowing o(). This is different from O().

4 Copyright © Peter Cappello4 Exercise 10 Defn: f( n ) is O( g( n ) ) if  k  c  n > k f( n )  cg( n ). Show that: 1) n 3 is O( n 4 ) 2) n 4 is not O( n 3 ).

5 Copyright © Peter Cappello5 Exercise10: Solution Defn: f( n ) is O( g( n ) ) when  k  c  n > k f( n )  cg( n ). Show that: 1) n 3 is O( n 4 ) 2) n 4 is not O( n 3 ). 1)For n ≥ 1 & c = 1: n 3  1n 4  1  n. 2)Proof (by contradiction) 1)Assume  k  c  n > k n 4  cn 3. 2)  k  c  n > k n 4  cn 3   k  c  n > k n  c ), which is false. ( Divide both sides by n 3 ) 3) Therefore, n 4 is not O( n 3 ).

6 Copyright © Peter Cappello6 Theorems You Can Use Thm 1: Let f(x) = a n x n + a n-1 x n-1 + … + a 1 x + a 0, where the a i are real. Then, f(x) is O( x n ). Let f 1 (x) is O (g 1 (x) ) & f 2 (x) is O( g 2 (x) ). Thm 2: (f 1 + f 2 )(x) is O( max( g 1 (x), g 2 (x) ) ) Thm 3: (f 1 f 2 )(x) is O( (g 1 g 2 )(x) ).

7 Copyright © Peter Cappello7 Exercise 20 Give a big-O estimate for the functions: (Use a simple g of smallest order.) a) f( n ) = ( n 3 + n 2 logn )( logn + 1 ) + ( 17logn + 19 )( n ).

8 Copyright © Peter Cappello8 Exercise 20 a) Solution Give a big-O estimate for the functions: (Use a simple g of smallest order.) a) f( n ) = ( n 3 + n 2 logn )( logn + 1 ) + ( 17logn + 19 )( n ). Using our theorems, ( n 3 + n 2 logn )( logn + 1 ) + ( 17logn + 19 )( n ) Is O( ( n 3 )( logn ) + ( 17logn )( n 3 ) ) Is O( ( n 3 logn ) + ( n 3 17logn ) Is O( ( n 3 logn ).

9 Copyright © Peter Cappello9 Exercise 20 b) b) f( n ) = ( 2 n + n 2 )( n n ).

10 Copyright © Peter Cappello10 Exercise 20 b) Solution b) f( n ) = ( 2 n + n 2 )( n n ). Using our theorems, f( n ) = ( 2 n + n 2 )( n n ) is O( ( 2 n )( 3 n ) ) which is O( 2 n 3 n ) which is O( 6 n ).

11 Copyright © Peter Cappello11 Exercise 20 c) c) f( n ) = ( n n + n2 n + 5 n )( n! + 5 n )

12 Copyright © Peter Cappello12 Exercise 20 c) Solution Defn: f( n ) is O( g( n ) ) when  k  c  n > k f( n )  cg( n ). c) f( n ) = ( n n + n2 n + 5 n )( n! + 5 n ) Using our theorems, f( n ) is O( ( n n + n2 n )( n! ) ) In n n + n2 n, which is the fastest growing term? Claim: n2 n is O ( n n ) : 1. n ≥ 2  2 n-1  n n n ≥ 2  n2 n  2n n. (Multiply both sides of 1. by 2n.) Thus, f( n ) is O( n n n! ).

13 Copyright © Peter Cappello13 Exercise 30 Defn: f( n ) is O( g( n ) ) when  k  c  n > k f( n )  cg( n ). Defn. f( n ) is Ω( g( n ) ) when  k  c > 0  n > k f( n ) ≥ cg( n ). What does it mean for f( n ) to be Ω( 1 )? Hint: graph f( n ).

14 Copyright © Peter Cappello14 Exercise 30 Solution Defn. f( n ) is Ω( g( n ) ) when  k  c > 0  n > k f( n ) ≥ c g( n ). What does it mean for a function to be Ω( 1 )? From the definition, f( n ) is Ω( 1 ) when  k  c > 0  n > k f( n ) ≥ c. f( n ) ≥ c > 0, for sufficiently large n. f( n ) = 1/n is Ω( 1 ). True or false?

15 Copyright © Peter Cappello15 Generalizing the definitions Defn: f( n ) is O( g( n ) ) when  k  c  n > k f( n )  cg( n ). What is a good definition of f( n, m ) is O( g( n, m ) )?

16 Copyright © Peter Cappello16 Time Complexity of Bubble Sort void bubblesort( int[] a ) { for ( int i = 0; i < a.length – 1; i++ ) for ( int j = 0; j < a.length – 1 – i; j++ ) { if ( a[ j ] < a[ j + 1 ] ) { int temp = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = temp; } Let a.length = n. What is the total # of comparisons as a function of n? This number is O( ? )

17 Copyright © Peter Cappello17 End 3.2

18 Copyright © Peter Cappello Defn. f( n ) is Θ( g( n ) ) when f( n ) is O( g ( n ) ) and f( n ) is Ω( g( n ) ). Show that: if f 1 ( x ) & f 2 ( x ) are functions from Z + to R and f 1 ( x ) is Θ( g 1 ( x ) ) and f 2 ( x ) is Θ( g 2 ( x ) ), then f 1 f 2 ( x ) is Θ( g 1 g 2 ( x ) ).

19 Copyright © Peter Cappello Proof 1.Assume f 1 ( x ) & f 2 ( x ) are functions from Z + to R and f 1 ( x ) is Θ( g 1 ( x ) ) and f 2 ( x ) is Θ( g 2 ( x ) ). 2.f 1 ( x ) is O( g 1 ( x ) ). (1. and defn of Θ) 3.  k 1, C 1, x > k 1  f 1 ( x )  C 1 g 1 ( x ) (2.,Defn of O) 4.f 2 ( x ) is O( g 2 ( x ) ). (1. and defn of Θ) 5.  k 2, C 2, x > k 2  f 2 ( x )  C 2 g 2 ( x ) (4., Defn of O) 6.x > max{ k 1, k 2 }  f 1 f 2 ( x )  C 1 C 2 g 1 g 2 ( x ) 7.f 1 f 2 ( x ) is O( g 1 g 2 ( x ) ). (6., Defn of O)

20 Copyright © Peter Cappello Proof continued 1.f 1 ( x ) is Ω( g 1 ( x ) ). (Previous 1. & defn of Θ) 2.  k’ 1, C’ 1, x > k’ 1  f 1 ( x ) ≥ C’ 1 g 1 ( x ) (1. & Defn of Ω) 3.f 2 ( x ) is Ω( g 2 ( x ) ). (Previous 1. & defn of Θ) 4.  k’ 2, C’ 2, x > k’ 2  f 2 ( x ) ≥ C’ 2 g 2 ( x ) (3. & Defn of Ω) 5.x > max{ k’ 1, k’ 2 }  f 1 f 2 ( x ) ≥ C’ 1 C’ 2 g 1 g 2 ( x ) 6.f 1 f 2 ( x ) is Ω( g 1 g 2 ( x ) ). (5. & Defn of Ω) 7.f 1 f 2 ( x ) is Θ( g 1 g 2 ( x ) ). (6., previous 7., defn Θ)

21 Copyright © Peter Cappello Show that ┌ xy ┐ is Ω(xy). Proof: 1. ┌ xy ┐ ≥ xy. (Defn of ceiling) 2.Let c = 1. 3.For x > 0, y > 0, ┌ xy ┐ ≥ cxy. 4.Therefore, ┌ xy ┐ is Ω(xy). (Defn of Ω)


Download ppt "The Growth of Functions: Selected Exercises Goals Introduce big-O & big-Omega Show how to estimate the size of functions using this notation."

Similar presentations


Ads by Google