Dynamic Programming 3 Longest Common Subsequence LCS What
Dynamic Programming (3) Longest Common Subsequence (LCS)
What is a subsequence ? Sequence: ABCBDABBBACD
What is a subsequence ? Sequence: ABCBDABBBACD BDBBC is a subsequence
What is a subsequence ? Sequence: ABCBDABBBACD ABBD is a subsequence
Definition of a subsequence •
The Longest Common Subsequence Problem •
The Longest Common Subsequence Problem •
The Longest Common Subsequence Problem •
A more interesting input https: //www. ncbi. nlm. nih. gov/nuccore/MN 908947 X= ……….
A more interesting input https: //www. ncbi. nlm. nih. gov/nuccore/NC_004718. 3 Y= ……….
Some notation
A recursive solution
A recursive solution
A recursive solution • A simple recursive procedure will solve the same problem multiple times • Solve all subproblems by filling a table in increasing subproblem size
The table we need to fill 0 0 0 1 A 0 2 B 0 3 C 0 4 B 0 5 D 0 6 A 0 7 B 0 X 1 2 3 4 5 6 B D C A B A 0 0 0 Y
Filling the table 0 0 1 2 3 4 5 6 B D C A B A 0 0 0 0 0 1 1 A 0 2 B 0 3 C 0 4 B 0 5 D 0 6 A 0 7 B 0
Filling the table 0 0 1 2 3 4 5 6 B D C A B A 0 0 0 0 1 A 0 0 1 1 1 2 B 0 1 1 2 2 3 C 0 4 B 0 5 D 0 6 A 0 7 B 0
Filling the table 0 0 1 2 3 4 5 6 B D C A B A 0 0 0 0 1 A 0 0 1 1 1 2 B 0 1 1 2 2 3 C 0 1 1 2 2 4 B 0 5 D 0 6 A 0 7 B 0
Filling the table 0 0 1 2 3 4 5 6 B D C A B A 0 0 0 0 1 A 0 0 1 1 1 2 B 0 1 1 2 2 3 C 0 1 1 2 2 4 B 0 1 1 2 2 3 3 5 D 0 6 A 0 7 B 0
Filling the table 0 0 1 2 3 4 5 6 B D C A B A 0 0 0 0 1 A 0 0 1 1 1 2 B 0 1 1 2 2 3 C 0 1 1 2 2 4 B 0 1 1 2 2 3 3 5 D 0 1 2 2 2 3 3 6 A 0 7 B 0
Filling the table 0 0 1 2 3 4 5 6 B D C A B A 0 0 0 0 1 A 0 0 1 1 1 2 B 0 1 1 2 2 3 C 0 1 1 2 2 4 B 0 1 1 2 2 3 3 5 D 0 1 2 2 2 3 3 6 A 0 1 2 2 3 3 4 7 B 0
Filling the table 0 0 1 2 3 4 5 6 B D C A B A 0 0 0 0 1 A 0 0 1 1 1 2 B 0 1 1 2 2 3 C 0 1 1 2 2 4 B 0 1 1 2 2 3 3 5 D 0 1 2 2 2 3 3 6 A 0 1 2 2 3 3 4 7 B 0 1 2 2 3 4 4
P-code for the algorithm O(nm) time and space
Recovering the longest sequence We actually can get away without the matrix b Can use linear space if we are just interested in the length of the LCS
- Slides: 24