Professional Documents
Culture Documents
Subsequence Using
Dynamic Programming
Y. n*2m
STEP3: Find the longest common subsequence from available s
ubsequences.
T.C= O(n2m)
To improve time complexity, we use dynamic programming
Dynamic Programing
Optimal substructure
We have two strings
X= { x1,x2,x3,,xn}
Y= {y1,y2,y3,.,ym}
First compare xn and ym. If they matched, find the subsequence in
the remaining string and then append the xn with it.
If xn ym,
Remove xn from X and find LCS from x1 to xn-1 and y1 to ym
Remove ym from Y and find LCS from x1 to xn and y1 to ym-1
In each step, we reduce the size of the problem into the subproblem
s. It is optimal substructure.
Cont.
Recursive Equation
X= { x1,x2,x3,,xn}
Y= {y1,y2,y3,.,ym}
C[I,j] is length of LCS in X and Y
0
c[I,j] =
{ ; i=0 or j=0
1+c[i-1,j-1] ; I,j>0 and xi=yi
max(c[i-1,j],c[I,j-1]) ; I,j>0 and xiyi
Recursion Tree Of LCS
BEST CASE
X={A,A,A,A}
Y={A,A,A,A}
{0
c[I,j] =
; i=0 or j=0
1+c[i-1,j-1] ; I,j>0 and xi=yi
max(c[i-1,j],c[I,j-1]); I,j>0 and xiyi
C(4,4) =4
1+C(3,3) =4
1+C(2,2) =3
1+C(1,1) =2
1+C(0,0) =1
LCS = 4
T.C. = O(n) 0
Cont.
WORST CASE
X={A,A,A}
Y={B,B,B}
0
c[I,j] =
{ ; i=0 or j=0
1+c[i-1,j-1] ; I,j>0 and xi=yi
max(c[i-1,j],c[I,j-1]); I,j>0 and xiyi
C(3,3)
C(3,2) C(2,3)
No of nodes O(2+)
As here, the overlapping problem exits, we can apply the dynamic programming.
There are 3*3 unique subproblems. So we compute them once and save in table for
further refrence so n*n memory space required.
Cont.
0 1 - - m
00 01 02 0m
0
(m+1)*(n+1)
=O(m*n)
10 11 12 1m
1
Every element is
20 21 22 2m depend on
2 diagonal, left or
above element
C(2,2)
-
n0 n1 n2 nm
n C(2,1) C(1,2)
0 B D C A B A
for i =1 to m
0 c[i,0] = 0
for j =0 to n
A c[0,j] = 0
B
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
if xi = for
yj i =1 to m
0 0 0 0 0 0 0 0 then c[i,0] = 0
c[i, j] for j =0 to
= c[i-1, n
j-1]+1
A 0 else c[0,j] = 0
L[i, j]=max{c[i-1,j], c[i, j-1]
B 0
C 0
B 0
D 0
A 0
B 0
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
if xi = yj
0 0 0 0 0 0 0 0 then
c[i, j] = c[i-1, j-1]+1
A 0 0 0 0 else
L[i, j]=max{c[i-1,j], c[i, j-1]
B 0
C 0
B 0
D 0
A 0
B 0
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
if xi = yj
0 0 0 0 0 0 0 0 then
c[i, j] = c[i-1, j-1]+1
A 0 0 0 0 0+1 else
L[i, j]=max{c[i-1,j], c[i, j-1]
B 0
C 0
B 0
D 0
A 0
B 0
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
if xi = yj
0 0 0 0 0 0 0 0 then
c[i, j] = c[i-1, j-1]+1
A 0 0 0 0 1 1 1 else
L[i, j]=max{c[i-1,j], c[i, j-1]
B 0 1 1 1 1 2 2
C 0 1 1 2 2 2 2
B 0 1 1 2 2 3 3
D 0 1 2 2 2 3 3
A 0 1 2 2 3 3 4
B 0 1 2 2 3 4 4
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
0 0 0 0 0 0 0 0
A 0 0 0 0 1 1 1
B 0 1 1 1 1 2 2
C 0 1 1 2 2 2 2
B 0 1 1 2 2 3 3
D 0 1 2 2 2 3 3
A 0 1 2 2 3 3 4
B 0 1 2 2 3 4 4
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
0 0 0 0 0 0 0 0
A 0 0 0 0 1 1 1
B 0 1 1 1 1 2 2
C 0 1 1 2 2 2 2
B 0 1 1 2 2 3 3
D 0 1 2 2 2 3 3
A 0 1 2 2 3 3 4
B 0 1 2 2 3 4 4
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
0 0 0 0 0 0 0 0
A 0 0 0 0 1 1 1
B 0 1 1 1 1 2 2
Subsequence=BCBA
C 0 1 1 2 2 2 2
B 0 1 1 2 2 3 3
D 0 1 2 2 2 3 3
A 0 1 2 2 3 3 4
B 0 1 2 2 3 4 4
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
0 0 0 0 0 0 0 0
A 0 0 0 0 1 1 1
B 0 1 1 1 1 2 2
C 0 1 1 2 2 2 2
B 0 1 1 2 2 3 3
D 0 1 2 2 2 3 3
A 0 1 2 2 3 3 4
B 0 1 2 2 3 4 4
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
0 0 0 0 0 0 0 0
A 0 0 0 0 1 1 1 Subsequence = BCAB
B 0 1 1 1 1 2 2
C 0 1 1 2 2 2 2
B 0 1 1 2 2 3 3
D 0 1 2 2 2 3 3
A 0 1 2 2 3 3 4
B 0 1 2 2 3 4 4
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
0 0 0 0 0 0 0 0
A 0 0 0 0 1 1 1
B 0 1 1 1 1 2 2
C 0 1 1 2 2 2 2
B 0 1 1 2 2 3 3
D 0 1 2 2 2 3 3
A 0 1 2 2 3 3 4
B 0 1 2 2 3 4 4
Cont.
X={B,D,C,A,B,A}
Y={A,B,C,B,D,A,B}
0 B D C A B A
0 0 0 0 0 0 0 0
Subsequence = BDAB
A 0 0 0 0 1 1 1
B 0 1 1 1 1 2 2
C 0 1 1 2 2 2 2
B 0 1 1 2 2 3 3
D 0 1 2 2 2 3 3
A 0 1 2 2 3 3 4
B 0 1 2 2 3 4 4
Cont
We get three longest common subsequences
BCBA
BCAB
BDAB
Length of longest common subsequence is 4
Analysis Of LCS
We have two nested loops
The outer one iterates n times
The inner one iterates m times
A constant amount of work is done inside each
iteration of the inner loop
Thus, the total running time is O(nm)