dynamic programming - algorithm

Upload: -sufi

Post on 07-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Dynamic Programming - Algorithm

    1/63

    CSC2105: AlgorithmsCSC2105: Algorithms

    Dynamic ProgrammingDynamic Programming

    Mashiour RahmanMashiour RahmanAssistant ProfessorAssistant [email protected]@aiub.edu

    American International University BangladeshAmerican International University Bangladesh

  • 8/6/2019 Dynamic Programming - Algorithm

    2/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming2

    gor m es gngor m es gntechniquestechniques

    Iterative (brute-force) algorithmsIterative (brute-force) algorithms

    Example: insertion sort

    Algorithms that use efficient data structuresAlgorithms that use efficient data structuresExample: heap sort

    Divide-and-conquer algorithmsDivide-and-conquer algorithms

    Example: Binary search, merge sort, quick sortDynamic programmingDynamic programming

    Example: Fibonacci numbers, Matrix multiplication optimization, LongestCommon Subsequence

    Greedy algorithmsGreedy algorithms

    Example: Huffman coding, Minimum Spanning Tree, Dijkstara

    Graph AlgorithmsGraph AlgorithmsExample: BFS (Robot moving, Path Finding), DFS( Topological Sorting,Strongly Connected Component)

  • 8/6/2019 Dynamic Programming - Algorithm

    3/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming2

    Fibonacci NumbersFibonacci Numbers

    Leonardo Fibonacci (1202)Leonardo Fibonacci (1202)::A rabbit starts producing offspring during the second yearafter its birth and produces one child each generation

    How many rabbits will there be aftern generations?

    F(1)=1 F(2)=1 F(3)=1 F(4)=3 F(5)=5 F(6)=8+1F(3)=2 F(4)=2+1 F(5)=3+2 F(6)=5+3

  • 8/6/2019 Dynamic Programming - Algorithm

    4/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming2

    Fibonacci NumbersFibonacci Numbers

    F(n)= F(n-1)+ F(n-2)F(n)= F(n-1)+ F(n-2)

    F(0)F(0)=0, F(1)=0, F(1)

    =1=1

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34

    Straightforward recursive procedure is slow!Straightforward recursive procedure is slow!

    FibonacciR(n)

    01 if n 1 then return n02 else return FibonacciR(n-1) + FibonacciR(n-2)

    FibonacciR(n)

    01 if n 1 then return n02 else return FibonacciR(n-1) + FibonacciR(n-2)

  • 8/6/2019 Dynamic Programming - Algorithm

    5/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming2

    Fibonacci NumbersFibonacci Numbers

    We keep calculating the same value over and over!We keep calculating the same value over and over!Subproblems are overlapping they share sub-subproblems

    F(6) = 8

    F(5)

    F(4)

    F(3)

    F(1)

    F(2)

    F(0)

    F(1)F(1) F(1)F(1)

    F(2)F(2)

    F(0)F(0)

    F(3)F(3)

    F(1)F(1)

    F(2)F(2)

    F(0)F(0)

    F(1)F(1)

    F(4)F(4)

    F(3)F(3)

    F(1)F(1)

    F(2)F(2)

    F(0)F(0)

    F(1)F(1) F(1)F(1)

    F(2)F(2)

    F(0)F(0)

  • 8/6/2019 Dynamic Programming - Algorithm

    6/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming2

    Fibonacci NumbersFibonacci Numbers

    How many summations are there in F(How many summations are there in F(nn)?)?

    F(n) = F(n1) + F(n 2) + 1

    F(n) 2F(n 2) +1 and F(1) = F(0) = 0

    Solving the recurrence we get

    F(n) 2n/2 1 1.4n

    Running time isRunning time is exponentialexponential!!

  • 8/6/2019 Dynamic Programming - Algorithm

    7/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming2

    Fibonacci NumbersFibonacci Numbers

    We can calculateWe can calculate FF((nn)) inin linearlinear time bytime by

    remembering solutionsremembering solutions to the solvedto the solved

    subproblems (=subproblems (= dynamic programmingdynamic programming).).

    Compute solution in a bottom-up fashionCompute solution in a bottom-up fashion

    Trade space for time!Trade space for time!

    Fibonacci(n)

    01 F[0]0

    02 F[1]1

    03 for i 2 to n do

    04 F[i]

    F[i-1]

    + F[i-2]

    05 return F[n]

    Fibonacci(n)

    01 F[0]0

    02 F[1]103 for i 2 to n do

    04 F[i]

    F[i-1]

    + F[i-2]

    05 return F[n]

  • 8/6/2019 Dynamic Programming - Algorithm

    8/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming2

    Fibonacci NumbersFibonacci Numbers

    In fact, only two values need to be rememberedIn fact, only two values need to be remembered

    at any time!at any time!

    FibonacciImproved(n)

    01 if n 1 then return n02 Fim2 0

    03 Fim1 1

    04 for i 2 to n do

    05 FiFim1+ Fim206 Fim2 Fim1

    07 Fim1 Fi

    05 return Fi

    FibonacciImproved(n)

    01 if n 1 then return n02 Fim2 0

    03 Fim1 1

    04 for i 2 to n do

    05 Fi

    Fim1

    + Fim2

    06 Fim2 Fim1

    07 Fim1 Fi

    05 return Fi

  • 8/6/2019 Dynamic Programming - Algorithm

    9/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming2

    HistoryHistory

    Dynamic programmingDynamic programming

    Invented in the 1957 by Richard BellmanRichard Bellman as a

    general method for optimizing multistage decision

    processes

    The term programming refers to a tabular methodtabular method.Often used foroptimizationoptimization problems.

  • 8/6/2019 Dynamic Programming - Algorithm

    10/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Dynamic ProgrammingDynamic Programming

    Solves problems bySolves problems by combiningcombining the solutions tothe solutions tosubproblems that contain common sub-sub-problems.subproblems that contain common sub-sub-problems.

    Difference between DP and Divide-and-ConquerDifference between DP and Divide-and-Conquer

    Using Divide and ConquerDivide and Conquerto solve these problems isinefficientinefficient as the same common sub-sub-problems

    have to be solved many timesmany times.

    DP will solve each of them onceonce and theiranswersanswers

    are stored in a tableare stored in a table for future reference.

  • 8/6/2019 Dynamic Programming - Algorithm

    11/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Intuitive ExplanationIntuitive Explanation

    Given a problemGiven a problem PP, obtain, obtaina sequence of problemsa sequence of problems

    QQ00,, QQ11, .,, ., QQmm, where:, where:

    You have a solution to QQ00

    The solution to a problem QQjj,

    j > 0j > 0, can be obtained from

    solutions to problems

    QQ00...QQkk, kk< j< j, that appear

    earlier in the sequence.

    El t f D iE t D

  • 8/6/2019 Dynamic Programming - Algorithm

    12/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Elements of DynamicE ements o Dynam cProgrammingProgramming

    DPDP is used to solve problems with the followingis used to solve problems with the following

    characteristics:characteristics:

    Optimal sub-structureOptimal sub-structure (Principle of Optimality)

    an optimal solution to the problem contains within it

    optimal solutions to sub-problems.

    Overlapping subproblemsOverlapping subproblems

    there exist some places where we solve the same

    subproblem more than once

  • 8/6/2019 Dynamic Programming - Algorithm

    13/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    xamp e: onaccxamp e: onaccNumbersNumbers

    We keep calculating the same value over and over!We keep calculating the same value over and over!Subproblems are overlapping they share sub-subproblems

    F(6) = 8

    F(5)

    F(4)

    F(3)

    F(1)

    F(2)

    F(0)

    F(1)F(1) F(1)F(1)

    F(2)F(2)

    F(0)F(0)

    F(3)F(3)

    F(1)F(1)

    F(2)F(2)

    F(0)F(0)

    F(1)F(1)

    F(4)F(4)

    F(3)F(3)

    F(1)F(1)

    F(2)F(2)

    F(0)F(0)

    F(1)F(1) F(1)F(1)

    F(2)F(2)

    F(0)F(0)

  • 8/6/2019 Dynamic Programming - Algorithm

    14/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    eps o es gn ng aeps o es gn ng aDynamic ProgrammingDynamic Programming

    AlgorithmAlgorithm

    1.1. CharacterizeCharacterize optimal sub-structureoptimal sub-structure

    2.2. RecursivelyRecursivelydefine the value of andefine the value of anoptimal solutionoptimal solution

    3.3. Compute the valueCompute the value bottom upbottom up

    4.4. (if needed)(if needed) ConstructConstructan optimal solutionan optimal solution

  • 8/6/2019 Dynamic Programming - Algorithm

    15/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    1.Optimal Sub-Structure1.Optimal Sub-Structure

    An optimal solution to the problem contains optimalAn optimal solution to the problem contains optimal

    solutions to sub-problemssolutions to sub-problems

    Solution to a problem:

    Making achoicechoice out of a number of

    possibilitiespossibilities (look what

    possible choices there can be)

    SolvingSolving one or more sub-problemssub-problems that are the result of aresult of a

    choicechoice (characterize the space of sub-problems)

    Show that solutions to sub-problems must themselves be

    optimal for the whole solution to be optimal.

  • 8/6/2019 Dynamic Programming - Algorithm

    16/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    2. Recursive Solution2. Recursive Solution

    Write a recursive solution for the value of anWrite a recursive solution for the value of an

    optimal solution.optimal solution.

    Show that the number of different instancesShow that the number of different instancesof sub-problems is bounded by a polynomial.of sub-problems is bounded by a polynomial.

    +=

    k

    kMM

    opt

    k

    optchoicethemakingwithassociatedcostThe

    choicefromresultingssubproblemallforofnCombinatioOptimal

    choicesallover

  • 8/6/2019 Dynamic Programming - Algorithm

    17/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    3. Bottom Up3. Bottom Up

    ComputeCompute the value of an optimal solution inthe value of an optimal solution in

    a bottom-up fashion, so that you always havea bottom-up fashion, so that you always have

    the necessarythe necessary sub-results pre-computedsub-results pre-computed

    (or use memoization)(or use memoization)

    Check if it is possible toCheck if it is possible to reducereduce thethe spacespace

    requirements, by requirements, by forgettingforgetting solutions to solutions to

    sub-problems that will not be used any moresub-problems that will not be used any more

  • 8/6/2019 Dynamic Programming - Algorithm

    18/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    . ons ruc p ma. ons ruc p maSolutionSolution

    Construct an optimal solution fromConstruct an optimal solution fromcomputed informationcomputed information (which(which

    records a sequence of choices maderecords a sequence of choices made

    that lead to an optimal solution)that lead to an optimal solution)

  • 8/6/2019 Dynamic Programming - Algorithm

    19/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    ev ew: a r xev ew: a r xMultiplicationMultiplication

    Matrix-Multiply(A,B):Matrix-Multiply(A,B):

    1 if columns[A] != rows[B] then2 error "incompatible dimensions"

    3 else{

    5 for i = 1 to rows[A] do

    6 for j = 1 to columns[B] do{

    7 C[i,j] = 0

    8 for k = 1 to columns[A] do

    9 C[i,j] = C[i,j]+A[i,k]*B[k,j]

    10 }

    11 }

    12 return C

    Time complexityTime complexity = O(pqr),

    where |A|=pq and |B|=qr

  • 8/6/2019 Dynamic Programming - Algorithm

    20/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Two matrices,Two matrices,A nA nmmmatrixmatrixandand B mB mkkmatrix, can be multiplied to getmatrix, can be multiplied to get CCwithwith

    dimensionsdimensions nnkk,, usingusing nnmmkkscalarscalar

    multiplicationsmultiplications

    Problem:: ComputeCompute a product of manya product of many

    matricesmatrices efficientlyefficiently

    Multiplying MatricesMultiplying Matrices

    , , ,

    1

    m

    i j i l l j

    l

    c a b=

    = 11 12

    1311 12

    21 22 22

    2321 22

    31 32

    ... ... ...

    ... ...

    ... ... ...

    a abb b

    a a cbb b

    a a

    =

  • 8/6/2019 Dynamic Programming - Algorithm

    21/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Matrix Chain Multiplication [MCM]:Matrix Chain Multiplication [MCM]:

    The ProblemThe Problem

    Input: Matrices: Matrices TT11,T,T22,, T,, Tnn, each, each TTii of sizeof size ddi-1i-1 x dx dii

    Output: Fully: Fullyparenthesizedparenthesizedproductproduct TT11TT22TTnn thatthat

    minimizes the number of scalar multiplications.minimizes the number of scalar multiplications.

    A product of matrices is fully parenthesized if it is eitherA product of matrices is fully parenthesized if it is either

    a) a single matrix, or

    b) the product of 2 fully parenthesized matrix products surrounded byparentheses.

    Example:Example: TT11 TT22 TT33 TT44can be fully parenthesized as:can be fully parenthesized as:

    1.1. (T(T11 (T(T22 (T(T33 TT44 )))))) 4.4. ((T((T11 (T(T22 TT33 ))T))T44 ))

    2.2. (T(T11 ((T((T22 TT33 )T)T44 )))) 5.5. (((T(((T11 TT22 )T)T33 )T)T44 ))

    3.3. ((T((T11 TT22 )(T)(T33 TT44 ))))

  • 8/6/2019 Dynamic Programming - Algorithm

    22/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    (A1( A

    2( A

    3A4) )) : p

    2p3p4+ p

    1p2p4+ p

    0p1p4

    (((A1

    A2) A

    3)A4) : p

    0p1p2+ p

    0p2p3+ p

    0p3p4

    Suppose size ofAiis p

    i-1by p

    i.

    A1

    A2A3

    A4

    p0

    p1 p2 p3 p4

    Matrix Chain Multiplication [MCM]:Matrix Chain Multiplication [MCM]:

    The ProblemThe Problem

  • 8/6/2019 Dynamic Programming - Algorithm

    23/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    A1

    A2A3

    A4

    p0

    p1 p2 p3 p4

    (A(A33 AA44))

    p2p3p4 multiplications

    A1

    A2

    p0

    p1 p2

    A3 A4

    p4

    A1

    A2(A3A4)

    p0

    p1 p4

    (A(A22(A(A33 AA44))))

    p1p2p4

    multiplications

    A1(A2(A3A4))p0

    p4

    (A(A11(A(A22(A(A33 AA44))))))

    p0p1p4multiplications

    Total: p2p3p4+ p

    1p2p4+ p

    0p1p4

    Matrix Chain Multiplication [MCM]:Matrix Chain Multiplication [MCM]:

    (A(A11(A(A22(A(A33 AA44))))))

  • 8/6/2019 Dynamic Programming - Algorithm

    24/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    A1

    A2A3

    A4

    p0

    p1

    p2

    p3

    p4

    (A1A2)

    A3

    A4

    p0

    p2

    p3

    p4

    (A(A11 AA22))p0p1p2 multiplications

    (A1A2)A3

    A4

    p0

    p3 p4

    ((A((A11 AA22)A)A33))

    p0p2p3

    multiplications

    ((A1A2)A3)A4p0

    p4

    (((A(((A11 AA22)A)A33)A)A44))

    p0p3p4multiplications

    Total: p0p1p2+ p

    0p2p3+ p

    0p3p4

    Matrix Chain Multiplication [MCM]:Matrix Chain Multiplication [MCM]:

    (((A(((A11 AA22)A)A33)A)A44))

  • 8/6/2019 Dynamic Programming - Algorithm

    25/63Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCM: ParenthesizationMCM: ParenthesizationSome Preliminaries

    Matrix multiplication is associativeassociative

    (ABAB)CC=AA(BCBC)

    The parenthesizationparenthesization matters

    ConsiderAABBCCDD, where

    AAis 303011, BBis 114040,

    CCis 40401010, DDis 10102525Costs:

    (((ABAB)CC)DD)

    30x1x40

    (AB)=30x40

    30x40x10

    ((AB)C)=30x10

    30x10x25

    (((AB)C)D)=30x25

    A=30 x 1 B=1 x 40

    C=40x10

    D=10x25

    =1200 + 12000 7500 = 20700+

  • 8/6/2019 Dynamic Programming - Algorithm

    26/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCM: ParenthesizationMCM: ParenthesizationSome Preliminaries

    Matrix multiplication is associativeassociative

    (ABAB)CC=AA(BCBC)

    The parenthesizationparenthesization matters

    ConsiderAABBCCDD, where

    AAis 303011, BBis 114040,

    CCis 40401010, DDis 10102525Costs:

    (((ABAB)CC)DD)

    = 1200 + 12000 + 7500 = 20700

    ((ABAB)(CDCD))

    40x10x25

    (CD)=40x25

    30x40x25

    ((AB)(CD))=30x25

    C=40x10 D=10x25

    30x1x40

    (AB)=30x40

    A=30 x 1 B=1 x 40

    =1200 + 10000 30000 = 41200+

  • 8/6/2019 Dynamic Programming - Algorithm

    27/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCM: ParenthesizationMCM: ParenthesizationSome Preliminaries

    Matrix multiplication is associativeassociative

    (ABAB)CC=AA(BCBC)

    The parenthesizationparenthesization matters

    ConsiderAABBCCDD, where

    AAis 303011, BBis 114040,

    CCis 40401010, DDis 10102525Costs:

    (((ABAB)CC)DD)

    = 1200 + 12000 + 7500 = 20700

    ((ABAB)(CDCD))

    = 1200 + 10000 + 30000 = 41200

    (AA((BCBC)DD))

    1x40x10

    (BC)=1 x10

    1x10x25

    ((BC)D)=1x25

    30x1x25

    (A((BC)D))=30x25

    A=30 x 1

    B=1 x40 C=40x10

    D=10x25

    =400 + 250 750 = 1400+

  • 8/6/2019 Dynamic Programming - Algorithm

    28/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming2 2

    MCM: ParenthesizationMCM: ParenthesizationSome Preliminaries

    Matrix multiplication is associativeassociative

    (ABAB)CC=AA(BCBC)

    The parenthesizationparenthesization matters

    ConsiderAABBCCDD, where

    AAis 303011, BBis 114040,

    CCis 40401010, DDis 10102525Costs:

    (((ABAB)CC)DD)

    = 1200 + 12000 + 7500 = 20700

    ((ABAB)(CDCD))

    = 1200 + 10000 + 30000 = 41200

    (AA((BCBC)DD))

    = 400 + 250 + 750 = 1400

    We need to optimally parenthesize

    TT11x TT22xTTnn, where TTii is a ddi-1i-1 x ddii matrix.

    According to the given example

    dd = {30, 1, 40, 10, 25 }

    TT

    11x

    TT

    22x

    TT

    33x

    TT

    44where

    TT11 = dd1-11-1 x dd11= dd00 x dd11 = 30 x 1

    TT22 = dd2-12-1 x dd22 = dd11 x dd22 = 1 x 40

    TT33 = dd3-13-1 x dd33 = dd22 x dd33 = 40 x 10

    TT44 = dd4-14-1 x dd44 = dd33 x dd44 = 10 x 25

    Costs:

    ((TT11 TT22) TT33) TT44= 20700

    (TT11 TT22) (TT33 TT44) = 41200

    TT11 ((TT22 TT33) TT44) = 1400

  • 8/6/2019 Dynamic Programming - Algorithm

    29/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCM: ParenthesizationMCM: Parenthesization

    Let the number of different parenthesizations, P(n).

    Then,

    Using Generating FunctionGenerating Function we have,P(n) = C(n-1), the (n-1)thCatalan Numberwhere

    Exhaustively checking all possible parenthesizations

    take exponential time!

    ( )n4C3/2n2n

    n/1)1/(nC(n) =+=

    ==

    =

    1-n

    1k

    2nifk)-P(k)P(n

    1nif1P(n)

    MCM :: Step 1: Characterize Optimal Sub-MCM :: Step 1: Characterize Optimal Sub-

  • 8/6/2019 Dynamic Programming - Algorithm

    30/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCM :: Step 1: Characterize Optimal Sub-MCM :: Step 1: Characterize Optimal Sub-structurestructure

    LetLetMM((i, ji, j))be thebe the minimumminimum number of multiplications necessarynumber of multiplications necessary

    to computeto compute TTi..ji..j = T= Tii x x Tx x Tjj

    Key observationsKey observations

    The outermost parenthesis partitions the chain of matrices ((i, ji, j))at some

    kk, (ii k < jk < j): (TTiiTTkk)(TTk+1k+1 TTjj)

    The optimal parenthesization of matrices ((i, ji, j))is also optimal on either

    side ofkk; i.e., for matrices (i, ki, k) and (k+1, jk+1, j)

    Within the optimal parenthesization ofTTi..ji..j

    ,

    (a) the parenthesization ofTTi..ki..k must be optimal

    (b) the parenthesization ofTTk+1..jk+1..j must be optimal

    Why?

    MCM ::MCM :: Step 2: Recurs ve Recurrenceep : ecurs ve ecurrence

  • 8/6/2019 Dynamic Programming - Algorithm

    31/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCM ::MCM :: Step 2: Recurs ve Recurrenceep : ecurs ve ecurrenceFormulationFormulation

    Need to find TT1..n1..n

    LetM(i, j)M(i, j)= minimum # of scalar multiplications needed to compute TTi..ji..j

    Since TTi..ji..j can be obtained by breaking it into TTi..ki..k & TTk+1..jk+1..j , we have

    Note: The sizes ofTTi..ki..k is ddi-1i-1 x dx dkk and TTk+1..jk+1..j is ddkk x dx djj,

    and TTi..ki..k TTk+1..jk+1..j is ddi-1i-1 x dx djjafterddi-1i-1 ddkk ddjjscalar multiplications.

    Let s(i, j)s(i, j) be the value kk where the optimal split occurs

    A direct recursive implementation is exponential a lot of duplicated work.

    But there are only few different subproblems ((i, ji, j)): one solution for each

    choice ofiiandjj (i < ji < j).

    { }

  • 8/6/2019 Dynamic Programming - Algorithm

    32/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCM::MCM::Step 2: Recursive (Recurrence)Step 2: Recurs ve RecurrenceFormulationFormulation

    Recursive-Matrix-Chain(Recursive-Matrix-Chain(dd,, ii,, jj))

    11 ififii == jjthenthen

    22 returnreturn 0;0;

    33 MM[i,[i, jj] =] = ;;

    44 forforkk == iitotojj-1-1 dodo55 qq= Recursive-Matrix-Chain(= Recursive-Matrix-Chain(dd,, ii,, kk) +) +

    Recursive-Matrix-Chain(Recursive-Matrix-Chain(dd,, k+1k+1,, jj) +) +

    ddi-1i-1ddkkddjj

    66 ififqq

  • 8/6/2019 Dynamic Programming - Algorithm

    33/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCMMCM :: Step 2: Recursive (Recurrence):: Step 2: Recursive (Recurrence)

    FormulationFormulationOverlapping SubproblemsOverlapping Subproblems

    Let T(n) be the time complexity ofLet T(n) be the time complexity ofRecursive-Matrix-Chain(d, 1, n)Recursive-Matrix-Chain(d, 1, n)

    For n > 1, we haveFor n > 1, we have

    T(n)= 1 +

    a) 1 is used to cover the cost of lines 1-3, and 8

    b) 1 is used to cover the cost of lines 6-7

    Using substitution, we can show thatUsing substitution, we can show that T(n) 2T(n) 2n-1n-1

    HenceHence T(n) = (2T(n) = (2nn))

    =

    ++1n

    1k

    1)k)-T(n(T(k)

    MCMMCM :: tep : omput ng pt ma:: ep : ompu ng p ma

  • 8/6/2019 Dynamic Programming - Algorithm

    34/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCMMCM :: tep : omput ng pt ma:: ep : ompu ng p maCostsCosts

    To compute TTi..ji..jwe need only values for subproblems of

    length

  • 8/6/2019 Dynamic Programming - Algorithm

    35/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCM :: : uCostsCosts

    Idea:Idea: store the optimal coststore the optimal costM(i, j)M(i, j)for eachfor each

    subproblem in a 2d arraysubproblem in a 2d array

    MM

    [[1..n,1..n1..n,1..n

    ]]

    TriviallyM(i, i)M(i, i) = 0= 0, 11 ii nn

    To computeM(i, j)M(i, j),, where i ji j == LL, we need only

    values ofMMfor subproblems of length

  • 8/6/2019 Dynamic Programming - Algorithm

    36/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCMMCM :: Step 3: Computing the Optimal:: tep : omput ng t e pt maCostsCosts

    Matrix-Chain-Order(Matrix-Chain-Order( dd))

    0101 nn== lengthlength[[dd]-1]-1 ////dd is the array of matrix sizesis the array of matrix sizes02 for02 for ii = 1 to= 1 to nndodo

    0303 MM[[ii,,ii] = 0] = 0 // no multiplication for 1 matrix// no multiplication for 1 matrix

    04 for04 for lenlen= 2 to= 2 to nndodo // len is length of sub-chain// len is length of sub-chain

    05 for05 for ii = 1 to= 1 to nn--lenlen+1 do+1 do //// ii: start of sub-chain: start of sub-chain

    0606 jj ==ii++lenlen-1-1 //// jj: end of sub-chain: end of sub-chain0707 MM[[ii,,jj] =] =

    0808 forfor kk== ii toto jj-1 do-1 do

    0909 qq==MM[[ii,,kk]+M[]+M[kk+1,+1,jj]+]+ddii-1-1ddkkddjj

    1010 ifif qq

  • 8/6/2019 Dynamic Programming - Algorithm

    37/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MCMMCM :: Step 3: Computing the Optimal:: Step 3: Computing the OptimalCostsCosts

    After the execution:After the execution:MM[[1,n1,n]]contains the value of ancontains the value of an

    optimal solution andoptimal solution and ss contains optimal subdivisions (choicescontains optimal subdivisions (choices

    ofofkk) of any subproblem into two sub-subproblems) of any subproblem into two sub-subproblems

    Let us run the algorithm on the six matrices:Let us run the algorithm on the six matrices:

    d={ 30, 35, 15, 5, 10, 20, 25 }d={ 30, 35, 15, 5, 10, 20, 25 }

    See CLRS Figure 15.3See CLRS Figure 15.3

    MatrixMatrix DimensionDimension

    T1T1 30 X 3530 X 35

    T2T2 35 X 1535 X 15

    T3T3 15 X 515 X 5

    T4T4 5 X 105 X 10

    T5T5 10 X 2010 X 20

    T6T6 20 X 2520 X 25

    Simulation-Simulation- 0 1 2 3 4 5 6

  • 8/6/2019 Dynamic Programming - Algorithm

    38/63

    S 1 2 3 4 5 6

    1

    2

    3

    4

    5

    6

    Matrix-Chain-Order(Matrix-Chain-Order( dd))

    0101 nn== lengthlength[[dd]-1]-102 for02 for ii = 1 to= 1 to nndodo

    0303 MM[[ii,,ii] = 0] = 004 for04 for lenlen= 2 to= 2 to nndodo05 for05 for ii = 1 to= 1 to nn--lenlen+1 do+1 do0606 jj ==ii++lenlen-1-1

    0707 MM[[ii,,jj] =] =

    0808 forfor kk== ii toto jj-1 do-1 do0909 qq==MM[[ii,,kk]+M[]+M[kk+1,+1,jj]+]+ddii-1-1 ddkkddjj1010 ifif qq

  • 8/6/2019 Dynamic Programming - Algorithm

    39/63

    S 1 2 3 4 5 6

    1

    2

    3

    4

    5

    6

    Matrix-Chain-Order(Matrix-Chain-Order( dd))

    0101 nn== lengthlength[[dd]-1]-102 for02 for ii = 1 to= 1 to nndodo

    0303 MM[[ii,,ii] = 0] = 004 for04 for lenlen= 2 to= 2 to nndodo05 for05 for ii = 1 to= 1 to nn--lenlen+1 do+1 do0606 jj ==ii++lenlen-1-1

    0707 MM[[ii,,jj] =] =

    0808 forfor kk== ii toto jj-1 do-1 do0909 qq==MM[[ii,,kk]+M[]+M[kk+1,+1,jj]+]+ddii-1-1 ddkkddjj1010 ifif qq

  • 8/6/2019 Dynamic Programming - Algorithm

    40/63

    S 1 2 3 4 5 6

    1 1

    2 2

    3 3

    4 4

    5 5

    6

    Matrix-Chain-Order(Matrix-Chain-Order( dd))

    0101 nn== lengthlength[[dd]-1]-102 for02 for ii = 1 to= 1 to nndodo

    0303 MM[[ii,,ii] = 0] = 004 for04 for lenlen= 2 to= 2 to nndodo05 for05 for ii = 1 to= 1 to nn--lenlen+1 do+1 do0606 jj ==ii++lenlen-1-1

    0707 MM[[ii,,jj] =] =

    0808 forfor kk== ii toto jj-1 do-1 do0909 qq==MM[[ii,,kk]+M[]+M[kk+1,+1,jj]+]+ddii-1-1 ddkkddjj1010 ifif qq

  • 8/6/2019 Dynamic Programming - Algorithm

    41/63

    S 1 2 3 4 5 6

    1 1

    2 2

    3 3

    4 4

    5 5

    6

    Matrix-Chain-Order(Matrix-Chain-Order( dd))

    0101 nn== lengthlength[[dd]-1]-102 for02 for ii = 1 to= 1 to nndodo

    0303 MM[[ii,,ii] = 0] = 004 for04 for lenlen= 2 to= 2 to nndodo05 for05 for ii = 1 to= 1 to nn--lenlen+1 do+1 do0606 jj ==ii++lenlen-1-1

    0707 MM[[ii,,jj] =] =

    0808 forfor kk== ii toto jj-1 do-1 do0909 qq==MM[[ii,,kk]+M[]+M[kk+1,+1,jj]+]+ddii-1-1 ddkkddjj1010 ifif qq

  • 8/6/2019 Dynamic Programming - Algorithm

    42/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    :: : uSolutionSolution

    To get the optimal solutionTo get the optimal solution TT1..61..6 ,, s[]s[] isis

    used as follows:used as follows:TT1..61..6

    = (T= (T1..31..3 TT4..64..6 ) ;) ;sincesince s[1,6] = 3s[1,6] = 3= ((T= ((T1..11..1 TT2..32..3 )(T)(T4..54..5 TT6..66..6 ))))

    ;;sincesince s[1,3] =1s[1,3] =1 andand s[4,6]=5s[4,6]=5

    =((T=((T11 (T(T22 TT33 ))((T))((T44 TT55 )T)T66 ))))

    MCM can be solved inMCM can be solved in O(n3)O(n3) timetime

    Matrix Chain MultiplicationMatr x C a n Mu t p cat on

  • 8/6/2019 Dynamic Programming - Algorithm

    43/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Matrix Chain MultiplicationMatr x C a n Mu t p cat onProblemProblem

    Running timeRunning time

    It is easy to see that it is O(n3)

    (three nested loops)

    It turns out it is also (n3)

    Thus, a reduction from exponential time toThus, a reduction from exponential time topolynomial time.polynomial time.

    MemoizationMemoization

  • 8/6/2019 Dynamic Programming - Algorithm

    44/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MemoizationMemoizationMemoizationMemoization is one way to deal with overlapping subproblemsis one way to deal with overlapping subproblems

    After computing the solution to a subproblem, store it in a table

    Subsequent calls just do a table lookup

    Can modify recursive algorithm to use memoziationCan modify recursive algorithm to use memoziation

    If we prefer recursion we can structure our algorithm as aIf we prefer recursion we can structure our algorithm as a

    recursive algorithmrecursive algorithm::

    Initialize all elements toInitialize all elements to and calland callMemoMCM(i,j)MemoMCM(i,j)

    MemoMCM(i,j)

    1. if i = j thenreturn 0

    2. else if M[i,j] < then return M[i,j]

    3. else for k := i to j-1 do

    4. q := MemoMCM(i,k)+MemoMCM(k+1,j) + d

    i-1dkdj

    5. if q < M[i,j] then

    6. M[i,j] := q

    7. return M[i,j]

    MemoMCM(i,j)

    1. if i = j thenreturn 0

    2. else if M[i,j] < then return M[i,j]

    3. else for k := i to j-1 do

    4. q := MemoMCM(i,k)+

    MemoMCM(k+1,j) + di-1dkdj

    5. if q < M[i,j] then

    6. M[i,j] := q

    7. return M[i,j]

    M i tiM i ti

  • 8/6/2019 Dynamic Programming - Algorithm

    45/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    MemoizationMemoization

    Memoization:Memoization:

    Solve the problem in a top-downtop-down fashion, but

    record the solutions to subproblems in a table.

    Pros and cons:Pros and cons:

    Recursion is usually slower than loops and usesstack space

    Easier to understand

    If not all subproblems need to be solved, you aresure that only the necessary ones are solved

    Longest Common SubsequenceLongest Common Subsequence

  • 8/6/2019 Dynamic Programming - Algorithm

    46/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Longest Common SubsequenceLongest Common SubsequenceThe ProblemThe Problem

    Given two sequencesGiven two sequences

    X = < x1, x2, , xm>

    Z = < z1, z

    2, , z

    k>

    ZZ is ais a subsequencesubsequence ofofXX ifif

    zj = xi( j ) for all j = 1, , k

    whereis strictly increasing(but not required to

    be consecutive)

    Examples:Examples:

    Let X = < A, B, C, B, D, A, B >

    < A >, < B >, < C >, and< D >are subsequences.

    < C, A >, < C, B >, < C, B, A, B >are subsequences.

    How many possible subsequences for a n-element sequence?

    Longest Common SubsequenceLongest Common Subsequence

  • 8/6/2019 Dynamic Programming - Algorithm

    47/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Longest Common SubsequenceLongest Common SubsequenceProblemProblem

    ZZ is ais a commoncommon subsequence of sequencessubsequence of sequences XX andand YY ififZZ is ais a

    subsequence of bothsubsequence of both XX andand YY..

    Example:Example:

    Let X = < A, B, C, B, D, A, B >X = < A, B, C, B, D, A, B >and Y = < B, D, C, A, B, A >Y = < B, D, C, A, B, A >

    < A >, , ,< A >, , , andare common subsequences.

    < C, A >< C, A >is, but< A, C >< A, C >is not.

    < B, C, A>< B, C, A>is a common subsequence

    < B, C, B, A >< B, C, B, A >is the longest common subsequence.

    Example:Example:

    xx = = ssarariieempmpioiollcceewewe

    yy = we= wessttiiggmmuuppsasallrtrtee

    Longest Common SubsequenceLongest Common Subsequence

  • 8/6/2019 Dynamic Programming - Algorithm

    48/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Longest Common SubsequenceLongest Common SubsequenceProblemProblem

    LCSLCS::

    InputInput: two sequences: two sequences x[1..x[1..mm]] andand y[1..y[1..nn]]

    OutputOutput: longest common subsequence of: longest common subsequence ofxx andand yy(denoted(denoted

    LCS(x,y)LCS(x,y)))

    Brute-force algorithm:Brute-force algorithm:

    For every subsequence ofxx, check if it is a subsequence ofyy

    22mm subsequences ofxx to check against nnelements ofyy : O(O(n 2n 2mm))

    LCS O ti l S b t tLCS: Optimal Sub structure

  • 8/6/2019 Dynamic Programming - Algorithm

    49/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    LCS: Optimal Sub-structureLCS: Optimal Sub-structure

    TheThe iiththprefixprefixofofX = < xX = < x11,x,x22,,x,,xmm >>is denotedis denoted XXii

    = < x= < x11,x,x22,,x,,xii >>

    XX00is the emptyemptysequence

    XXmmis the wholewhole sequence XX

    Theorem 15.1 (Optimal Sub-structure of LCS)Theorem 15.1 (Optimal Sub-structure of LCS)

    LetLet X=, andand

    Z=

    bebe

    LCS(X,Y)LCS(X,Y)

    .

    .

    (1) Ifxxmm= y= ynn, then zzkk = x= xmm = y= ynn and ZZk-1k-1 is LCS(XLCS(Xm-1m-1 ,YYn-1n-1 ))

    (2) ifxxmm y ynn, then zzkk x xmmimplies ZZ is LCS(XLCS(Xm-1m-1 ,Y),Y)

    (3) ifxxmm y ynn, then zzkk y ynn implies ZZ is LCS(X,YLCS(X,Yn-1n-1 ))

  • 8/6/2019 Dynamic Programming - Algorithm

    50/63

    LCS Optimal S bstr ct reLCS: Optimal Substructure

  • 8/6/2019 Dynamic Programming - Algorithm

    51/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    LCS: Optimal SubstructureLCS: Optimal Substructure

    LCSLCS has an optimal sub-structure defined byhas an optimal sub-structure defined byprefixesprefixesofof

    XX andand YY

    The sub-problems of findingThe sub-problems of finding LCS(XLCS(Xm-1m-1 ,Y,Ynn)) andand

    LCS(XLCS(Xmm,Y,Yn-1n-1 )) share a common sub-sub-problem ofshare a common sub-sub-problem of

    findingfinding LCS(XLCS(Xm-1m-1 ,Y,Yn-1n-1 )). They are overlapping.. They are overlapping.

    Simplify:Simplify: just worry aboutjust worry about LCSLCS length for nowlength for now

    Define c[i, j]c[i, j]= length ofLCS(XLCS(Xii, Y, Yjj))

    So c[m, n]c[m, n]= length ofLCS(X, Y)LCS(X, Y)

    LCS: RecurrenceLCS: Recurrence

  • 8/6/2019 Dynamic Programming - Algorithm

    52/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    LCS: RecurrenceLCS: RecurrenceDefineDefine c[i, j]c[i, j] = length of= length ofLCSLCS ofofx[1..i],x[1..i],

    y[1..j]y[1..j]

    Note that the conditions in the problem restrict sub-Note that the conditions in the problem restrict sub-

    problems (ifproblems (ifxxii = y= yii we considerwe considerxxi-1i-1 andand yyi-1i-1, etc), etc)

    UseUseb[i, j]b[i, j] to remember where to extract anto remember where to extract an

    element of anelement of an LCSLCS..

    0 if 0 or 0

    [ , ] [ 1, 1] 1 if , 0 and

    max{ [ , 1], [ 1, ]} if , 0 and

    i j

    i j

    i j

    c i j c i j i j x

    c i j c i j i j x

    = =

    = + >

    >

    LCS: RecurrenceLCS: Recurrence

  • 8/6/2019 Dynamic Programming - Algorithm

    53/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    LCS: RecurrenceLCS: Recurrenceintint lcsRec(lcsRec(intint i,i, intint j) {j) {

    ifif (i==0 || j==0) return 0;(i==0 || j==0) return 0;

    else ifelse if (x[i] == y[j])(x[i] == y[j])

    returnreturn lcsRec(i-1,j-1) + 1;lcsRec(i-1,j-1) + 1;

    elseelse

    returnreturn max(lcsRec(i-1,j),lcsRec(i,j-1));max(lcsRec(i-1,j),lcsRec(i,j-1));

    }}

    intint lcsRec(lcsRec(intint i,i, intint j) {j) {

    ifif (i==0 || j==0) return 0;(i==0 || j==0) return 0;

    else ifelse if (x[i] == y[j])(x[i] == y[j])

    returnreturn lcsRec(i-1,j-1) + 1;lcsRec(i-1,j-1) + 1;

    elseelse

    returnreturn max(lcsRec(i-1,j),lcsRec(i,j-1));max(lcsRec(i-1,j),lcsRec(i,j-1));

    }}

    Recurrence for time complexity:

    T(2n)=T(2n-2)+1 if x[n]=y[n]

    T(2n)=2T(2n-1) if x[n]!=y[n]T(2n)=1 if n=0

    T(2n)=2T(2n-1)

    =2(2T(2n-2))

    =4T(2n-2)

    =4(2T(2n-3))

    =8T(2n-3)

    =2iT(2n-i)

    = 2

    2n

    = 4

    n

    LCS: RecurrenceLCS: Recurrence

  • 8/6/2019 Dynamic Programming - Algorithm

    54/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    LCS: RecurrenceLCS: Recurrenceintint lcsMemo(lcsMemo(intint i,i, intint j) {j) { ifif (c[i][j] != -1)(c[i][j] != -1) returnreturn c[i][j]c[i][j]

    else ifelse if (x[i] == y[j]) {(x[i] == y[j]) {c[i][j] = lcsMemo(i-1,j-1) + 1c[i][j] = lcsMemo(i-1,j-1) + 1

    returnreturn c[i][j]c[i][j]}}

    elseelse {{

    c[i][j]=max(lcsMemo(i-1,j),lcsMemo(i,j-1))c[i][j]=max(lcsMemo(i-1,j),lcsMemo(i,j-1)) returnreturn c[i][j]c[i][j]}}

    }}

    intint lcsMemo(lcsMemo(intint i,i, intint j) {j) { ifif (c[i][j] != -1)(c[i][j] != -1) returnreturn c[i][j]c[i][j]

    else ifelse if (x[i] == y[j]) {(x[i] == y[j]) {c[i][j] = lcsMemo(i-1,j-1) + 1c[i][j] = lcsMemo(i-1,j-1) + 1

    returnreturn c[i][j]c[i][j]}}

    elseelse {{

    c[i][j]=max(lcsMemo(i-1,j),lcsMemo(i,j-1))c[i][j]=max(lcsMemo(i-1,j),lcsMemo(i,j-1)) returnreturn c[i][j]c[i][j]}}

    }}

    T(n) = O(n2)c String x

    String

    y

    LCS: Computing LengthLCS: Computing Length

  • 8/6/2019 Dynamic Programming - Algorithm

    55/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    LCS: Computing LengthLCS: Computing Length

    LCS-Length(X, Y, m, n)

    1 for i1 to m do2 c[i,0] 0

    3 for j0 to n do

    4 c[0,j] 0

    5 for i1 to m do

    6 for j1 to n do

    7 if xi= y

    jthen

    8 c[i,j] c[i-1,j-1]+1

    9 b[i,j] copy

    10 else if c[i-1,j] c[i,j-

    1] then

    11 c[i,j] c[i-1,j]

    12 b[i,j] skipX

    13 else

    14 c[i,j] c[i,j-1]

    15 b[i,j] skipY

    16 return c, b

    LCS-Length(X, Y, m, n)

    1 for i1 to m do

    2 c[i,0] 0

    3 for j0 to n do

    4 c[0,j] 0

    5 for i1 to m do

    6 for j1 to n do

    7 if xi = yjthen8 c[i,j] c[i-1,j-1]+1

    9 b[i,j] copy

    10 else if c[i-1,j] c[i,j-

    1] then

    11 c[i,j] c[i-1,j]12 b[i,j] skipX

    13 else

    14 c[i,j] c[i,j-1]

    15 b[i,j] skipY

    16 return c, b

    LCS: ExampleLCS: Example

  • 8/6/2019 Dynamic Programming - Algorithm

    56/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    00 00 00 00 00 00 00 00

    aa bb bb cc aa aa cc

    aa

    cc

    cc

    bb

    cc

    cc

    aa

    00

    00

    00

    00

    00

    00

    00

    XX

    YY

    LCS: ExampleLCS: Example0, if i=0, j=00, if i=0, j=0

    c[i,j] = c[i-1,j-1]+1 if i,j>0 and xc[i,j] = c[i-1,j-1]+1 if i,j>0 and xii = y= yjj

    max ( c[i,j-1], c[i-1,j] ) if i,j>0 and xmax ( c[i,j-1], c[i-1,j] ) if i,j>0 and xii yyii

    LCS: ExampleLCS: Example

  • 8/6/2019 Dynamic Programming - Algorithm

    57/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    00 00 00 00 00 00 00 00

    aa bb bb cc aa aa cc

    aa

    cc

    cc

    bb

    cc

    cc

    aa

    00

    00

    00

    00

    00

    00

    00

    LCS: ExampleLCS: Example0, if i=0, j=00, if i=0, j=0

    c[i,j] = c[i-1,j-1]+1 if i,j>0 and xc[i,j] = c[i-1,j-1]+1 if i,j>0 and xii = y= yjj

    max ( c[i,j-1], c[i-1,j] ) if i,j>0 and xmax ( c[i,j-1], c[i-1,j] ) if i,j>0 and xii yyii

    LCS: ExampleLCS: Example

  • 8/6/2019 Dynamic Programming - Algorithm

    58/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    00 00 00 00 00 00 00 00

    aa bb bb cc aa aa cc

    aa

    cc

    cc

    bb

    cc

    cc

    aa

    00

    00

    00

    00

    00

    00

    00

    LCS: ExampleLCS: Example0, if i=0, j=00, if i=0, j=0

    c[i,j] = c[i-1,j-1]+1 if i,j>0 and xc[i,j] = c[i-1,j-1]+1 if i,j>0 and xii = y= yjj

    max ( c[i,j-1], c[i-1,j] ) if i,j>0 and xmax ( c[i,j-1], c[i-1,j] ) if i,j>0 and xii yyii

    LCS: ExampleLCS: Example

  • 8/6/2019 Dynamic Programming - Algorithm

    59/63

    0, if i=0, j=00, if i=0, j=0

    c[i,j] = c[i-1,j-1]+1 if i,j>0 and xc[i,j] = c[i-1,j-1]+1 if i,j>0 and xii = y= yjj

    max(c[i,j-1],c[i-1,j]) if i,j>0 and xmax(c[i,j-1],c[i-1,j]) if i,j>0 and xii yyii

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    00 00

    11

    11

    11

    00 00

    11 11

    11 11

    11 11

    11

    11

    11

    11

    22 22

    22 22

    22 22

    22 22

    00 00

    11 11

    22 22

    22 22

    00 00

    11 11

    22 22

    22 33

    22 22

    33 33

    33 33

    33 44

    22 33

    33 33

    33 44

    44 44

    aa bb bb cc aa aa cc

    aa

    cc

    cc

    bb

    cc

    cc

    aa

    00

    00

    00

    00

    00

    00

    00

    LCS: ExampleLCS: Example

    Constructing an LCSConstructing an LCS

  • 8/6/2019 Dynamic Programming - Algorithm

    60/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    Constructing an LCSConstructing an LCSPrint-LCS(b, X, i, j)

    1 if i = 0 or j = 0 then

    2 return

    3 if b[i,j] = copy" then

    4 Print-LCS(b,X,i-1,j-1)

    5 print x[i]

    6 elseif b[i,j]=skipX" then

    7 Print-LCS(b,X,i-1,j)

    8 else

    Print-LCS(b,X,i,j-1)

    Print-LCS(b, X, i, j)

    1 if i = 0 or j = 0 then

    2 return3 if b[i,j] = copy" then

    4 Print-LCS(b,X,i-1,j-1)

    5 print x[i]

    6 elseif b[i,j]=skipX" then7 Print-LCS(b,X,i-1,j)

    8 else

    Print-LCS(b,X,i,j-1)

    length[X] = m, length[Y] = n,

    Call Print-LCS(b, X, n, m) to construct LCS

    Time complexity: O(m+n).

    LCS: ExampleLCS: Example

  • 8/6/2019 Dynamic Programming - Algorithm

    61/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    0 0

    11

    1

    0 0

    1 11 1

    1 1

    1

    1

    1

    1

    2 2

    2 2

    2 2

    2 2

    0 0

    1 12 2

    2 2

    0 0

    1 12 2

    2 3

    2 2

    3 3

    3 3

    3 4

    2 3

    3 3

    3 4

    4 4

    a b b c a a c

    ac

    c

    b

    c

    c

    a

    00

    0

    0

    0

    0

    0

    LCS: ExampleLCS: Example0, if i=0, j=00, if i=0, j=0

    c[i,j] = c[i-1,j-1]+1 if i,j>0 and xc[i,j] = c[i-1,j-1]+1 if i,j>0 and xii = y= yjj

    max(c[i,j-1],c[i-1,j]) if i,j>0 and xmax(c[i,j-1],c[i-1,j]) if i,j>0 and xii yyii

    onges ommononges ommon

  • 8/6/2019 Dynamic Programming - Algorithm

    62/63

    Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming

    onges ommononges ommonSubsequenceSubsequence

    There is a need to quantify how similar theyThere is a need to quantify how similar they

    are:are:Comparing DNA sequences in studies of evolutionof different species

    Spell checkers

    Editing

    References & ReadingsReferences & Readings

  • 8/6/2019 Dynamic Programming - Algorithm

    63/63

    References & ReadingsReferences & Readings

    CLRS:CLRS:

    15.2, 15.3, 15.4

    Exercises: 15.2-1, 15.2-2, 15.2-3, 15.3-2, 15.4-1,

    15.4-3, 15.4-5, 15.4-6.

    Problems: 15-3.

    HSR:HSR:

    5.6