1 / 20

Section 2.2

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

chiku
Download Presentation

Section 2.2

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. Section 2.2 The Growth of Functions

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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:ZR and g:ZR OR f:RR and g:RR • 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

  7. 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

  8. 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)

  9. 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

  10. 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

  11. 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

  12. 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)

  13. 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

  14. 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)|)

  15. 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))

  16. 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- 

  17. Big-O and Big- • The definition of big-  is similar to the definition of big-O: Given f:ZR and g:ZR or f:RR and g:RR, 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))

  18. 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))

  19. 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-

  20. Section 2.2 The Growth of Functions -ends-

More Related