7
Longest Common Subsequence Longest Common Subsequence Let’s look at another dynamic programming example: Longest Common Subsequence (LCS): Let X = x 1 ,x 2 ,...,x m be a sequence. Then, another sequence Z = z 1 ,...,z k is a subsequence of X if there exists a strictly increasing sequence of indices i 1 ,...,i k such that z j = x i j for all 1 j k. Given two sequences X and Y , a third sequence Z is a common subsequence of both X and Y if it is a subsequence of X and a subsequence of Y . CS404/504 Computer Science 1 Design and Analysis of Algorithms: Lecture 18

Longest Common Subsequence Longest Common Subsequence

  • Upload
    dangnhu

  • View
    230

  • Download
    7

Embed Size (px)

Citation preview

Page 1: Longest Common Subsequence Longest Common Subsequence

Longest Common SubsequenceLongest Common Subsequence

Let’s look at another dynamic programming example:

Longest Common Subsequence (LCS):

Let X = 〈x1, x2, . . . , xm〉 be a sequence. Then, another

sequence Z = 〈z1, . . . , zk〉 is a subsequence of X if there exists a

strictly increasing sequence of indices i1, . . . , ik such that

zj = xij for all 1 ≤ j ≤ k.

Given two sequences X and Y , a third sequence Z is a common

subsequence of both X and Y if it is a subsequence of X and a

subsequence of Y .

'

&

$

%CS404/504 Computer Science

1Design and Analysis of Algorithms: Lecture 18

Page 2: Longest Common Subsequence Longest Common Subsequence

ExamplesExamples

Consider the sequence X = 〈A, B, C, B, D, A, B〉. Then, the

sequence Z = 〈B, B, A, B〉 is a subsequence of X.

Similarly, let

X = 〈A, B, C, B, D, A, B, C, D〉

and

Y = 〈B, A, C, A, D, B, C, A, A, A〉.

Then, Z = 〈A, C, D〉 is a common subsequence of X and Y .

What is the longest common subsequence of X and Y ?

'

&

$

%CS404/504 Computer Science

2Design and Analysis of Algorithms: Lecture 18

Page 3: Longest Common Subsequence Longest Common Subsequence

Step (i): Optimal SubstructureStep (i): Optimal Substructure

Let X = < x1, x2, ..., xm > and Y = < y1, y2, ..., yn > be two

sequences, and let Z = < z1, z2, ..., zk > be a LCS of X and Y.

Then:

• if xm = yn, then zk = xm = yn and Zk−1 is a LCS of Xm−1

and Yn−1 (zk has to be equal to xm/yn, otherwise Z won’t

be a LCS).

• if xm 6= yn, then:

– zk 6= xm ⇒ Z is an LCS of Xm−1 and Y.

– zk 6= yn ⇒ Z is an LCS of X and Yn−1.

'

&

$

%CS404/504 Computer Science

3Design and Analysis of Algorithms: Lecture 18

Page 4: Longest Common Subsequence Longest Common Subsequence

Step (ii): A recursive solutionStep (ii): A recursive solution

Definition: Let c[i, j] be the length of the longest common

subsequence between Xi = 〈x1, . . . , xi〉 and Yj = 〈y1, . . . , yj〉.

Then c[n, m] contains the length of an LCS of X and Y, and:

c[i, j] =

0 if i = 0 or j = 0

c[i− 1, j − 1] + 1 if i, j > 0 and xi = yj

max(c[i− 1, j], c[i, j − 1]) otherwise.

'

&

$

%CS404/504 Computer Science

4Design and Analysis of Algorithms: Lecture 18

Page 5: Longest Common Subsequence Longest Common Subsequence

Step (iii): Bottom-up iterative computationStep (iii): Bottom-up iterative computation

LCS(X,Y,m,n) /*X has m elements, Y has n elements */

for i = 1 to m c[i,0] := 0;

for j = 1 to n c[0,j] := 0;

for i = 1 to m

for j = 1 to n

if X[i] == Y[j]

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

b[i,j] := ′′տ′′;

else

if c[i-1,j] ≥ c[i,j-1]

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

b[i,j] := ′′ ↑′′;

else

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

b[i,j] := ′′←′′;

'

&

$

%CS404/504 Computer Science

5Design and Analysis of Algorithms: Lecture 18

Page 6: Longest Common Subsequence Longest Common Subsequence

Step (iv): Figuring out the LCSStep (iv): Figuring out the LCS

Use a recursive algorithm: b[i, j] points to the table entry

corresponding to the optimal subproblem solution chosen when

computing c[i, j].

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

if i = 0 return;

if j = 0 return;

if b[i, j] = “տ′′

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

print X[i];

else if b[i, j] = “ ↑′′

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

else

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

'

&

$

%CS404/504 Computer Science

6Design and Analysis of Algorithms: Lecture 18

Page 7: Longest Common Subsequence Longest Common Subsequence

An ExampleAn Example

Consider the following example:

X = 〈A, B, C, B, D, A, B〉

and

Y = 〈B, D, C, A, B, A〉.

Let’s compute c and b on the board. Then, we’ll compute the

LCS.

'

&

$

%CS404/504 Computer Science

7Design and Analysis of Algorithms: Lecture 18