More dynamic programming Longest common subsequence
description
Transcript of More dynamic programming Longest common subsequence
More dynamic programmingLongest common subsequence
Definition of Subsequences on board. Proof of optimal substructure on board. Recursive Formula derivation on board. Example from CLRS.
0 0 0 0 0 0 0
0
0
0
0
0
0
0
λ B D C A B A
λ
A
B
C
B
D
A
B
Lambda is the null string. We fill the first row and column with zeros because the LCS of a null string with anything is zero.
0 1 2 3 4 5 6j
6
0
1
2
3
4
5
i
7
xi
yj
0 0 0 0 0 0 0
0 0 0 0 1 1 1
0
0
0
0
0
0
λ B D C A B A
λ
A
B
C
B
D
A
B
Begin filling in row major order. We place a pointer to tell where the value wasobtained from to help with traceback.
0 1 2 3 4 5 6j
6
0
1
2
3
4
5
i
7
yj
xi
0 0 0 0 0 0 0
0 0 0 0 1 1 1
0 1 1 1 1 2 2
0
0
0
0
0
λ B D C A B A
λ
A
B
C
B
D
A
B
Continue filling in row major order.
0 1 2 3 4 5 6j
6
0
1
2
3
4
5
i
7
xi
yj
Final Result (from CLRS page 395)
The following procedure (from CLRS page 396) will print the LCS. Initial call is to (b, X, 7, 6)
This will print BCBA.
PrintLCS(7,6) (6,6) print A (5,5) (4,5) print B (3,4) (3,3) print C (2,2) (2,1) print B (1,0)
See previous slide. It prints on return from call with