200 likes | 276 Views
Section 2.2. The Growth of Functions. Algorithm Analysis & Functions. Algorithm analysis is the study of the practicality of programs Although many solutions to a problem may exist, not all algorithms can be implemented to execute in a reasonable amount of time
E N D
Section 2.2 The Growth of Functions
Algorithm Analysis & Functions • Algorithm analysis is the study of the practicality of programs • Although many solutions to a problem may exist, not all algorithms can be implemented to execute in a reasonable amount of time • The number of times each instruction in an algorithm will execute can be expressed as a function of the size of the program’s data set
Growth of Functions • Suppose n is the number of inputs to two different sorting algorithms • Having counted the number of times each instruction of each algorithm will execute relative to n, we come up with two functions to describe how long each algorithm will take to complete the sort
Growth of Functions • For this example, suppose the two functions we derive from the algorithms are: • f(n) = n2 + 7n + 3 • g(n) = 200n • If n is a very small number (something less than 200), the first algorithm will perform less instructions to sort the list of n inputs than the second
Growth of Functions • But as n grows, the first function will clearly grow faster than the second, since the second will always multiply n by 200, while the first will multiply n by itself • In algorithm analysis, we are usually interested in how algorithms will perform relative to one another given a very large data set
Big-O Notation • A mathematical way to describe the growth of functions is called big-O notation, defined as follows: • Let f and g be functions with f:ZR and g:ZR OR f:RR and g:RR • We say that f(x) is O(g(x)) if there are constants C and k such that |f(x)| <= C|g(x)| whenever x>k
Big-O Notation • To show that f(x) is O(g(x)) requires that we find one pair of constants C,k such that |f(x)|<=C|g(x)| • If one such pair exists, there are infinitely many pairs
Example Show that x4 + 9x3 + 4x + 7 is O(x4) 0 <= x4 + 9x3 + 4x + 7 <= x4 + 9x4 + 4x4 + 7x4 = 21x4 when x > 1 The pair C=21, k=1 satisfies the definition of big-O and f(x) is O(x4)
Example Show that x4 + 9x3 + 4x + 7 is O(x4) Another approach: for x > 9, 9x3 < x4 So, if x > 9, 0 <= x4 + 9x3 + 4x + 7 <= x4 + x4 + 4x + 7 = 2x4 + 4x + 7 Since when x > 1, 4x< x4 and 1 < 9, and since 7 < 9, we can safely assert: if x > 9, 0 <= x4 + 9x3 + 4x + 7 <= x4 + x4 + x4 + x4 = 4x4 So k=9, C=4 is another pair that satisfies the definition
Functions of the Same Order • In the example we just saw, we have two functions: f(x) = x4 + 9x3 + 4x + 7 where O(f(x)) = x4 g(x) = x4 • In this instance, f(x) is O(g(x)) and g(x) is O(f(x)) (because x4 <= x4 + 9x3 + 4x + 7 for all non-negative real numbers x) • Thus f(x) and g(x) are of the same order
Choose the Smallest Function • If f(x) is O(g(x)), and h(x) is a function with larger absolute values than g(x), we can show that f(x) is also O(h(x)) • In general, however, we choose function g to be as small as possible in big-O notation
Using Polynomials to Estimate the Growth of Functions • When a function is expressed as a polynomial, the leading term dominates the growth of that function • We can assert (and prove) that a function of degree n or less is O(xn)
Growth of Combinations of Functions • Many algorithms are made up of 2 or more subprocedures • To give big-O estimate for the number of steps used by such an algorithm, need to find big-O estimate for each subprocedure and then combine them
Growth of Combinations of Functions • It is often necessary to estimate the growth of the sum and product of two functions • If two functions, f1(x) and f2(x) are both g(x), then (f1 + f2)(x) is O(g(x)) • On the other hand, is f1(x) is O(g1(x)) and f2(x) is O(g2(x)), (f1 + f2)(x) is O(max(|g1(x)|, |g2(x)|)
Growth of Combinations of Functions • A big-O estimate for the product of 2 functions f1(x) and f2(x) which are O(g1(x)) and O(g2(x)), respectively, can be derived as follows: (f1f2)(x) = |f1(x)||f2(x)| <= C1|g1(x)|C2|g2(x)| <= C1C2|(g1g2)(x)| <= C|(g1g2)(x)| where C = C1C2 and x > max(k1,k2) • So (f1f2)(x) is O(g1(x) g2(x))
Big- and Big- • Big-O notation provides an upper bound, in terms of g(x) for describing the growth of a function f(x) • To describe lower bound, we use big- • Where both an upper and a lower bound are needed, we use big-
Big-O and Big- • The definition of big- is similar to the definition of big-O: Given f:ZR and g:ZR or f:RR and g:RR, we say that f(x) is g(x) if there are positive constants C and k such that |f(x)| >= C|g(x)| whenever x > k • Note that f(x) is g(x) if and only if g(x) is O(f(x))
Big- • Big- notation provides both upper and lower bounds on the size of a function • If f(x) is (g(x)), then f(x) is both O(g(x)) and (g(x)) • If f(x) is (g(x)), we say that f(x) is of order g(x) • If f(x) is (g(x)), then g(x) is (f(x))
Big- • We can show that f(x) is (g(x)) if we can find positive real numbers C1 and C2 and positive real number k such that: C1|g(x)| <= |f(x)| <= C2|g(x)| whenever x >= k • The leading term of a polynomial determines its order • Many people use the big-O notation when they really mean big-
Section 2.2 The Growth of Functions -ends-