CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
Growth of Functions CSCI 1900 Lecture
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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
CSCI 1900 Lecture 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!
CSCI 1900 Lecture 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!
CSCI 1900 Lecture 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 n 2 – n –f ( n ) = n 2 –f ( n ) = n lg n –f ( n ) = lg n –f ( n ) = 2
CSCI 1900 Lecture Big-Oh Example Show that n n O( n 3 ) Need to find a c and n 0 such that c n 3 > n n 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
CSCI 1900 Lecture 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 n 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 )
CSCI 1900 Lecture 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?
CSCI 1900 Lecture 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 Lecture 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 Lecture 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 Lecture 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