The Growth of Functions: Selected Exercises

Slides:



Advertisements
Similar presentations
The Growth of Functions: Selected Exercises
Advertisements

 The running time of an algorithm as input size approaches infinity is called the asymptotic running time  We study different notations for asymptotic.
Discrete Mathematics CS 2610 February 26, part 1.
Reference: Tremblay and Cheston: Section 5.1 T IMING A NALYSIS.
The Growth of Functions
Asymptotic Growth Rate
Cutler/HeadGrowth of Functions 1 Asymptotic Growth Rate.
Introduction to Proofs Goals 1.Introduce notion of proof & basic proof methods. 2.Distinguish between correct & incorrect arguments 3.Understand & construct.
CSE 373 Data Structures and Algorithms Lecture 4: Asymptotic Analysis II / Math Review.
The Growth of Functions Rosen 2.2 Basic Rules of Logarithms log z (xy) log z (x/y) log z (x y ) If x = y If x < y log z (-|x|) is undefined = log z (x)
MCA 202: Discrete Structures Instructor Neelima Gupta
Asymptotic Notation (O, Ω, )
Introduction to Programming (in C++) Complexity Analysis of Algorithms
Introduction to Algorithms: Verification, Complexity, and Searching (2) Andy Wang Data Structures, Algorithms, and Generic Programming.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
1 Section 5.6 Comparing Rates of Growth We often need to compare functions ƒ and g to see whether ƒ(n) and g(n) are about the same or whether one grows.
EXAMPLE 3 Write an indirect proof Write an indirect proof that an odd number is not divisible by 4. GIVEN : x is an odd number. PROVE : x is not divisible.
Big-O. Speed as function Function relating input size to execution time – f(n) = steps where n = length of array f(n) = 4(n-1) + 3 = 4n – 1.
Introduction to Proofs Goals 1.Introduce notion of proof & basic proof methods. 2.Distinguish between correct & incorrect arguments 3.Understand & construct.
Algorithm Analysis 1.
CSCE 411 Design and Analysis of Algorithms
Algorithms Chapter 3.
Analysis of Non – Recursive Algorithms
Analysis of Non – Recursive Algorithms
Introduction to Analysis of Algorithms
Analysis of Algorithms
Introduction to the Design and Analysis of Algorithms
Analysis of Algorithms
COMP108 Algorithmic Foundations Algorithm efficiency
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Computational Complexity
The Growth of Functions
The Growth of Functions
Complexity Analysis.
Enough Mathematical Appetizers!
CS 3343: Analysis of Algorithms
Computation.
Sorting Algorithms Written by J.J. Shepherd.
O-notation (upper bound)
Algorithm design and Analysis
Asymptotic Notations Algorithms Lecture 9.
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Asymptotic Growth Rate
BIG-OH AND OTHER NOTATIONS IN ALGORITHM ANALYSIS
Fundamentals of Algorithms MCS - 2 Lecture # 9
Orders of Growth Rosen 5th ed., §2.2.
Copyright © Cengage Learning. All rights reserved.
CS 2210 Discrete Structures Algorithms and Complexity
Analysis of Algorithms
Chapter 3 Section 6.
CSE 373, Copyright S. Tanimoto, 2002 Asymptotic Analysis -
Chapter 3 Section 6.
Applied Combinatorics, 4th Ed. Alan Tucker
CMSC 203, Section 0401 Discrete Structures Fall 2004 Matt Gaston
More on Asymptotic Analysis and Performance Measurement
Enough Mathematical Appetizers!
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
More on Asymptotic Analysis + Performance Measurement
Advanced Algorithms Analysis and Design
Enough Mathematical Appetizers!
Algorithms Chapter 3 With Question/Answer Animations.
Copyright © Cengage Learning. All rights reserved.
이산수학(Discrete Mathematics)  함수의 증가 (Growth of Functions)
Exponential Functions and Their Graphs
Analysis of Algorithms
2.2 함수의 증가 (Growth of Functions)
이산수학(Discrete Mathematics)  함수의 증가 (Growth of Functions)
이산수학(Discrete Mathematics)  함수의 증가 (Growth of Functions)
Relations & Their Properties: Selected Exercises
Presentation transcript:

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

Copyright © Peter Cappello 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 ) = logk n, for all k  N & any constant log base ≥ 2. f( n ) = nq, for all q  Q+ f( n ) = kn < (k+1)n, for all k  Z+ - {1} f( n ) = n! Copyright © Peter Cappello

Copyright © Peter Cappello 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(). o() is different from O(). Copyright © Peter Cappello

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

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

Copyright © Peter Cappello Theorems You Can Use Thm 1: Let f(x) = anxn + an-1xn-1 + … + a1x + a0, where the ai are real. Then, f(x) is O( xn ). Let f1(x) be O (g1(x) ) & f2(x) be O( g2(x) ). Thm 2: (f1 + f2)(x) is O( max( g1(x), g2(x) ) ) Thm 3: (f1 f2)(x) is O( (g1g2)(x) ). Copyright © Peter Cappello

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

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

Copyright © Peter Cappello Exercise 20 b) b) f( n ) = ( 2n + n2 )( n3 + 3n ). Copyright © Peter Cappello

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

Copyright © Peter Cappello Exercise 20 c) c) f( n ) = ( nn + n2n + 5n )( n! + 5n ) Copyright © Peter Cappello

Copyright © Peter Cappello Exercise 20 c) Solution Defn: f( n ) is O( g( n ) ) when k c n > k f( n )  cg( n ). c) f( n ) = ( nn + n2n + 5n )( n! + 5n ) Using our theorems, f( n ) is O( ( nn + n2n )( n! ) ) In nn + n2n, which is the fastest growing term? Claim: n2n is O( nn ) : n is O( 2n ) n2n is O ( 2n2n ) is O ( 4n ) is O( nn ). Thus, f( n ) is O( nnn! ). Copyright © Peter Cappello 12

Copyright © Peter Cappello 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 ). Copyright © Peter Cappello

Copyright © Peter Cappello 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? Copyright © Peter Cappello

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 ) )? Copyright © Peter Cappello

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( ? ) Copyright © Peter Cappello

Copyright © Peter Cappello End 3.2 Copyright © Peter Cappello

Copyright © Peter Cappello 2011 40 Defn. f( n ) is Θ( g( n ) ) when f( n ) is O( g ( n ) ) and f( n ) is Ω( g( n ) ). Show that: if f1( x ) & f2( x ) are functions from Z+ to R and f1( x ) is Θ( g1( x ) ) and f2( x ) is Θ( g2( x ) ), then f1f2 ( x ) is Θ( g1g2( x ) ). Copyright © Peter Cappello 2011

Copyright © Peter Cappello 2011 40 Proof Assume f1( x ) & f2( x ) are functions from Z+ to R and f1( x ) is Θ( g1( x ) ) and f2( x ) is Θ( g2( x ) ). f1( x ) is O( g1( x ) ). (1. and defn of Θ) k1, C1, x > k1  f1( x )  C1g1( x ) (2.,Defn of O) f2( x ) is O( g2( x ) ). (1. and defn of Θ) k2, C2, x > k2  f2( x )  C2g2( x ) (4., Defn of O) x > max{ k1, k2 }  f1 f2( x )  C1C2g1g2( x ) f1 f2( x ) is O( g1g2( x ) ). (6., Defn of O) Copyright © Peter Cappello 2011

Copyright © Peter Cappello 2011 40 Proof continued f1( x ) is Ω( g1( x ) ). (Previous 1. & defn of Θ) k’1, C’1, x > k’1  f1( x ) ≥ C’1g1( x ) (1. & Defn of Ω) f2( x ) is Ω( g2( x ) ). (Previous 1. & defn of Θ) k’2, C’2, x > k’2  f2( x ) ≥ C’2g2( x ) (3. & Defn of Ω) x > max{ k’1, k’2 }  f1 f2( x ) ≥ C’1C’2g1g2( x ) f1 f2( x ) is Ω( g1g2( x ) ). (5. & Defn of Ω) f1 f2( x ) is Θ( g1g2( x ) ). (6., previous 7., defn Θ) Copyright © Peter Cappello 2011

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

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