21 Longest Common Subsequence (LCS)
21 Longest Common Subsequence (LCS)
Subsequence:
Subset of letters in order from left to right.
Remember:
“Subsequence” is different from “substring”. Substring
is continuous, no gap. But, subsequence can have gap.
Paris 1
Example : Subsequence
X= {A B C B D A B } Y= {B D C A B A}
CBD is a substring of X. BBA is a subsequence of X,
but not a substring of X.
DBBB is not a subsequence of X, because order violation.
BDAB is both substring and subsequence of X,
but it is only a subsequence of Y
Paris 2
Example : LCS
Common Subsequence:
Subsequence of both.
Longest Common Subsequence:
Same subsequence of maximum length.
X= {A B C B D A B } Y= {B D C A B A}
X = A BCBD AB, Y = BD CA BA LCS: BCBA
(4)
Problem:
Given two sequences X = < x1, x2, ….., xm > and
Y = < y1, y2, ….., yn > find a
maximum-length common subsequence of X and Y.
Solution (Idea):
Notice that the LCS problem has optimal substructure:
solutions of subproblems are parts of the final solution.
Paris 4
Notations
• X = < x1, x2, ….., xm > and Y = < y1, y2, ..., yn > : Sequences
Paris 5
Optimal Substructure of an LCS
• If xm y n , then z k xm
implies that Z is an LCS of X m 1 and Y
• If xm y n , then z k y n
implies that Z is an LCS of X and Yn 1
Paris 6
Optimal Substructure of an LCS (Book: Cormen; Page-392)
Paris 7
The Recurrence
0 if i 0 or j 0
c[i, j ] c[i 1, j 1] 1 if i, j 0 and xi y j ,
max(c[i, j 1], c[i 1, j ]) if i, j 0 and x y
i j
Paris 8
LCS Recursive Solution
• We start with i = j = 0 (empty substrings of x
and y)
• Since X0 and Y0 are empty strings, their LCS is
always empty (i.e. c[0,0] = 0)
• LCS of empty string and any other string is
empty, so for every i and j: c[0, j] = c[i,0] = 0
Paris 9
LCS Recursive Solution
When we calculate c[i,j], we consider two cases:
• First case: x[i]=y[j]: one more symbol matches, so the
length of LCS Xi and Yj equals to the length of LCS of
smaller strings Xi-1 and Yi-1 , plus 1
• Second case: x[i] != y[j]: As symbols do not match,
solution is not improved, and the length of LCS(Xi , Yj)
is the same as before (maximum of LCS (Xi, Yj-1) and
LCS (Xi-1,Yj) )
Paris 10
LCS Example
X = ABCB
Y = BDCAB
Paris 11
LCS Example (0)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
A
1
2 B
3 C
4 B
X = ABCB; m = |X| = 4
Y = BDCAB; n = |Y| = 5
Allocate array c[6,5]
Paris 12
LCS Example (1)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
A
1 0
2 B
0
3 C 0
4 B 0
for i = 1 to m c[i,0] = 0
Paris 13
LCS Example (2)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0
2 B
0
3 C 0
4 B 0
for j = 0 to n c[0,j] = 0
Paris 14
LCS Example (3)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0
2 B
0
3 C 0
4 B 0
Paris 18
LCS Example (7)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0
3 C 0
4 B 0
Paris 20
LCS Example (9)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1
3 C 0
4 B 0
Paris 24
LCS Example (13)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1
4 B 0
Paris 27
LCS Example (16)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2
4 B 0
Paris 30
LCS Example (19)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1
Paris 34
LCS-Length(X, Y)
m = length(X), n = length(Y)
Algorithm: LCS
for i = 1 to m
c[i, 0] = 0
for j = 0 to n
c[0, j] = 0
for i = 1 to m
for j = 1 to n
if ( xi = = yj )
c[i, j] = c[i - 1, j - 1] + 1
bi, j " "
else if c[i - 1, j]>=c[i, j - 1]
c[i, j] = c[i - 1, j]
bi, j " "
else c[i, j] = c[i, j - 1]
bi, j " "
Paris 35
return c and b
Finding LCS
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
Paris 36
Finding LCS (2)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
LCS (reversed order): B C B
LCS (straight order): B C B
Paris 37
Algorithm: Finding LCS
Paris 38
Practice Example (Book: Cormen; Page-395)
Paris
LCS : BCBA Length : 4 39
Thank You
Paris 40
Stay Safe
Paris 41