CSE115/ENGR160 Discrete Mathematics 03/08/11 - PowerPoint PPT Presentation

About This Presentation
Title:

CSE115/ENGR160 Discrete Mathematics 03/08/11

Description:

... n=10 Related to big-Omega and big-Theta notations in algorithm design * Big-O notation Let f ... is big-oh of g(x) * Big-O notation The constants C and k are ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 30
Provided by: CindaHee1
Category:

less

Transcript and Presenter's Notes

Title: CSE115/ENGR160 Discrete Mathematics 03/08/11


1
CSE115/ENGR160 Discrete Mathematics03/08/11
  • Ming-Hsuan Yang
  • UC Merced

2
3.2 Growth of Functions
  • Study number of operations used by algorithm
  • For example, given n elements
  • Study the number of comparisons used by the
    linear and binary search
  • Estimate the number of comparisons used by the
    bubble sort and insertion sort
  • Use big-O notation to study this irrespective of
    hardware

3
Big-O notation
  • Used extensively to estimate the number of
    operations in algorithm uses as its inputs grows
  • Determine whether it is practical to use a
    particular algorithm to solve a problem as the
    size of the input increases
  • Can compare with two algorithms and determine
    which is more efficient

4
Big-O notation
  • For instance, one algorithm uses 100n217n4
    operations and the other uses n3 operations
  • Can figure out which is more efficient with big-O
    notation
  • The first one is more efficient when n is large
    even though it uses more operations for smaller
    values of n, e.g., n10
  • Related to big-Omega and big-Theta notations in
    algorithm design

5
Big-O notation
  • Let f and g be functions from the set of integers
    or the set of real numbers to the set of real
    numbers, we say f(x) is O(g(x)) if there are
    constants C and k such that
  • f(x) C g(x)
  • whenever x gt k
  • Read as f(x) is big-oh of g(x)

6
Big-O notation
  • The constants C and k are called witnesses to the
    relationship f(x) is O(g(x))
  • Need only one pair of witnesses to this
    relationship
  • To show f(x) is O(g(x)), need only one pair of
    constants C and k, s.t. f(x) C g(x)
  • When there is one pair of witnesses, there are
    infinitely many pairs of witness
  • Let CltC and kltk, we have f(x) Cg(x)
    Cg(x) when xgtkgtk

7
Example
  • Show that f(x)x22x1 is O(x2)
  • We observe that we can estimate size of f(x) when
    xgt1 because xltx2 and 1ltx2 when xgt1,
  • 0 x22x1 x22x2x24x2
  • when xgt1. Thus, we can take C4 and k1 to
    show that f(x) is O(x2)
  • Alternatively, when xgt2, 2xx2 and 1x2
  • 0 x22x1 x2x2x23x2
  • so C3, and k2 are also witnesses to
    relation f(x) is O(x2)

8
Example
9
Example
  • Observe that O(x2) can be replaced by any
    function with larger values than x2,
  • e.g., f(x) is O(x3), f(x) is O(x2x7),
    O(x22x1)
  • In this example, f(x)x22x1, g(x)x2, we say
    both of these big-O relationships are of the same
    order
  • Sometimes written as f(x)O(g(x))
  • It is acceptable to write f(x) ? O(g(x)) as
    O(g(x)) represents the set of functions that are
    O(g(x))

10
Big-O notation
  • When f(x) is O(g(x)) and h(x) is a function that
    has larger absolute values than g(x) does for
    sufficient large value of x
  • It follows that f(x) is O(h(x))
  • f(x)Cg(x) if xgtk
  • and if h(x)gtg(x) for all xgtk, then
  • f(x)Ch(x) if xgtk
  • When big-O notation is used, f(x) is O(g(x)) is
    chosen to be as small as possible

11
Example
  • Show that 7x2 is O(x3)
  • When xgt7, 7x2ltx3, So let C1 and k7, we see 7x2
    is O(x3)
  • Alternatively, when xgt1, 7x2lt7x3 and so that C7
    and k1, we have the relationship 7x2 is O(x3)

12
Example
  • Show that n2 is not O(n)
  • To show that n2 is not O(n), we must show that no
    pair of constants C and k exist such that n2Cn
    when ngtk
  • When ngt0, we have nC
  • No matter what C and k are, the inequality
    nC cannot hold for all n with ngtk

13
Example
  • Previous example shows that 7x2 is O(x3). Is it
    also true that x3 is also O(7x2)
  • To show that, x37x2 is equivalent to x7C
    whenever xgtk
  • No C exists for which x7C for all xgtk
  • Thus x3 is not O(7x2)

14
Some important big-O results
  • Let f(x)anxnan-1xn-1a1xa0, where a0, a1, ,
    an-1, an are all real numbers, then f(x) is O(xn)
  • Using the triangle inequality, if xgt1

15
Example
  • Big-O notation of the sum of first n positive
    integers
  • 12n nnnn2, O(n2) with C1, k1
  • Alternatively, O(n(n1)/2)O(n2)
  • Big_O notation of n!
  • n!12 3 n n n n n nn, O(nn) with
    C1 and k1
  • log n!log nnn log n, log n! is O(nlog n) with
    C1, k1

16
Example
  • We know that nlt2n when n is a positive integer.
    Show that this implies n is O(2n) and use this to
    show that log n is O(n)
  • n is O(2n) by taking k1 and C1
  • Thus, log nltn (base 2), so log n is O(n)
  • If we have logarithms to a different base b than
    2, we still have logb n is O(n) as
  • logbn log n/log b lt n/log b when n is a
    positive integer. Take C1/log b and k1

17
Growth of functions
18
Growth of combinations of functions
  • Many algorithms are made up of two or more
    separate subprocedures
  • The number of steps is the sum of the number of
    steps of these subprocedures

19
Theorems
  • Theorem 2 Suppose f1(x) is O(g1(x)) and f2(x) is
    O(g2(x)),
  • (f1f2)(x) is O(max(g1(x), g2(x))
  • (f1f2)(x) is O(g1(x)g2(x))
  • (f1f2)(x)f1(x)f2(x)C1g1(x)C2g2(x)
  • C1C2(g1g2)(x)C(g1g2)(x) (C C1C2,
    kmax(k1,k2))
  • Corollary f1(x) and f2(x) are both O(g(x)), then
    (f1f2)(x) is O(g(x))

20
Example
  • Big-O notation of f(n)3n log(n!)(n23)logn
    where n is a positive integer
  • We know log(n!) is O(nlog n), so the first part
    is O(n2 log n)
  • As n23lt2n2 when ngt2, it follows that n23 is
    O(n2), and the second part is O(n2 log n)
  • So f(n) is O(n2 log n)

21
Example
  • Big-O notation of f(x)x1 log(x21) 3 x2
  • Note x1 is O(x) and x21 2x2 when xgt1
  • So, log x21 log(2x2)log 2 log x2log 2 2
    log x
  • 3 log x if x gt2
  • Thus, log x21 is O(log x)
  • The first part of f(x) is O(x log x)
  • Also, 3x2 is O(x2)
  • So, f(x) is O(max(x log x, x2))O(x2) as x log x
    x2 for x gt1

22
Big-Omega
  • Big-O notation does not provide a lower bound for
    the size of f(x) for large x
  • Let f and g be functions from the set of integers
    or the set of real numbers to the set of real
    numbers. We say f(x) is ???(g(x)) if there are
    positive constants C and k s.t.
  • f(x)Cg(x) when xgtk
  • Read as f(x) is big-Omega of g(x)

23
Example
  • f(x)8x35x27 is ??(g(x)) where g(x)x3
  • It is easy to see as f(x) 8x35x278x3 for all
    positive numbers x
  • This is equivalent to say g(x)x3 is O(8x35x27)

24
Big-Theta notation
  • Want a reference function g(x) s.t. f(x) is
    O(g(x)) and f(x) is ??(g(x))
  • Let f and g be functions from the set of integers
    or the set of real numbers to the set of real
    numbers. We say that f(x) is ??(g(x)) if f(x) is
    O(g(x)) and f(x) is ??(g(x))
  • When f(x) is ??(g(x)) , we say f is big-Theta of
    g(x), and we also say f(x) is of order g(x)

25
Big Theta-notation
  • When f(x) is ??(g(x)), g(x) is ??(f(x))
  • f(x) is ??(g(x)) if and only if f(x) is O(g(x))
    and g(x) is O(f(x))

26
Example
  • Let f(n)12n. We know that f(n) is O(n2), to
    show that f(x) is of order n2, we need to find C
    and k s.t. f(n)gtCn2 for large n
  • f(n) is O(n2) and ??(n2), thus f(n) is ??(n2)

27
Big-Theta notation
  • We can show that f(x) is ??(g(x)) if we can find
    positive real numbers C1 and C2 and a positive
    number k, s.t.
  • C1g(x)f(x)C2g(x)
  • when xgtk
  • This shows f(x) is O(g(x)) and f(x) is ??(g(x))

28
Example
  • Show that 3x28x log x is ??(x2)
  • As 0 8x log x8x2, it follows that
  • 3x28x logx 11x2 for xgt1
  • Consequently 3x28x logx is O(x2)
  • Clearly 3x28x logx is ??(x2)
  • Consequently 3x28x logx is ??(x2)

29
Polynomial
  • One useful fact is that the leading term of a
    polynomial determines its order
  • E.g., f(x)3x5x417x32 is of order x5
  • Let f(x)anxnan-1xn-1a1xa0, where a0, a1, ,
    an-1, an are all real numbers, then f(x) is of
    order xn
Write a Comment
User Comments (0)
About PowerShow.com