Asymptotic Notations for Time Efficiency Analysis

download Asymptotic Notations for Time Efficiency Analysis

of 33

Transcript of Asymptotic Notations for Time Efficiency Analysis

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    1/33

    Tugas DAA

    - INDIVIDU

    jawab exercise (1) dan (2) di slide ke-31 dan 32 ini ppt

    nya kirim ke email bu astuti yg ini [email protected]

    deadline sebelum UAS

    -KELOMPOK

    tambahkan pertanyaan ama pembahasaan dr ygtemen2/ibunya tanyain pas presentasi, isi nim nama

    penanya trus

    buat komplekitas algoritma yg lengkap dr presentasi

    yg kalian bawain, kirim juga ke email deadline

    sebelum UAS

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    2/33

    Asymptotic Notations forTime Efficiency Analysis

    Design and Analysis of Algorithms

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    3/33

    Contents

    Asymptotic Notations:

    O(big oh)

    (big omega)

    (big theta)

    Basic Efficiency Classes

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    4/33

    In the following

    discussiont(n) & g(n): any nonnegative functions

    defined on the set of natural numbers

    t(n)an algorithms running timeUsually indicated by its basic operation count

    C(n)

    g(n)some simple function to compare

    the count with

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    5/33

    O(g(n)): Informally

    O(g(n))is a set of all functions with a

    smalleror same order of growth as g(n)

    Examples:

    n O(n2); 100n + 5 O(n2)

    n (n-1) O(n2)

    n3O(n2); 0.0001 n3O(n2); n4+n+1 O(n2)

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    6/33

    (g(n)): Informally

    (g(n))is a set of all functions with a

    largeror same order of growth as g(n)

    Examples:

    n3(n2)

    n (n-1) (n2)

    100n + 5 (n2)

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    7/33

    (g(n)): Informally

    (g(n))is a set of all functions with a

    same order of growth as g(n)

    Examples:

    an2+bn+c; a>0 (n2); n2+sin n (n2)

    n (n-1) (n2); n2+log n (n2)

    100n + 5 (n2); n3(n2)

    =

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    8/33

    O-notation: Formally

    DEF1: A function t(n) is said to be in

    O(g(n)), denoted t(n) O(g(n)), if t(n) is

    bounded aboveby some constant multiple

    of g(n) for all large n

    i.e. there exist some positive constant c

    and some nonnegative integer n0, suchthat

    t(n) cg(n) for all n n0

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    9/33

    t(n) O(g(n)): Illustration

    n

    n0

    doesn't

    matter

    t(n)

    cg(n)

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    10/33

    Proving Example: 100n + 5 O(n2)

    Remember DEF1: find c and n0, such that

    t(n) cg(n) for all n n0

    100n + 5 100n + n (for all n 5) = 101n 101n2c=101, n0=5

    100n + 5 100n + 5n (for all n 1) = 105n 105n2c=105, n0=1

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    11/33

    -notation: Formally

    DEF2: A function t(n) is said to be in

    (g(n)), denoted t(n) (g(n)), if t(n) is

    bounded belowby some constant multiple

    of g(n) for all large n

    i.e. there exist some positive constant c

    and some nonnegative integer n0, suchthat

    t(n) cg(n) for all n n0

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    12/33

    t(n) (g(n)): Illustration

    n

    n0

    doesn't

    matter

    t(n)

    cg(n)

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    13/33

    Proving Example: n3

    (n2)

    Remember DEF2: find c and n0, such that

    t(n) cg(n) for all n n0

    n3 n2 (for all n 0)c=1, n0=0

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    14/33

    -notation: Formally

    DEF3: A function t(n) is said to be in

    (g(n)), denoted t(n) (g(n)), if t(n) is

    bounded both aboveand belowby some

    constant multiple of g(n) for all large n

    i.e there exist some positive constant c1

    and c2and some nonnegative integer n0,such that

    c2g(n) t(n) c1g(n) for all n n0

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    15/33

    t(n) (g(n)): Illustration

    nn0

    doesn't

    matter

    t(n)

    c1g(n)

    c2g(n)

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    16/33

    Proving Example: n(n-1) (n2)

    Remember DEF3: find c1and c2and some

    nonnegative integer n0, such that

    c2g(n) t(n) c1g(n) for all n n0

    The upper bound: n(n-1) = n2 n n2 (for all n 0)

    The lower bound: n(n-1) = n2 n n2- n n (for all n 2) = n2

    c1= , c

    2= , n

    0= 2

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    17/33

    Basic Efficiency Classes

    The time efficiency of a large number of

    algorithms fall into only few classes

    1, log n, n, n log n, n2, n3, 2n, n!

    Multiplicative constants are ignoredit is

    possible that an algorithm in worse

    efficiency class running faster than

    algorithm in better class

    Exp: Alg A: n3, alg B: 106n2; unless n > 106,

    alg B runs faster than alg A

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    18/33

    Class: 1

    Name: constant

    Comment:

    Short of best-case efficiency

    Very few reasonable examples can be given

    algs running time typically goes to infinitywhen its input size grows infinitely large

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    19/33

    Class: log n

    Name: logarithmic

    Comment:

    Typically, a result of cutting a problems sizeby a constant factor on each iteration of the

    algorithm

    Logarithmic alg cannot take into account all its

    input or even a fixed fraction of it: anyalgorithm that does so will have at least linear

    running time

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    20/33

    Class: n

    Name: linear

    Comment:

    Algorithms that scan a list of size n (e.g.

    sequential search) belong to this class

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    21/33

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    22/33

    Class: n2

    Name: quadratic

    Comment:

    Typically, characterizes efficiency of

    algorithms with two embedded loops

    Standard examples: elementary sorting

    algorithms and certain operations on n-by-n

    matrices

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    23/33

    Class: n3

    Name: cubic

    Comment:

    Typically, characterizes efficiency of

    algorithms with three embedded loops

    Several nontrivial algorithms from linear

    algebra fall into this class

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    24/33

    Class: 2n

    Name: exponential

    Comment:

    Typical for algorithms that generate all

    subsets of an n-element set

    The term exponential is often used to includethis and faster orders of growth as well

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    25/33

    Class: n!

    Name: factorial

    Comment:

    Typical for algorithms that generate all

    permutations of an n-element set

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    26/33

    Useful Property

    Theorem:

    If t1(n) O(g1(n)) and t2(n) O(g2(n)),

    then t1

    (n) + t2

    (n) O(max{g1

    (n), g2

    (n)})

    The analogous assertions are true for the

    and

    notations as well

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    27/33

    Example

    Alg to check whether an array has

    identical elements:

    Sort the array

    Scan the sorted array to check its

    consecutive elements for equality

    (1) = n(n-1) comparisonO(n2)

    (2) = n-1 comparisonO(n)The efficiency of (1)+(2) = O(max{n2,n})

    = O(n2)

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    28/33

    Using Limits for Comparing

    OoG

    A convenient method for comparing order ofgrowth of two specific functions

    Three principal cases:

    The first two cases t(n) O(g(n)); the last twocasest(n) (g(n)); the second case alone

    t(n) (g(n))

    gOolargahasthat t(implies

    gaOosamthehasthat t(impliesc

    Osmaahasthat t(implies0

    )(

    )(limng

    nt

    n

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    29/33

    Limit-based: why

    convenient?It can take advantage of the powerful

    calculus techniques developed for

    computing limits, such as

    LHopitals rule

    Stirlings formula

    ('

    ('lim

    )(

    )(lim

    g

    t

    ng

    nt

    nn

    ovlargfor2! ne

    n

    nn

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    30/33

    Example (1)

    Compare OoG of n(n-1) and n2.

    The limit = cn(n-1) (n2 )

    Compare OoG of log2n and n

    The limit = 0log2n has smaller order of n

    1li2

    1lim2

    1)1(lim 1

    2

    2

    2

    21

    n

    nnn n

    nn

    n

    nn

    lilo2)(loglim)'(

    )'(loglimloglim 22

    1

    1222

    eennnn

    nnnnnn

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    31/33

    Example (2)

    Compare OoG of n! and 2n.

    The limit = n! (2n )

    n

    nnn

    n

    nn

    n

    e

    n

    nn

    n en

    ennnn 22li

    22lim

    2

    2lim2

    !lim

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    32/33

    Exercises (1)

    True or false:

    n(n+1)/2 O(n3)n(n+1)/2 O(n2)

    n(n+1)/2 (n3)n(n+1)/2 (n)

    Indicate the class (g(n)):

    (n2

    +1)10

    (10n2+7n+3)

    2n log (n+2)2+(n+2)2log (n/2)

  • 8/10/2019 Asymptotic Notations for Time Efficiency Analysis

    33/33

    Exercises (2)

    1. Prove that every polynomial

    p(n) = aknk+ ak-1n

    k-1+ + a0with ak> 0belongs to (nk)

    Prove that exponential functions anhavedifferent orders of growth for different

    values of base a > 0