Download presentation

Presentation is loading. Please wait.

Published byJovany Napper Modified over 6 years ago

1
Lecture 16 Complexity of Functions CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

2
CSCI 1900 Lecture 16 - 2 Lecture Introduction Reading –Rosen Sections 3.2, 3.3 Examine some functions occurring frequently in Computer Science Characterize the efficiency of an algorithm –Big-Oh –Big-Theta

3
CSCI 1900 Lecture 16 - 3 Functions in CS mod n function – f n ( m ) = m mod n –Example f 3 ( 7 ) = 1f 3 ( 8 ) = 2f 3 ( 111 ) = 0 Factorial function –f ( n ) = n!n! = n* (n-1)! –f ( 0 ) = 1f ( 1 ) = 1f ( 2 ) = 2f ( 3 ) = 6 f ( 10 )> 10 6 f ( 20 )>10 18 f (70) > 10 100

4
CSCI 1900 Lecture 16 - 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) = -3Nota Bene: -2 > -2.3

5
CSCI 1900 Lecture 16 - 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 ) = -2Nota Bene: -2 > -2.3

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

7
CSCI 1900 Lecture 16 - 7 Functions in CS (cont) Logarithm –f n ( x )=log n (x) the power to which n must be raised to yield x f 2 ( 4 ) = 2 f 2 ( 8 ) = 3 f 2 ( 2 ) = 1 f 2 ( 1 ) = 0 f 2 ( 1/2 ) = -1 f 2 ( 1/16 ) = -4 f 2 ( 0 ) = undefined

8
Growth of Functions CSCI 1900 Lecture 16 - 8

9
CSCI 1900 Lecture 16 - 9 Growth of Functions (cont) Previous example show that functions grow at different rates Specifically, let: f (x) = x, g(x) = 2 x –f (1) = 1, g(1) = 2 – f (10) = 10, but g (10) = 1024 Polynomial functions –n « n 2 « n 2.2 « n 3

10
CSCI 1900 Lecture 16 - 10 Growth of Functions (cont) All log functions grow at the same rate, regardless of the base –log 2 ( n ) grows at the same rate as log 100 ( n ) –We usually write log 2 as lg –Recall log n ( k ) = log m ( k ) / log m ( n ) Powers of the log function –(log n ) « (log n) 2 « (log n) 304 « n

11
CSCI 1900 Lecture 16 - 11 Growth of Functions (cont) log compared to polynomials –lg n « n « n lg( n ) « n ( lg( n ) ) 2 « n (lg( n )) 1670 « n 2 Polynomials compare to exponentials –n 2 « n 3 « n 2876 « 2 n Exponentials compared to factorial – 2 n « n!

12
CSCI 1900 Lecture 16 - 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 ) « n 2 « n 3 « … « 2 n « n!

13
CSCI 1900 Lecture 16 - 13 Big-Oh Notation f O( g ) if there exist two constants, c and n 0 such that | f (n) | c | g(n) | for all n n 0 –This means: f grows no faster than g does What is in O(n 3 )? –Anything that grows no faster than n 3 –f ( n ) = 10n 3 –f ( n ) = n 3 + 100n 2 – n –f ( n ) = n 2 –f ( n ) = n lg n –f ( n ) = lg n –f ( n ) = 2

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

15
CSCI 1900 Lecture 16 - 15 Big-Theta Notation f ( g ), if f and g have same order – f O( g ) and g O( f ) What is in ( n 3 ) –f ( n ) = n 3 + 100n 2 – n is in ( n 3 ) –f ( n ) = 2 is not in ( n 3 ) –f ( n ) = n 2 is not in ( n 3 ) –f ( n ) = lg n is not in ( n 3 ) –f ( n ) = n lg n is not in ( n 3 )

16
CSCI 1900 Lecture 16 - 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 ( n 2 ) –quicksort ( n lg n) –What is the fastest running-time for a comparison-based sorting algorithm?

17
CSCI 1900 Lecture 16 - 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”

18
CSCI 1900 Lecture 16 - 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

19
CSCI 1900 Lecture 16 - 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 ?

20
CSCI 1900 Lecture 16 - 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

Similar presentations

© 2021 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google