1 / 20

Lecture 16 Complexity of Functions

Lecture 16 Complexity of Functions. CSCI – 1900 Mathematics for Computer Science Spring 2014 Bill Pine. Lecture Introduction. Reading Kolman Sections 5.2, 5.3 Examine some functions occurring frequently in Computer Science Characterize the efficiency of an algorithm Big-Oh Big-Theta.

lang
Download Presentation

Lecture 16 Complexity of Functions

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. Lecture 16Complexity of Functions CSCI – 1900 Mathematics for Computer Science Spring 2014 Bill Pine

  2. Lecture Introduction • Reading • Kolman Sections 5.2, 5.3 • Examine some functions occurring frequently in Computer Science • Characterize the efficiency of an algorithm • Big-Oh • Big-Theta CSCI 1900

  3. Functions in CS • mod n function • fn( m ) = m mod n • Example • f3( 7 ) = 1 f3( 8 ) = 2 f3( 111 ) = 0 • Factorial function • f ( n ) = n! n! = n* (n-1)! • f ( 0 ) = 1 f ( 1 ) = 1 f ( 2 ) = 2 f ( 3 ) = 6f ( 10 )> 106f ( 20 )>1018f (70) > 10100 CSCI 1900

  4. Functions in CS (cont) • Floor function • f ( x ) = largest integer ≤ x • f (2.1) = 2 • f (2.9) = 2 • f (2.999999) = 2 • f (3) = 3 • f (-2.3) = -3 Nota Bene: -2 > -2.3 CSCI 1900

  5. Functions in CS (cont) • Ceiling function • f( x ) = smallest integer ≥ x • f( 2.000001 ) = 3 • f( 2.9 ) = 3 • f( 2.999999 ) = 3 • f( 3 ) = 3 • f( 3.000001 ) = 4 • f( -2.3 ) = -2 Nota Bene: -2 > -2.3 CSCI 1900

  6. Functions in CS (cont) • Exponential • f ( x )=2x • f ( 1 )=2 • f ( 2 )=4 • f ( 2.2 )=4.4957… • f ( 0 )=1 • f ( -1 )=1/2 • f ( -2 )=1/4 CSCI 1900

  7. Functions in CS (cont) • Logarithm • fn( x )=logn(x) the power to which n must be raised to yield x • f2( 4 ) = 2 • f2( 8 ) = 3 • f2( 2 ) = 1 • f2( 1 ) = 0 • f2( 1/2 ) = -1 • f2( 1/16 ) = -4 • f2( 0 ) = undefined CSCI 1900

  8. Growth of Functions CSCI 1900

  9. Growth of Functions (cont) • Previous example show that functions grow at different rates • Specifically, let: f (x) = x, g(x) = 2x • f (1) = 1, g(1) = 2 • f (10) = 10, but g (10) = 1024 • Polynomial functions • n« n2 « n2.2 « n3 CSCI 1900

  10. Growth of Functions (cont) • All log functions grow at the same rate, regardless of the base • log2 ( n ) grows at the same rate as log100 ( n ) • We usually write log2 as lg • Recall logn( k ) = logm( k ) / logm( n ) • Powers of the log function • (logn ) « (log n)2 « (log n)304 « n CSCI 1900

  11. Growth of Functions (cont) • log compared to polynomials • lgn« n« nlg( n ) « n ( lg( n ) )2 « n (lg( n))1670 « n2 • Polynomials compare to exponentials • n2 « n3 « n2876 « 2n • Exponentials compared to factorial • 2n« n! CSCI 1900

  12. Growth of Functions (cont) • In general, the classes of functions, in order of increasing running time are • constant « log( n ) « n « n log( n )« n2 « n3 « … « 2n« n! CSCI 1900

  13. Big-Oh Notation • f O( g ) if there exist two constants, c and n0 such that |f (n) |  c |g(n) | for all n  n0 • This means: f grows no faster than g does • What is in O(n3)? • Anything that grows no faster than n3 • f ( n ) = 10n3 • f ( n ) = n3 + 100n2 –n • f ( n ) = n2 • f ( n ) = n lg n • f ( n ) = lg n • f ( n ) = 2 CSCI 1900

  14. Big-Oh Example • Show that n3 + 100n2 +20  O( n3 ) • Need to find a c and n0 such that c n3 > n3 + 100n2 –n for all n > n0 • Two important consequences of f O( g ) • g serves as an upper bound on f • We ignore the behavior for small values of n CSCI 1900

  15. Big-Theta Notation • f  ( g ), if f and g have same order • f O( g ) and g  O( f ) • What is in ( n3 ) • f ( n ) = n3 + 100n2 – nis in ( n3 ) • f ( n ) = 2 is not in ( n3 ) • f ( n ) = n2 is not in ( n3 ) • f ( n ) = lg nis not in ( n3 ) • f ( n ) = n lg nis not in ( n3 ) CSCI 1900

  16. Running Time • The -class of a function that describes the number of steps performed by an algorithm is called the running time of the algorithm • This allows us to compare algorithms for a specific task • For example: • bubble sort ( n2 ) • quicksort  ( n lg n) • What is the fastest running-time for a comparison-based sorting algorithm? CSCI 1900

  17. Determining Running Time • To compare two algorithm’s running times • Select an operation that is central to the algorithm • For searching, we might choose comparison • For sorting, we might choose comparison or perhaps data saving • Examine the algorithm to determine how the count of the key operation depends upon “input size” • To evaluate a single algorithm • Count the total number of operations • Examine the algorithm to determine how the count of the key operation depends upon “input size” CSCI 1900

  18. Running Time Example • Consider the following function function meanOf( n ) sum  0 for i = 1 thru n sum  sum + i mean = sum / n return mean CSCI 1900

  19. Running Time Example (cont) • What is the input size? • Identify and count the characterizing operation • Number of pre-loop operations • Number of operations in the loop • Number of post-loop operations • What is the running time? • Is there a more efficient way to determine meanOf? CSCI 1900

  20. Key Concepts Summary • Examined some functions “useful” in Computer Science • Characterize the efficiency of an algorithm • Big-Oh • Big-Theta • Reading for next time • Kolman Sections 7.1, 7.2 CSCI 1900

More Related