22
1 CS4311 Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV

CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

1

CS4311Design and Analysis of

Algorithms

Lecture 12: Dynamic Programming IV

Page 2: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

2

Subsequence of a String

•Let S = s1s2…sm be a string of length m•Any string of the form

si1si2

…sikwith i1 i2 …ik is a subsequence of S

•E.g., if S = farmers fame, arm, mrs, farmers, are some of

the subsequences of S

Page 3: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

3

Longest Common Subsequence

•Let S and T be two strings•If a string is both

•a subsequence of S and•a subsequence of T,

it is a common subsequence of S and T

•In addition, if it is the longest possibleone, it is a longest common subsequence

Page 4: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

4

Longest Common Subsequence

•E.g.,S = algorithmsT = logarithms

•Then, aim, lots, ohms, grit, are some ofthe common subsequences of S and T

•Longest common subsequences:lorithms , lgrithms

Page 5: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

5

Longest Common Subsequence

•Let S = s1s2…sm be a string of length m•Let T = t1t2…tn be a string of length n

Can we quickly find a longest commonsubsequence (LCS) of S and T ?

Page 6: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

6

Let X = x1x2…xk be an LCS ofS1,i = s1 s2…si and T1,j = t1t2…tj.

Lemma:•If si = tj, then xk = si = tj, and x1x2…xk-1

must be the LCS of S1,i-1 and T1,j-1

•If si tj, then X must either be(i) an LCS of S1,i and T1,j-1 , or(ii) an LCS of S1,i-1 and T1,j

Optimal Substructure

Page 7: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

7

Let leni,j = length of the LCS of S1,i and T1,j

Optimal Substructure

Lemma: For any i, j ¸ 1,• if si = tj, leni,j = leni-1,j-1 + 1

• if si tj, leni,j = max { leni,j-1 , leni-1,j }

Page 8: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

8

Define a function Compute_L(i,j) as follows:Compute_L(i, j) /* Finding leni,j */

1. if (i == 0 or j == 0 ) return 0; /* base case */

2. if (si == tj)return Compute_L(i-1,j-1) + 1;

3. elsereturn max {Compute_L(i-1,j), Compute_L(i,j-1)};

Length of LCS

Compute_L(m, n) runs in O(2m+n) time

Page 9: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

9

Overlapping SubproblemsTo speed up, we can see that :

To Compute_L(i,j) and Compute_L(i-1,j+1),has a common subproblem:

Compute_L(i-1,j)

In fact, in our recursive algorithm, there aremany redundant computations !

Question: Can we avoid it ?

Page 10: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

10

Bottom-Up Approach•Let us create a 2D table L to store all

leni,j values once they are computedBottomUp_L( ) /* Finding min #operations */

1. For all i and j, set L[i,0] = L[0, j] = 0;2. for (i = 1,2,…, m)

Compute L[i,j] for all j;// Based on L[i-1,j-1], L[i-1,j], L[i,j-1]

4. return L[m,n] ;

Running Time = (mn)

Page 11: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

11

Remarks•Again, a slight change in the algorithm

allows us to obtain a particular LCS

•Also, we can make minor changes to therecursive algorithm and obtain a memoizedversion (whose running time is O(mn))

Page 12: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

12MOORYTRID

YROTIMROD

000000000

0000000000

Example Run: After Step 1

Page 13: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

13MOORYTRID

YROTIMROD

00000000

11111111100000000000

Example Run: After Step 2, i = 1

Page 14: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

14MOORYTRID

YROTIMROD

0000000

222221111011111111100000000000

Example Run: After Step 2, i = 2

Page 15: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

15MOORYTRID

YROTIMROD

000000

3322222110222221111011111111100000000000

Example Run: After Step 2, i = 3

Page 16: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

16MOORYTRID

YROTIMROD

00000

33332221103322222110222221111011111111100000000000

Example Run: After Step 2, i = 4

Page 17: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

17MOORYTRID

YROTIMROD

4443332210444322221044432222104433222110433322211033332221103322222110222221111011111111100000000000

Example Run: After Step 2

Page 18: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

18MOORYTRID

YROTIMROD

00000000

11111111100000000000

Extra information to obtain an LCS

Page 19: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

19MOORYTRID

YROTIMROD

0000000

222221111011111111100000000000

Extra Info: After Step 2, i = 2

Page 20: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

20MOORYTRID

YROTIMROD

000000

3322222110222221111011111111100000000000

Extra Info: After Step 2, i = 3

Page 21: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

21MOORYTRID

YROTIMROD

4443332210444322221044432222104433222110433322211033332221103322222110222221111011111111100000000000

Extra Info: After Step 2

Page 22: CS4311 Design and Analysis of Algorithmswkhon/ds/ds12/lecture/tutorial-dp.pdf · Design and Analysis of Algorithms Lecture 12: Dynamic Programming IV. 2 Subsequence of a String •Let

22MOORYTRID

YROTIMROD

4443332210444322221044432222104433222110433322211033332221103322222110222221111011111111100000000000

LCS obtained by tracing from L[m,n]