80 likes | 279 Views
More dynamic programming Longest common subsequence. Definition of Subsequences on board. Proof of optimal substructure on board. Recursive Formula derivation on board. Example from CLRS. Lambda is the null string. We fill the first row and column with zeros because the LCS of
E N D
More dynamic programming Longest common subsequence
Definition of Subsequences on board. • Proof of optimal substructure on board. • Recursive Formula derivation on board. • Example from CLRS.
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 6 j λ C A B A yj i B D λ 0 1 A 2 B C 3 4 B 5 D A 6 7 B xi
Begin filling in row major order. We place a pointer to tell where the value was obtained from to help with traceback. 0 1 2 3 4 5 6 j λ C A B A yj i B D λ 0 1 A 2 B C 3 4 B 5 D A 6 7 B xi
Continue filling in row major order. 0 1 2 3 4 5 6 j λ C A B A yj i B D λ 0 1 A 2 B C 3 4 B 5 D A 6 7 B xi
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