52
. . . . . . 1/1 Algorithms and Data Structures 10th Lecture: Dynamic Programming Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming Chu University of Aizu Last Updated: 2020/12/1 Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu Algorithms and Data Structures

Algorithms and Data StructuresAlgorithms and Data Structures 10th Lecture: Dynamic Programming Yutaka Watanobe, Jie Huang, Yan Pei, Wenxi Chen, Qiangfu Zhao, Wanming Chu University

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

  • . . . . . . 1/1

    Algorithms and Data Structures10th Lecture: Dynamic Programming

    Yutaka Watanobe, Jie Huang, Yan Pei,Wenxi Chen, Qiangfu Zhao, Wanming Chu

    University of Aizu

    Last Updated: 2020/12/1

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 2/1

    Outline

    Dynamic ProgrammingRecursive vs. Dynamic ProgrammingMatrix Chain MultiplicationLongest Common Subsequence

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 3/1

    Dynamic Programming

    Dynamic programming solves a problem by combining thesolutions to subproblems. (The word ”programming” does notmean writing computer code)The development of a dynamic programming algorithm can bebroken into a sequence of four steps:

    1 Characterize the structure of an optimal solution.2 Recursively define the value of an optimal solution.3 Compute the value of an optimal solution in a bottom-up fashion.4 Construct an optimal solution from computed information.

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 4/1

    Recursive vs. Dynamic Programming: FibonacciNumber (1)

    Fibonacci Numbers: fn = 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . .A recursive formula for Fibonacci Numbers:

    f (n) =

    1 if n = 01 if n = 1f (n − 2) + f (n − 1)

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 5/1

    Recursive vs. Dynamic Programming: FibonacciNumber (2)

    A recursive program to calculate the n-th fibonacci number.

    fibonacci(n)

    if n == 0 || n == 1

    return 1

    return fibonacci(n - 2) + fibonacci(n - 1)

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 6/1

    Recursive vs. Dynamic Programming: FibonacciNumber (3)

    A function f (5) calls f (4) and f (3), but f (4) calls f (3) again.

    f(5)

    f(2)

    f(1) f(0)

    f(3)

    f(2) f(1)

    f(2)

    f(1) f(0)

    f(4)

    f(1) f(0)

    f(3)

    f(1)

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 7/1

    Recursive vs. Dynamic Programming: FibonacciNumber (4)

    A recursive program with memorization to calculate the n-thfibonacci number.

    fibonacci(n)

    if n == 0 || n == 1

    return F[n] = 1

    if F[n] is already defined

    return F[n]

    return F[n] = fibonacci(n - 2) + fibonacci(n - 1)

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 8/1

    Recursive vs. Dynamic Programming: FibonacciNumber (5)

    The generation of Fibonacci Numbers with memorization.

    f(5)

    F[2]f(3)

    f(2) F[1]

    f(4)

    f(1) f(0)

    F[3]

    F[1] =

    F[2] =

    F[3] =

    F[4] =

    F[5] =

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 9/1

    Recursive vs. Dynamic Programming: FibonacciNumber (6)

    The generation of Fibonacci Numbers by Dynamic Programming(implementation by a loop).

    fibonacci(n)

    F[0] = 1

    F[1] = 1

    for i = 2 to n

    F[i] = F[i - 2] + F[i - 1]

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 10/1

    Recursive vs. Dynamic Programming: BinomialCoefficient (1)

    We want to compare two simple programs.Consider the binomial coefficient defined recursively as follows:for any natural numbers k ,n ∈ N,(

    n0

    )=

    (nn

    )= 1

    (nk

    )=

    (n − 1

    k

    )+

    (n − 1k − 1

    )

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 11/1

    Recursive vs. Dynamic Programming: BinomialCoefficient (2)

    Pascal’s triangle

    n = 0 1

    n = 1 1 1

    n = 2 1 2 1

    n = 3 1 3 3 1

    n = 4 1 4 6 4 1

    n = 5 1 5 10 10 5 1

    n = 6 1 6 15 20 15 6 1

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 12/1

    Recursive vs. Dynamic Programming: BinomialCoefficient (3)

    recursiveBinomial(n, k)

    if k == 0 or k == n

    return 1

    else

    u = recursiveBinomial(n-1, k)

    v = recursiveBinomial(n-1, k-1)

    return u + v

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 13/1

    Recursive vs. Dynamic Programming: BinomialCoefficient (4)

    The following dynamic programming reduces the running time ofa recursive function.

    dynamicBinomial(n, k)

    if dp[n, k] is already defined

    return dp[n, k]

    else

    if k == 0 or k == n

    dp[n, k] = 1

    else

    u = dynamicBiomial(n-1, k)

    v = dynamicBiomial(n-1, k-1)

    dp[n, k] = u + v

    return dp[n, k]

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 14/1

    Matrix-chain Multiplication (1)

    We can multiply two matrices A and B only if they are compatible.If A is an l × m matrix and B is an m × n matrix, the resultingmatrix C is an l × n matrix.The time to compute C is dominated by the number of scalarmultiplications l × m × n.

    l

    m

    m

    n

    n

    l

    A B C

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 15/1

    Matrix-chain Multiplication (2)

    We are given a sequence (chain) {A1,A2, ...,An} of n matrices tobe multiplied, and we wish to compute the product A1A2...An.We can evaluate the expression using the standard algorithm formultiplying pairs of matrices as a subroutine once we haveparenthesized it to resolve all ambiguities in how the matricesare multiplied together.A product of matrices is fully parenthesized if it is either a singlematrix or the product of two fully parenthesized matrix products,surrounded by parentheses.

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 16/1

    Matrix-chain Multiplication (3)

    Matrix multiplication is associative, and so all parenthesizationyield the same product.For example, if the chain of matrices is {A1,A2,A3,A4}, theproduct A1A2A3A4 can be fully parenthesized in five distinctways:

    (A1(A2(A3A4)))(A1((A2A3)A4))((A1A2)(A3A4))((A1(A2A3))A4)(((A1A2)A3)A4)

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 17/1

    Matrix-chain Multiplication: Example 1 (1)

    Matrix-chain multiplication of matrices A1A2...A6 where Ai is api−1 × pi matrix.

    p0

    p1p2 p3 p4 p5 p6

    A 1 A 2 A 3 A 4 A 5 A 6

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 18/1

    Matrix-chain Multiplication: Example 1 (2)

    Possible parenthesization: From the left to the right.

    4 x 2 x 3 = 24

    4 x 3 x 1 = 12

    4 x 1 x 2 = 8

    4 x 2 x 2 = 16

    4 x 2 x 3 = 24

    24 + 12 + 8 + 16 + 24 = 84

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 19/1

    Matrix-chain Multiplication: Example 1 (3)

    Possible parenthesization: From the right to the left.

    2 x 2 x 3 = 12

    1 x 2 x 3 = 6

    3 x 1 x 3 = 9

    2 x 3 x 3 = 18

    4 x 2 x 3 = 24

    12 + 6 + 9 + 18 + 24 = 69

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 20/1

    Matrix-chain Multiplication: Example 1 (4)

    Possible parenthesization: the optimal solution.

    2 x 3 x 1 = 6

    1 x 2 x 2 = 4

    4 x 2 x 1 = 8

    1 x 2 x 3 = 6

    4 x 1 x 3 = 12

    6 + 4 + 8 + 6 + 12 = 36

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 21/1

    Matrix-chain Multiplication: Example 2

    Consider the problem of a chaining {A1,A2,A3}. Suppose thatthe dimensions of the matrices are 10× 100, 100× 5, and 5× 50respectively.

    ((A1A2)A3)cost of (A1A2) = A12 : 10 × 100 × 5 = 5000cost of (A12A3) : 10 × 5 × 50 = 2500cost of ((A1A2)A3) : 5000 + 2500 = 7500

    (A1(A2A3))cost of (A2A3) = A23 : 100 × 5 × 50 = 25000cost of (A1A23) : 10 × 100 × 50 = 50000cost of (A1(A2A3)) : 25000 + 50000 = 75000

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 22/1

    The Structure of an Optimal Parenthesization (1)

    Let us adopt the notation Ai..j , where i ≤ j , for the matrix thatresults from evaluating the product AiAi+1..Aj .If the problem is nontrivial, i.e., i < j , then any parenthesizationof the product AiAi+1...Aj must split the product between Ak andAk+1 for some integer k in the range i ≤ k < j .(AiAi+1...Ak )(Ak+1....Aj)

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 23/1

    The Structure of an Optimal Parenthesization (2)

    For some value of k , we first compute the matrices Ai..k andAk+1..j and then multiply them together to produce the finalproduct Ai..j .The cost of this parenthesization is thus the cost of computingthe matrix Ai..k , plus the cost of computing Ak+1..j , plus the costof multiplying them together.

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 24/1

    A Recursive Solution

    Let m[i , j] be the minimum number of scalar multiplicationsneeded to compute the matrix Ai..j ; for the full problem, the costof a cheapest way to compute A1..n would thus be m[1,n].We can define m[i , j] recursively as follows.

    m[i , j] ={

    0 if i = jmini≤k

  • . . . . . . 25/1

    A Recursive Solution: Implementation

    matrixChainOrder(p)

    n = p.length-1

    for i = 1 to n

    m[i, i] = 0

    for l = 2 to n

    for i = 1 to n-l+1

    j = i+l-1

    m[i, j] = INF

    for k = i to j-1

    q = m[i,k] + m[k+1, j] + p[i-1]*p[k]*p[j]

    m[i,j] = min(m[i,j], q)

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 26/1

    Computing the Optimal Costs (1)

    4 3 5 1 6

    p0 p1 p2 p3 p4

    1

    2

    3

    4 1

    2

    3

    4

    j i

    m[i,j]

    A1 A2 A3 A4

    0 0 0 0

    Initializationm[i , i] = 0

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 27/1

    Computing the Optimal Costs (2)

    4 3 5 1 6

    p0 p1 p2 p3 p4

    1

    2

    3

    4 1

    2

    3

    4

    j i

    m[i,j]

    A1 A2 A3 A4

    0 0 0 0

    60

    m[1,2] = min1≤k

  • . . . . . . 28/1

    Computing the Optimal Costs (3)

    4 3 5 1 6

    p0 p1 p2 p3 p4

    1

    2

    3

    4 1

    2

    3

    4

    j i

    m[i,j]

    A1 A2 A3 A4

    0 0 0 0

    60 15

    m[2,3] = min2≤k

  • . . . . . . 29/1

    Computing the Optimal Costs (4)

    4 3 5 1 6

    p0 p1 p2 p3 p4

    1

    2

    3

    4 1

    2

    3

    4

    j i

    m[i,j]

    A1 A2 A3 A4

    0 0 0 0

    60 15 30

    m[3,4] = min3≤k

  • . . . . . . 30/1

    Computing the Optimal Costs (5)

    4 3 5 1 6

    p0 p1 p2 p3 p4

    1

    2

    3

    4 1

    2

    3

    4

    j i

    m[i,j]

    A1 A2 A3 A4

    0 0 0 0

    60 15 30

    27

    m[1,3] = min1≤k

  • . . . . . . 31/1

    Computing the Optimal Costs (6)

    4 3 5 1 6

    p0 p1 p2 p3 p4

    1

    2

    3

    4 1

    2

    3

    4

    j i

    m[i,j]

    A1 A2 A3 A4

    0 0 0 0

    60 15 30

    27 33

    m[2,4] = min2≤k

  • . . . . . . 32/1

    Computing the Optimal Costs (7)

    4 3 5 1 6

    p0 p1 p2 p3 p4

    1

    2

    3

    4 1

    2

    3

    4

    j i

    m[i,j]

    A1 A2 A3 A4

    0 0 0 0

    60 15 30

    27 33

    51

    m[1,4] = min1≤k

  • . . . . . . 33/1

    Computing the Optimal Costs: Example 2 (1)

    15,125

    11,875 10,500

    9,375 7,125

    7,875 4,375 2,500 3,500

    5,375

    15,750 2,625 750 1,000

    0 0 0 0 0 0

    5,0001

    2

    3

    4

    5

    6 1

    2

    3

    4

    5

    6

    A1 A2 A3 A4 A5 A6

    30×35 35×15 15×5 5×10 10×20 20×25

    ij

    m

    p 30 35 15 5 10 20 25

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 34/1

    Computing the Optimal Costs: Example 2 (2)

    m[2,5] =

    min

    m[2,2] + m[3,5] + p1p2p5 = 0 + 2500 + 35 × 15 × 20 = 13000m[2,3] + m[4,5] + p1p3p5 = 2625 + 1000 + 35 × 5 × 20 = 7125m[2,4] + m[5,5] + p1p4p5 = 4375 + 0 + 35 × 10 × 20 = 11375= 7125

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 35/1

    Matrix-chain Multiplication by DP: Complexity

    A simple inspection of the nested loop structure ofmatrixChainOrder(p) yields a running time of O(n3) for thealgorithm.The loops are nested three deep, and each loop index (l , i , andk ) takes on at most n − 1 values.

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 36/1

    Longest Common Subsequence (LCS)

    Given two sequences X and Y , a sequence Z is a commonsubsequence of X and Y if Z is a subsequence of both X and Y .For example, if X = {A,B,C,B,D,A,B} andY = {B,D,C,A,B,A}, the sequence {B,C,A} is a commonsubsequence of both X and Y .The sequence {B,C,A} is not a longest common subsequence(LCS) of X and Y , since it has length 3 and the sequence{B,C,B,A}, which is also common to both X and Y , has length4.The sequence {B,C,B,A} is an LCS of X and Y , since there isno common subsequence of length 5 or greater.

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 37/1

    LCS: Recursive Solution (1)

    In the longest common subsequence problem, we are given twosequences X = {x1, x2, ..., xm} and Y = {y1, y2, ..., yn} and wishto find a maximum-length common subsequence of X and Y .To be precise, given a sequence X = {x1, x2, ..., xm}, we definethe i th prefix of X , for i = 0,1, ...,m, as Xi = {x1, x2, ..., xi}. X0 isthe empty sequence.There are either one or two subproblems to examine whenfinding an LCS of X = {x1, x2, ..., xm} and Y = {y1, y2, ..., yn}.if xm = yn, we must find an LCS of Xm−1 and Yn−1. Appendingxm = yn to this LCS yields an LCS of X and Y .if xm ̸= yn, then we must solve two subproblems: finding an LCSof Xm−1 and Y and finding an LCS of X and Yn−1. Whichever ofthese two LCS’s is longer is an LCS of X and Y .

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 38/1

    LCS: Recursive Solution (2)

    To find an LCS of X and Y , we may need to find the LCS’s of Xand Yn−1 and of Xm−1 and Y .But each of these subproblems has the subsubproblem of findingthe LCS of Xm−1 and Yn−1.Many other subproblems share subsubproblems.

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 39/1

    LCS: Recursive Solution (3)

    Let us define c[i , j] to be the length of an LCS of the sequencesXi and Yj .The optimal substructure of the LCS problem gives the recursiveformula:

    c[i , j] =

    0 if i = 0 or j = 0c[i − 1, j − 1] + 1 if i , j > 0 and xi = yjmax(c[i , j − 1], c[i − 1, j]) if i , j > 0 and xi ̸= yj

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 40/1

    Computing the Length of an LCS (1)

    B

    0

    0 0 0 0 0 0 0

    0

    0

    0

    0

    0

    0

    0

    D C A B A

    A

    B

    C

    B

    D

    A

    B

    1 2 3 4 5 6

    0

    1

    2

    3

    4

    5

    6

    7

    i

    j

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 41/1

    Computing the Length of an LCS (2)

    B

    0

    0 0 0 0 0 0 0

    0 0 0 0 1 1 1

    0

    0

    0

    0

    0

    0

    D C A B A

    A

    B

    C

    B

    D

    A

    B

    1 2 3 4 5 6

    0

    1

    2

    3

    4

    5

    6

    7

    i

    j

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 42/1

    Computing the Length of an LCS (3)

    B

    0

    0 0 0 0 0 0 0

    0 0 0 0 1 1 1

    0 1 1 1 1 2 2

    0

    0

    0

    0

    0

    D C A B A

    A

    B

    C

    B

    D

    A

    B

    1 2 3 4 5 6

    0

    1

    2

    3

    4

    5

    6

    7

    i

    j

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 43/1

    Computing the Length of an LCS (4)

    B

    0

    0 0 0 0 0 0 0

    0 0 0 0 1 1 1

    0 1 1 1 1 2 2

    0 1 1 2 2 2 2

    0

    0

    0

    0

    D C A B A

    A

    B

    C

    B

    D

    A

    B

    1 2 3 4 5 6

    0

    1

    2

    3

    4

    5

    6

    7

    i

    j

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 44/1

    Computing the Length of an LCS (5)

    B

    0

    0 0 0 0 0 0 0

    0 0 0 0 1 1 1

    0 1 1 1 1 2 2

    0 1 1 2 2 2 2

    0 1 1 2 2 3 3

    0

    0

    0

    D C A B A

    A

    B

    C

    B

    D

    A

    B

    1 2 3 4 5 6

    0

    1

    2

    3

    4

    5

    6

    7

    i

    j

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 45/1

    Computing the Length of an LCS (6)

    B

    0

    0 0 0 0 0 0 0

    0 0 0 0 1 1 1

    0 1 1 1 1 2 2

    0 1 1 2 2 2 2

    0 1 1 2 2 3 3

    0 1 2 2 2 3 3

    0

    0

    D C A B A

    A

    B

    C

    B

    D

    A

    B

    1 2 3 4 5 6

    0

    1

    2

    3

    4

    5

    6

    7

    i

    j

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 46/1

    Computing the Length of an LCS (7)

    B

    0

    0 0 0 0 0 0 0

    0 0 0 0 1 1 1

    0 1 1 1 1 2 2

    0 1 1 2 2 2 2

    0 1 1 2 2 3 3

    0 1 2 2 2 3 3

    0 1 2 2 3 3 4

    0

    D C A B A

    A

    B

    C

    B

    D

    A

    B

    1 2 3 4 5 6

    0

    1

    2

    3

    4

    5

    6

    7

    i

    j

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 47/1

    Computing the Length of an LCS (8)

    B

    0

    0 0 0 0 0 0 0

    0 0 0 0 1 1 1

    0 1 1 1 1 2 2

    0 1 1 2 2 2 2

    0 1 1 2 2 3 3

    0 1 2 2 2 3 3

    0 1 2 2 3 3 4

    0 1 2 2 3 4 4

    D C A B A

    A

    B

    C

    B

    D

    A

    B

    1 2 3 4 5 6

    0

    1

    2

    3

    4

    5

    6

    7

    i

    j

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 48/1

    Computing the Length of an LCS (9)

    B

    0

    0 0 0 0 0 0 0

    0 0 0 0 1 1 1

    0 1 1 1 1 2 2

    0 1 1 2 2 2 2

    0 1 1 2 2 3 3

    0 1 2 2 2 3 3

    0 1 2 2 3 3 4

    0 1 2 2 3 4 4

    D C A B A

    A

    B

    C

    B

    D

    A

    B

    1 2 3 4 5 6

    0

    1

    2

    3

    4

    5

    6

    7

    i

    j

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 49/1

    Dynamic Programming to Compute the Length of anLCS (1)

    Since there are only O(mn) distinct subproblems, we can usedynamic programming to compute the solutions bottom up.

    LCSLength(X, Y)

    m = X.length

    n = Y.length

    for i = 1 to m

    c[i,0] = 0

    for j = 0 to n

    c[0,j] = 0

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 50/1

    Dynamic Programming to Compute the Length of anLCS (2)

    for i = 1 to m

    for j = 1 to n

    if X[i] == Y[j]

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

    else if c[i-1,j] >= c[i, j-1]

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

    else

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

    Note that there may be more than one solution if we have twosub-problems with the optimal solution.

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 51/1

    LCS by DP: Complexity

    The running time of the procedure is O(mn), since each tableentry takes O(1) time to compute.

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures

  • . . . . . . 52/1

    Reference

    1 Introduction to Algorithms (third edition), Thomas H.Cormen,Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. TheMIT Press, 2012.

    Y. Watanobe, J. Huang, Y. Pei, W. Chen, Q. Zhao, W. Chu University of Aizu

    Algorithms and Data Structures