2. Asymtotic Notation

download 2. Asymtotic Notation

of 38

Transcript of 2. Asymtotic Notation

  • 8/17/2019 2. Asymtotic Notation

    1/38

    Algorithms

  • 8/17/2019 2. Asymtotic Notation

    2/38

    REVIEW: RUNNING TIME

    Number of primitive opertio!s thatare executed:

    Except for time of executing afunction call most statementsroughly require the same amount of

    time

    We can be more exact if need be.

  • 8/17/2019 2. Asymtotic Notation

    3/38

    AN E"AMPLE: INSERTIONSORTI!p#t: Arr$ A o% si&e !O#tp#t: Sorte' rr$ i! i!(resi!g or'er

    I!sertio!Sort)A* !+ ,%or i - . to ! ,/e$ - A0i1

     2 - i 3 456hile )2 7 8+ !' )A021 7 /e$+ ,A02941 - A021 2 - 2 3 4

    A02941 - /e$

  • 8/17/2019 2. Asymtotic Notation

    4/38

    INSERTION SORTStatement Eort

    I!sertio!Sort)A* !+ ,%or 2 ; . to ! , c1n/e$ ; A021 c2n!1"i ; 2 3 45 c#n!1"

    6hile )i 7 8+ !' )A0i1 7 /e$+ , c$ % &A0i941 ; A0i1 c' % &!n!1""

    i ; i 3 4 c( % &!n!1""

    )A0i941 ; /e$ c*n!1"

    ) % &+ t2 , t# , - , tn here t & is number of times youshifting the element in right side to insert ne number.

  • 8/17/2019 2. Asymtotic Notation

    5/38

    ANALY

  • 8/17/2019 2. Asymtotic Notation

    6/38

    ANALY?est (se @The best case occurs if the array is alreadysorted.

    Therefore,t j = 1 for j = 2, 3, ...,n and the best-case running time

    can be computed using above equation as follows:This running time can be expressed as  An + B for

    constants Aand B that depend on the statement costsci.

     Therefore, T(n) it is a linear function ofn.

  • 8/17/2019 2. Asymtotic Notation

    7/38

    ANALYWorst (se@The worst case occurs if the array is reversesorted.

    Therefore,t j = 1 for j = 2, 3, ...,n and the worst-case running time

    can be computed using above equation.This running time can be expressed as An 2 + Bn + Cforconstants A , B and C that depend on the statement

    costsci.

     Therefore, T(n) it is a quadratic function ofn.

  • 8/17/2019 2. Asymtotic Notation

    8/38

     ASYMPTOTIC NOTATIONS

  • 8/17/2019 2. Asymtotic Notation

    9/38

     ASYMPTOTIC PERORMANCE

    Suppose e are considering t6o lgorithms* A !' B*for sol/ing a gi/en problem.

    0unning times of each of the algorithms are %n" and %n"

    respecti/ely3 here n is a measure of the problem si5e.

    Whi(h is =etter Algorithm: 6f e 7no the problem si5e ! in ad/ance and then if

    TA)!+ T?)!+ conclude that algorithm A is better thanlgorithm ?D

    U!%ort#!tel$* 6e #s#ll$ 'o!t /!o6 the pro=lemsi&e =e%oreh!'D

  • 8/17/2019 2. Asymtotic Notation

    10/38

    ASYMPTOTIC PERORMANCE

     Asymptotic performance: 8o does anlgorithm beha/e as the problem input"si5e gets /ery large9

    0unning time

    emory;storage requirements

  • 8/17/2019 2. Asymtotic Notation

    11/38

    ALGORITFMS WITF SAMECOMPLE"ITY 

    •0epresent

  • 8/17/2019 2. Asymtotic Notation

    12/38

    ASYMPTOTIC NOTATION

    Big-Oh O(.)Big-Oh O(.)

    Big-OmegaΩ(.)

    Big-ThetaΘ(.)

    Little-Oh o(.)

    Little-Omegaω(.)

    > Fortunately, there are asymptoticnotations which allow us to characterizethe main factors aecting analgorithm’s running time without goinginto detail

    > A good notation for large inputs.

    > The notations describe dierent rate-of-

    growth relations between the deningfunction and the dened set offunctions.

  • 8/17/2019 2. Asymtotic Notation

    13/38

    ASYMPTOTIC UPPER ?OUNH3?IG OF

     

  • 8/17/2019 2. Asymtotic Notation

    14/38

    ASYMPTOTIC UPPER ?OUNH3?IG OF

     ?et fn" and gn" represents complexities ofto algorithms.

    fn" is said to be in the family of gn" i 

    8 ≤ %)!+ ≤ ( Dg)!+ %or ll ! J !8

  • 8/17/2019 2. Asymtotic Notation

    15/38

    ?IG O NOTATION

    Emple 6f fn" + 2n2 

    gn" is @n# "

    K#e: Sho6 tht %)!+ is ?ig Oh g)!+

    A!s: to pro/e this e ha/e to sho that ) ≤ fn" ≤ c gn"

    for all n A n)

    6f e can Bnd positi/e constants ( !' !8 thene can say that %)!+ is =ig oh g)!+

  • 8/17/2019 2. Asymtotic Notation

    16/38

    ?IG O NOTATION

    Emple 4 6f fn" + 2n2 , gn" is O(n3) 

    fn" ≤ c gn"

    2n2

     ≤ cn3⇒

     2 ≤ cn⇒

     c = 1 and n0

    = 2 So, for c=1 and for all n A 2, f(n) is big oh g(n)

    There is no unique set of values forno and cin proving

    the asymptotic bounds but for whatever value of no youtake, it should satisfy for all n > n

    o

    Or For c=2 and n

    o =4 also, f(n) is big oh g(n)

  • 8/17/2019 2. Asymtotic Notation

    17/38

    ?IG O NOTATION

    Emple . 6f fn" + 8n+128, gn" is O(n2) 

     %o pro/e fn" is big oh gn" e ha/e to pro/ethat

    %)!+ ≤ ( g)!+ %or ll ! J !8 !' (o!st!t (Lets t/e (;4

    Since (n+8)>0 for all values of! J 8 , so for!8;4  Above rule is saisfied! So, for c=1 and for all n J 1", f(n) is big oh g(n)

  • 8/17/2019 2. Asymtotic Notation

    18/38

    ?IG3O NOTATION

    When determining the order of a functionfx"3 alays try to pic7 the smallest gx"possible.

    6f fx" is Οgx"" and gx" is Οfx""3 then fx"and gx" are of the same order.

    6n practice3 all big!@ results are obtained forfunctions that are positi/e for all /alues ofx.

  • 8/17/2019 2. Asymtotic Notation

    19/38

    ASYMPTOTIC LOWER ?OUNH: 3NOTATION

    ig @ is asymptotic upper bound

    Ω!notation is asymptotic loer bound

  • 8/17/2019 2. Asymtotic Notation

    20/38

     3NOTATION

    family of functions e say `̀ f(n) is omega g(n),'' iff

    C fn" : there exist positi/e constants c and no such

    that

    )≤ c gn"

    ≤ fn" for all nA n) D

  • 8/17/2019 2. Asymtotic Notation

    21/38

     3NOTATION

    Example:

    =or any to functions fn" and gn" e ha/e

    fn" + 5n2 -64n+256 and gn" + Ωn2"

    so* %)!+ ; )g)!+ i% !' o!l$ i% (D g)!+ ≤ %)!+

    Sol: in order to sho this e need to Bnd aninteger n) and a constant c!) such that

    for all integers nA n)  fn" A cn2

  • 8/17/2019 2. Asymtotic Notation

    22/38

     3NOTATION

    Example:

    =or any to functions fn" and gn" eha/e

    fn" + 5n2 -64n+256 and gn" + Ωn2" suppose e (hoose (;43 then

    So here (n-8)2 Is always positive for all n ≥ 0 so n0= " 

    Hence for c=1 and n0 =0 ``f(n) is omega g(n)!!

  • 8/17/2019 2. Asymtotic Notation

    23/38

     3 NOTATION HEINITION

     Θ gn" " denotes a family of functions.

    `̀ f(n) is Theta g(n),'' if e can Bndconstants c13 c23 and n) such that

    ) ≤ c1 gn" ≤  fn" ≤ c2 gn"for all n A n)

  • 8/17/2019 2. Asymtotic Notation

    24/38

    =or any to functions fn" and gn" eha/e

    fn" + Θgn""if and only if

    fn" + @gn"" and fn" + Ωgn""

  • 8/17/2019 2. Asymtotic Notation

    25/38

    ASYMPTOTIC TIGFT ?OUNH

     %his is said to asymptotictight bound.

    =or reasonably large

    /alues of n3 the functionfn" is ithin the range ofconstant multiples ofgn"

      fn" is bounded belo asell as abo/e.

  • 8/17/2019 2. Asymtotic Notation

    26/38

    RELATIONS ?ETWEENNOTAIONS

  • 8/17/2019 2. Asymtotic Notation

    27/38

     3 NOTATIONEmple6f fn" + *n2  '$n ,#

     gn" is Θ n2 "

    K#e: Sho6 tht %)!+ is Thet g)!+D

    ns: 6f fn" is also ha/ing the same running timeas gn" then pro/e folloing rule: ) ≤ c1 gn" ≤ 

    fn" ≤ c2 gn"

    for all n A n)

    No Bnd c13 c2 and n) 

    We want c c and n such

  • 8/17/2019 2. Asymtotic Notation

    28/38

    7n2  – 54n +3We want c 1, c 2  and n0  such

    that

    c 1n2 ≤   7n2  – 54n +3 ≤   c 2 n

    2  

    for all n ≥ n0  

    Select c 1 " # and #" c 2  

    Say c 1 = 5 and c 2  = 9

    o !et 5n2 ≤   7n2  – 54n +3,

    n "ust #e #$!!er than 27 %7n2  – 54n +3 ≤   9n2   $s true for all n%

    hus c 1 = 5, c 2  = 9 and n0  = 2&  

  • 8/17/2019 2. Asymtotic Notation

    29/38

    TAE ANOTFER E"AMPLE

    6f fn" + *n#  '$n2 , 1)n , #

  • 8/17/2019 2. Asymtotic Notation

    30/38

    LITTLE O3NOTATION

     ogn" +C fn":

    for any positi/e constant c F )there exists a constant n) F ) such

    that) G fn" H cgn" for all n ≥ n) D

    ?imit f;g → )

  • 8/17/2019 2. Asymtotic Notation

    31/38

    LITTLE ω3NOTATION

     ωgn" +C fn":

    for any positi/e constant c F )there exists a constant n) F ) such

    that) G cgn" H fn" for all n ≥ n) D

    ?imit g;f → )

  • 8/17/2019 2. Asymtotic Notation

    32/38

    NAMES O MOST COMMON?IG OF UNCTIONS

  • 8/17/2019 2. Asymtotic Notation

    33/38

  • 8/17/2019 2. Asymtotic Notation

    34/38

    TFEOREMR4:R4: 6f dn" is @fn""3 then adn" is @fn"3 a

    F )

    R.:R.: 6f dn" is @fn"" and en" is @gn""3then dn",en" is @fn",gn""

    R:R: 6f dn" is @fn"" and en" is @gn""3

    then dn"en" is @fn"gn""

    R:R: 6f dn" is @fn"" and fn" is @gn""3then dn" is @gn""

  • 8/17/2019 2. Asymtotic Notation

    35/38

    TFEOREM

    R:R: 6f fn" + a) , a1n , - , adnd3 d and a7 are constants3 then fn" @nd"

    R:R: nx is @an" for any Bxed x F ) and a F 1

    RQ:RQ: log nx is @log n" for any Bxed x F )

    R:R: log xn is @ny" for any Bxed constants xF ) and y F )

  • 8/17/2019 2. Asymtotic Notation

    36/38

      SOME FELPULMATFEMATICSConstant Series: For integersaandb,a≤ b,

    Linear Series (Arithmetic Series): Forn ≥ 0,

    Quadratic Series:Forn ≥ 0,

      ∑=

    ++=+++=

    n

    i

    nnnni

    1

    2222

    6

    )12)(1(21  

    ∑=

    +−=b

    ai

    ab   11

    2

    )1(21

    1

    +=+++=∑

    =

    nnni

    n

    i

  • 8/17/2019 2. Asymtotic Notation

    37/38

    SOME FELPULMATFEMATICS Cubic Series:Forn ≥ 0,

    Geometric Series: For real x ≠ 1,

     

    For | x| < 1, 

    ∑=

    +=+++=

    n

    i

    nnni

    1

    223333

    4

    )1(21  

    ∑=

    +

    −=++++=

    n

    n

    nk 

     x

     x x x x x

    0

    12

    1

    11  

    ∑∞

    =   −=

    0   1

    1

     x x

  • 8/17/2019 2. Asymtotic Notation

    38/38

    SOME FELPULMATFEMATICS Cubic Series:Forn ≥ 0,

    Geometric Series: For real x ≠ 1,

     

    For | x| < 1, 

    ∑=

    +=+++=

    n

    i

    nnni

    1

    223333

    4

    )1(21  

    ∑=

    +

    −=++++=

    n

    n

    nk 

     x

     x x x x x

    0

    12

    1

    11  

    ∑∞

    =   −=

    0   1

    1

     x x