More dynamic programming Longest common subsequence

7
More dynamic programming Longest common subsequence

description

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 - PowerPoint PPT Presentation

Transcript of More dynamic programming Longest common subsequence

Page 1: More dynamic programming Longest common subsequence

More dynamic programmingLongest common subsequence

Page 2: 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.

Page 3: More dynamic programming Longest common subsequence

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

Page 4: More dynamic programming Longest common subsequence

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

Page 5: More dynamic programming Longest common subsequence

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

Page 6: More dynamic programming Longest common subsequence

Final Result (from CLRS page 395)

Page 7: More dynamic programming Longest common subsequence

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