Example Longest Common Subsequence LCS
we have two strings X=BACDB and Y=BDCB , find the longest common subsequence.
S1 = “acbaed” and S2 = “abcadf”.
S1 = “ABCBDAB” and S2 = “BDCABA”.
S1 = “AGGTAB” and S2 = “GXTXAYB”.
First step: Initially create a 2D matrix (say dp[][]) of size 8 x 7 whose first row and first column are filled with
0.
Creating the dp table
Second step: Traverse for i = 1. When j becomes 5, S1[0] and S2[4] are equal. So the dp[][] is updated. For the
other elements take the maximum of dp[i-1][j] and dp[i][j-1]. (In this case, if both values are equal, we have
used arrows to the previous rows).
Filling the row no 1
Third step: While traversed for i = 2, S1[1] and S2[0] are the same (both are ‘G’). So the dp value in that cell is
updated. Rest of the elements are updated as per the conditions.
Filling the row no. 2
Fourth step: For i = 3, S1[2] and S2[0] are again same. The updations are as follows.
Filling row no. 3
Fifth step: For i = 4, we can see that S1[3] and S2[2] are same. So dp[4][3] updated as dp[3][2] + 1 = 2.
Filling row 4
Sixth step: Here we can see that for i = 5 and j = 5 the values of S1[4] and S2[4] are same (i.e., both are ‘A’).
So dp[5][5] is updated accordingly and becomes 3.
Filling row 5
Final step: For i = 6, see the last characters of both strings are same (they are ‘B’). Therefore the value of
dp[6][7] becomes 4.
Filling the final row
So we get the maximum length of common subsequence as 4.
Pseudocode
In this procedure, table C[m, n] is computed in row major order and another table B[m,n] is computed to
construct optimal solution.
Algorithm: LCS-Length-Table-Formulation (X, Y)
m := length(X)
n := length(Y)
for i = 1 to m do
C[i, 0] := 0
for j = 1 to n do
C[0, j] := 0
for i = 1 to m do
for j = 1 to n do
if xi = yj
C[i, j] := C[i - 1, j - 1] + 1
B[i, j] := ‘D’
else
if C[i -1, j] ≥ C[i, j -1]
C[i, j] := C[i - 1, j] + 1
B[i, j] := ‘U’
else
C[i, j] := C[i, j - 1] + 1
B[i, j] := ‘L’
return C and B
Algorithm: Print-LCS (B, X, i, j)
if i=0 and j=0
return
if B[i, j] = ‘D’
Print-LCS(B, X, i-1, j-1)
Print(xi)
else if B[i, j] = ‘U’
Print-LCS(B, X, i-1, j)
else
Print-LCS(B, X, i, j-1)
This algorithm will print the longest common subsequence of X and Y.