Design Analysis and Algorithm

Embed Size (px)

Citation preview

  • 7/25/2019 Design Analysis and Algorithm

    1/83

    UNIT 2Algorithms Analysis

    Techniques & DesignTechniqueEfciency o Algorithms,

    Analysis o Recursive Programs,

    Solving Recurrence Equation

    ivi!e " #onquer Algorithms

    ynamic $rogramming

    %ree!y Algorithm

    &ac'trac'ing(o$e #haitali

    )rushnaRoll no* +

  • 7/25/2019 Design Analysis and Algorithm

    2/83

    Efciency o Algorithms,

    e-nition

    A!vance! .IS

    /un!amental techniques 0hich are use! to !esign analgorithm efciently*

    +1 ivi!ean!#onquer

    21 %ree!y metho!

    31 ynamic Programming

    41 &ac'trac'ing

    1 &ranchan!&oun!

  • 7/25/2019 Design Analysis and Algorithm

    3/83

    Analysis o Recursive Programs

    e-nition

    A!vance! .IS

    Recurrence relations oten arise in calculating thetime an! s$ace com$le5ity o algorithms1

    Any $ro6lem can 6e solve! either 6y 0riting

    recursive algorithm or 6y 0riting nonrecursive

    algorithm1

    A recursive algorithm is one 0hich ma'es a

    recursive call to itsel 0ith smaller in$uts1

  • 7/25/2019 Design Analysis and Algorithm

    4/83

    7e oten use a recurrence relation to !escri6e the

    running time o a recursive algorithm1

    A recurrence relation is an equation or

    inequality that !escri6es a unction in terms o its

    value on smaller in$uts or as a unction o

    $rece!ing 8or lo0er9 terms1

    A!vance! .IS

    O O O ' '

  • 7/25/2019 Design Analysis and Algorithm

    5/83

    METHOD !OR O"#$% RE'(RRE%'E

    RE"AT$O%

    e-nition

    A!vance! .IS

    7e 0ill intro!uce three metho!s o solving therecurrence equation*

    +1 The Su6stitution .etho!

    21 Iteration .etho!

    31 The Recursiontree metho!

    41 .aster metho!

  • 7/25/2019 Design Analysis and Algorithm

    6/83

    In su6stitution metho!, 0e guess a 6oun! an!then use mathematical in!uction to $rove ourguess correct1

    The iteration metho! converts the recurrence intoa summation an! then relies on techniques or6oun!ing summations to solve the recurrence1

    The .aster metho! $rovi!es 6oun!s or therecurrence o the orm

    A!vance! .IS

  • 7/25/2019 Design Analysis and Algorithm

    7/83

    Divi)e & conquer technique

    e-nition

    A!vance! .IS

    ivi!e " conquer technique is a to*+)ona**roach to solve a $ro6lem1

    The algorithm 0hich ollo0s !ivi!e an! conquer

    technique involves 3 ste$s*

    -. Divi)e the original $ro6lem into a set o su6

    $ro6lems1

    /. 'onquer 8or Solve9 every su6$ro6lem

    in!ivi!ually, recursive1

    0. 'om1ine the solutions o these su6 $ro6lems to

    et the solution o ori inal ro6lem1

  • 7/25/2019 Design Analysis and Algorithm

    8/83

    &inary search

    e.g. 2 4 5 6 7 8 9

    search :* nee!s 3 com$arisons

    time* ;8log n9 The 6inary search can 6e use! only i the

    elements are sorte! an! store! in an array1

  • 7/25/2019 Design Analysis and Algorithm

    9/83

    Algorithm 6inarysearch

    $n*ut* A sorte! sequence o n elements store! in an array

    Out*ut2The $osition o 5 8to 6e searche!91

    te* -2 I only one element remains in the array, solve it!irectly1

    te* /2 #om$are 5 0ith the mi!!le element o the array1

    Ste$ 21+* I 5 < mi!!le element, then out$ut it an! sto$1

    Ste$ 212* I 5 = mi!!le element, then recursively solve the$ro6lem 0ith 5 an! the let hal array1

    Ste$ 213* I 5 > mi!!le element, then recursively solve the

    $ro6lem 0ith 5 an! the right hal array1

  • 7/25/2019 Design Analysis and Algorithm

    10/83

    Algorithm &inSearch8a, lo0, high, 59

    ?? a@* sorte! sequence in non!ecreasing or!er

    ?? lo0, high* the 6oun!s or searching in a @

    ?? 5* the element to 6e searche!

    ?? I 5 < a@B, or some B, then return B else return C+

    i 8lo0 > high9 then return C+ ?? invali! range i 8lo0 < high9 then ?? i small P

    i 85

  • 7/25/2019 Design Analysis and Algorithm

    11/83

    4 ++

    .erge t0o sorte! sequences into a singleone1

    time com$le5ity* ;8mDn9,

    m an! n* lengths o the t0o sorte! lists

    T0o0ay merge

    [25 37 48 57][12 33 86 92]

    [12 25 33 37 48 57 86 92]

    merge

  • 7/25/2019 Design Analysis and Algorithm

    12/83

    4 +2

    Sort into non!ecreasing or!er

    log2n $asses are require!1

    time com$le5ity* ;8nlogn9

    .erge sort

    [25][57][48][37][12][92][86][33]

    [25 57][37 48][12 92][33 86]

    [25 37 48 57][12 33 86 92]

    [12 25 33 37 48 57 86 92]

    pass 1

    pass 2

    pass 3

  • 7/25/2019 Design Analysis and Algorithm

    13/83

    Algorithm .ergeSort

    $n*ut2 A set S o n elements1

    Out*ut2The sorte! sequence o the in$uts in

    non!ecreasing or!er1

    te* -2 I S2, solve it !irectly1

    te* /2 Recursively a$$ly this algorithm to solve the let

    hal $art an! right hal $art o S, an! the results are

    store! in S+ an! S2, res$ectively1te* 02 Perorm the t0o0ay merge scheme on S+ an! S21

  • 7/25/2019 Design Analysis and Algorithm

    14/83

    P < 8A++D A2298&++D &229F < 8A2+D A229&++R < A++8&+2 &229

    S < A228&2+ &++9

    T < 8A++D A+29&22U < 8A2+ A++98&++D &+29

    G < 8A+2 A2298&2+D &2291

    #++< P D S T D G

    #+2< R D T

    #2+< F D S

    #22< P D R F D U

    Strassens matri5 multi$licaiton

    #++

    < A++

    &++

    D A+2

    &2+

    #+2

    < A++

    &+2

    D A+2

    &22#

    2+ < A

    2+&

    ++D A

    22

    &2+

    #22

    < A2+

    &+2

    D A22

    &22

  • 7/25/2019 Design Analysis and Algorithm

    15/83

    Time com$le5ity

    : multi$lications an! +H a!!itions or

    su6tractions Time com$le5ity*

    T(n) =

    b

    7T(n/2)+an2, n 2

    , n > 2

    )(O)(O

    )(

    constantais,7)(

    )1(7))()(1(

    )4/(7

    )4/(7)((7

    )2/(7)(

    81.27log

    7log4log7log4log7loglog

    4

    72

    loglog

    4

    72

    1

    4

    72

    4

    7

    4

    72

    22

    4

    72

    2

    2

    2

    2

    2

    222222

    22

    nn

    ncnncn

    ccn

    Tan

    nTanan

    nTaan

    nTannT

    n

    nn

    kk

    n

    =

    +=+=

    +

    +++++=

    =

    ++=

    ++=

    +=

    +

  • 7/25/2019 Design Analysis and Algorithm

    16/83

    4 +

    .a5imum num6ers

    -n!ing the ma5imum o a set S o nnum6ers

  • 7/25/2019 Design Analysis and Algorithm

    17/83

    ree)y Algorithm

    A greedy algorithmal0ays ma'es the choice that

    loo's 6est at the moment

    .y every!ay e5am$les*

    7al'ing to the #orner

    Playing a 6ri!ge han!

    The ho$e* a locally o$timal choice 0ill lea! to aglo6ally o$timal solution

    /or some $ro6lems, it 0or's

    Inclu!es

    som6o!yJgmail1com

  • 7/25/2019 Design Analysis and Algorithm

    18/83

    A Generic Greedy Algorithm:

    (1) Initialize Cto be the set o can!i!ate sol"tions (2)Initialize a set S= the e#$t% set (the set is to be theo$ti#al sol"tion &e a'e const'"cting). () hile Can! Sis (still) not a sol"tion !o (.1) selectx'o#set C"sing a g'ee!% st'ateg% (.2) !eletex'o# C

    (.) i *x Sis afeasiblesol"tion, thenS= S*x (i.e., a!!xto set S) (4)i Sis a sol"tion then 'et"'n S

    ()else 'et"'nfailure

    In gene'al, a g'ee!% algo'ith# is eicient beca"se it #a-es ase"ence o (local) !ecisions an! ne/e' bac-t'ac-s. The

    sol"tion is not al&a%s o$ti#al, ho&e/e'.

  • 7/25/2019 Design Analysis and Algorithm

    19/83

    e-nition

    A .inimum S$anning Tree 8.ST9 is asu6gra$h o an un!irecte! gra$hsuch that the su6gra$h s$ans

    8inclu!es9 all no!es, is connecte!, isacyclic, an! has minimum total e!ge0eight

  • 7/25/2019 Design Analysis and Algorithm

    20/83

    Algorithm #haracteristics

    &oth PrimKs an! )rus'alKs Algorithms0or' 0ith un!irecte! gra$hs

    &oth 0or' 0ith 0eighte! an!un0eighte! gra$hs 6ut are moreinteresting 0hen e!ges are 0eighte!

    &oth are gree!y algorithms that$ro!uce o$timal solutions

  • 7/25/2019 Design Analysis and Algorithm

    21/83

    PrimKs Algorithm

    Similar to iB'straKs Algorithm e5ce$tthat d

    v

    recor!s e!ge 0eights, not

    $ath lengths

  • 7/25/2019 Design Analysis and Algorithm

    22/83

    3al4+Through

    InitialiLearray

    K dv pv

    A 0

    B 0

    C 0 D 0

    E 0

    F 0

    G 0

    H 0

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    /

  • 7/25/2019 Design Analysis and Algorithm

    23/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    Start 0ith any no!e,say

    K dv pv

    A

    B

    C

    D T

    E

    F

    G

    H

    /

  • 7/25/2019 Design Analysis and Algorithm

    24/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    U$!ate !istances o

    a!Bacent, unselecte!no!es

    K dv pv

    A

    B

    C

    D T

    E 2

    F 18

    G 2

    H

    /

  • 7/25/2019 Design Analysis and Algorithm

    25/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    Select no!e 0ith

    minimum !istance

    K dv pv

    A

    B

    C

    D T

    E 2

    F 18

    G T 2

    H

    /

  • 7/25/2019 Design Analysis and Algorithm

    26/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    U$!ate !istances o

    a!Bacent, unselecte!no!es

    K dv pv

    A

    B

    C

    D T

    E 7 3

    F 18

    G T 2

    H 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    27/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    Select no!e 0ith

    minimum !istance

    K dv pv

    A

    B

    C T

    D T

    E 7 3

    F 18

    G T 2

    H 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    28/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    U$!ate !istances o

    a!Bacent, unselecte!no!es

    K dv pv

    A

    B 4

    C T

    D T

    E 7 3

    F

    G T 2

    H 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    29/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    Select no!e 0ith

    minimum !istance

    K dv pv

    A

    B 4

    C T

    D T

    E 7 3

    F T

    G T 2

    H 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    30/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    U$!ate !istances o

    a!Bacent, unselecte!no!es

    K dv pv

    A 1 0

    B 4

    C T

    D T

    E 2 0

    F T

    G T 2

    H 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    31/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    Select no!e 0ith

    minimum !istance

    K dv pv

    A 1 0

    B 4

    C T

    D T

    E T 2 0

    F T

    G T 2

    H 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    32/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    U$!ate !istances o

    a!Bacent, unselecte!no!es

    K dv pv

    A 1 0

    B 4

    C T

    D T

    E T 2 0

    F T

    G T 2

    H 3

    /

    Ta6le entriesunchange!

  • 7/25/2019 Design Analysis and Algorithm

    33/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    Select no!e 0ith

    minimum !istance

    K dv pv

    A 1 0

    B 4

    C T

    D T

    E T 2 0

    F T

    G T 2

    H T 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    34/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    U$!ate !istances o

    a!Bacent, unselecte!no!es

    K dv pv

    A 4 5

    B 4

    C T

    D T

    E T 2 0

    F T

    G T 2

    H T 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    35/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    Select no!e 0ith

    minimum !istance

    K dv pv

    A T 4 5

    B 4

    C T

    D T

    E T 2 0

    F T

    G T 2

    H T 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    36/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    U$!ate !istances o

    a!Bacent, unselecte!no!es

    K dv pv

    A T 4 5

    B 4

    C T

    D T

    E T 2 0

    F T

    G T 2

    H T 3

    /

    Ta6le entriesunchange!

  • 7/25/2019 Design Analysis and Algorithm

    37/83

    5

    /6

    A

    H

    7

    !

    E

    D

    '

    8

    /

    -9

    -:

    05

    0

    8

    :

    ;

    0

    -9

    Select no!e 0ith

    minimum !istance

    K dv pv

    A T 4 5

    B T 4

    C T

    D T

    E T 2 0

    F T

    G T 2

    H T 3

    /

  • 7/25/2019 Design Analysis and Algorithm

    38/83

    5

    A

    H

    7

    !

    E

    D

    '

    /

    05

    0

    0

    #ost o .inimum

    S$anning Tree < dv=/-

    K dv pv

    A T 4 5

    B T 4

    C T

    D T

    E T 2 0

    F T

    G T 2

    H T 3

    /

    Done

  • 7/25/2019 Design Analysis and Algorithm

    39/83

    6'"s-als lgo'ith#

    o'- &ith e!ges, 'athe' than no!es

    T&o ste$s 9o't e!ges b% inc'easing e!ge &eight 9elect the i'st :;: < 1 e!ges that !o not

    gene'ate a c%cle

  • 7/25/2019 Design Analysis and Algorithm

    40/83

    al-Th'o"gh#onsi!er an un!irecte!, 0eightgra$h

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4(,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    42/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    43/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    44/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

    Acce$ting e!ge 8E,%9 0oul! create acycle

  • 7/25/2019 Design Analysis and Algorithm

    45/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    46/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    47/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    48/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    49/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    50/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    51/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    52/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    0

    /

    5

    ) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1

  • 7/25/2019 Design Analysis and Algorithm

    53/83

    Select -rst GC+ e!ges 0hich !o

    not generate a cycle

    edge d v

    (,>) 1

    (,3) 2

    (>,3)

    (,)

    (3,5)

    (,0)

    (?,) 4

    6

    -

    A

    H

    7

    !

    E

    D

    '

    /

    0

    0

    0

    edge d v

    (?,>) 4

    (?,0) 4

    (?,5) 4

    (,5)

    (,0) @

    (,?) 8

    (,0) 1DoneTotal 'ost = dv=21

    5

    Mnotconsi!ere!

  • 7/25/2019 Design Analysis and Algorithm

    54/83

  • 7/25/2019 Design Analysis and Algorithm

    55/83

    >na*sac4 *ro1lem

  • 7/25/2019 Design Analysis and Algorithm

    56/83

    >na*sac4 *ro1lem

    %iven a 'na$sac' 0ith ma5imum ca$acity W, an!

    a set Sconsisting o nitems

    Each item ihas some 0eight wian! 6ene-t value

    bi 8all wi an! Ware integer values9

    Pro6lem* o0 to $ac' the 'na$sac' to achieve

    ma5imum total value o $ac'e! itemsO

    Inclu!es

    A!vance! .IS

  • 7/25/2019 Design Analysis and Algorithm

    57/83

    Pro6lem, in other 0or!s, is to -n!

    9+- >na*sac4 *ro1lem

    The $'oble# is calle! a A0-1B $'oble#,

    beca"se each ite# #"st be enti'el% acce$te!

    o' 'eCecte!.

    Ti

    i

    Ti

    i Wwb s"bCect to#aD

    ynamic Programming

  • 7/25/2019 Design Analysis and Algorithm

    58/83

    ynamic Programming

    ynamic Programming is an algorithm !esign technique oro$timiLation $ro6lems* oten minimiLing or ma5imiLing1

    i'e !ivi!e an! conquer, P solves $ro6lems 6y com6iningsolutions to su6$ro6lems1

    Unli'e !ivi!e an! conquer, su6$ro6lems are notin!e$en!ent1 Su6$ro6lems may share su6su6$ro6lems,

    o0ever, solution to one su6$ro6lem may not aQect thesolutions to other su6$ro6lems o the same $ro6lem1 8.ore onthis later19

    P re!uces com$utation 6y Solving su6$ro6lems in a 6ottomu$ ashion1

    Storing solution to a su6$ro6lem the -rst time it is solve!1

    oo'ing u$ the solution 0hen su6$ro6lem is encountere! again1

    )ey* !etermine structure o o$timal solutions

  • 7/25/2019 Design Analysis and Algorithm

    59/83

    #om$ +22, S$ring 24

    Ste$s in ynamic Programming

    1. Characterize structure of an optimal solution.

    2. Dene value of optimal solution recursively.

    . Compute optimal solution values either top!

    down with caching or bottom!up in a table.

    ". Construct an optimal solution from computed

    values.

    !loy)?s Algorithm2 All *airs shortest *aths

  • 7/25/2019 Design Analysis and Algorithm

    60/83

    !loy) s Algorithm2 All *airs shortest *aths

    Pro6lem* In a 0eighte! 8!i9gra$h, -n! shortest $aths

    6et0een every $air o vertices

    Same i!ea* construct solution through series o

    matrices 89, , 8n9 using increasing su6sets o thevertices allo0e! as interme!iate

    E5am$le*

    3

    42

    +

    4

    +

    +

    3

    4 + 4 3

    +

    !loy)?s Algorithm @matri generationB

  • 7/25/2019 Design Analysis and Algorithm

    61/83

    !loy) s Algorithm @matri generationB

    On the -th ite'ation, the algo'ith# !ete'#ines sho'test $aths

    bet&een e/e'% $ai' o /e'tices i, C that "se onl% /e'tices a#ong 1,E,- as inte'#e!iate

    (-)Fi,CG = #in *(-1)Fi,CG, (-1)Fi,-G + (-1)F-,CG

    i

    #

    $

    D@k+-BCi,j

    D@k+-BCi,k

    D@k+-BCk,j

    Initialcon!itionO

    !loy)?s Algorithm @eam*leB

  • 7/25/2019 Design Analysis and Algorithm

    62/83

    !loy) s Algorithm @eam*leB

    3 2 : +

    D89 FO(