Title: Dynamic Programming
1Lecture 7
Topics
Reference Introduction to Algorithm by
Cormen Chapter 15 Dynamic Programming
2Longest Common Subsequence (LCS)
- Biologists need to measure similarity between DNA
and thus determine how closely related an
organism is to another. - They do this by considering DNA as strings of
letters A,C,G,T and then comparing similarities
in the strings. - Formally, they look at common subsequences in the
strings. - Example X ABCBDAB, YBDCABA
- Subsequences may be ABA, BCA, BCB, BBA
- BCBA
- BDAB
etc. - But the Longest Common Subsequences (LCS) are
BCBA and BDAB. - How to find LCS efficiently?
3Longest Common Subsequence( Brute Force Approach
)
- if X m, Y n, then there are 2m
subsequences of X we must compare each with Y (n
comparisons) - So the running time of the brute-force algorithm
is O(n 2m) - Making it impractical for long sequences.
- LCS problem has optimal substructure
- solutions of subproblems are parts of the
final solution. - Subproblems find LCS of pairs of prefixes of X
and Y
4LCS Optimal Substructure
5LCS Setup for Dynamic Programming
- First well find the length of LCS, along the way
we will leave clues on finding subsequence. - Define Xi, Yj to be the prefixes of X and Y of
length i and j respectively. - Define ci,j to be the length of LCS of Xi and
Yj - Then the length of LCS of X and Y will be cm,n
6LCS recurrence
- Notice the issuesThis recurrence is exponential.
- We dont know max ahead of time.
- The subproblems overlap, to find LCS we need to
find LCS of ci, j-1 and of ci-1, j
7LCS Algorithm
- First well find the length of LCS. Later well
modify the algorithm to find LCS itself. - Recall we want to let Xi, Yj to be the prefixes
of X and Y of length i and j respectively - And that Define ci,j to be the length of LCS of
Xi and Yj - Then the length of LCS of X and Y will be cm,n
8LCS Recursive Solution
- We start with i j 0 (empty substrings of x
and y) - Since X0 and Y0 are empty strings, their LCS is
always empty (i.e. c0,0 0) - LCS of empty string and any other string is
empty, so for every i and j c0, j ci,0 0
9LCS Recursive Solution
- When we calculate ci,j, we consider two cases
- First case xiyj one more symbol in strings
X and Y matches, so the length of LCS Xi and Yj
equals to the length of LCS of smaller strings
Xi-1 and Yi-1 , plus 1
10LCS Recursive Solution
- Second case xi ! yj
- As symbols dont match, our solution is not
improved, and the length of LCS(Xi , Yj) is the
same as before (i.e. maximum of LCS(Xi, Yj-1) and
LCS(Xi-1,Yj)
11 LCS Example
- Well see how LCS algorithm works on the
following - example
- X ABCB
- Y BDCAB
- What is the Longest Common Subsequence
- of X and Y?
- LCS(X, Y)
BCB -
- X A B C B
- Y B D C A B
12LCS Example (0)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
A
1
B
2
3
C
4
B
X ABCB m X 4 Y BDCAB n Y
5 Allocate array c6,5
13LCS Example (1)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
A
1
0
B
2
0
3
C
0
4
B
0
for i 1 to m ci,0 0
14LCS Example (2)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
B
2
0
3
C
0
4
B
0
for j 0 to n c0,j 0
15LCS Example (3)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
B
2
0
3
C
0
4
B
0
case i1 and j1 A ! B but,
c0,1gtc1,0 so c1,1 c0,1, and
b1,1
16LCS Example (4)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
B
2
0
3
C
0
4
B
0
case i1 and j2 A ! D but,
c0,2gtc1,1 so c1,2 c0,2, and
b1,2
17LCS Example (5)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
0
B
2
0
3
C
0
4
B
0
case i1 and j3 A ! C but,
c0,3gtc1,2 so c1,3 c0,3, and
b1,3
18LCS Example (6)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
0
1
B
2
0
3
C
0
4
B
0
case i1 and j4 A A so
c1,4 c0,31, and b1,4
19LCS Example (7)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
0
1
1
B
2
0
3
C
0
4
B
0
case i1 and j5 A ! B this
time c0,5ltc1,4 so c1,5 c1, 4,
and b1,5
20LCS Example (8)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
3
C
0
4
B
0
case i2 and j1 B B so
c2, 1 c1, 01, and b2, 1
21LCS Example (9)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
3
C
0
4
B
0
case i2 and j2 B ! D and
c1, 2 lt c2, 1 so c2, 2 c2, 1
and b2, 2
22LCS Example (10)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
3
C
0
4
B
0
case i2 and j3 B ! D and
c1, 3 lt c2, 2 so c2, 3 c2, 2
and b2, 3
23LCS Example (11)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
3
C
0
4
B
0
case i2 and j4 B ! A and
c1, 4 c2, 3 so c2, 4 c1, 4
and b2, 2
24LCS Example (12)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
4
B
0
case i2 and j5 B B so c2,
5 c1, 41 and b2, 5
25LCS Example (13)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
4
B
0
case i3 and j1 C ! B and
c2, 1 gt c3,0 so c3, 1 c2, 1 and
b3, 1
26LCS Example (14)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
1
4
B
0
case i3 and j 2 C ! D and
c2, 2 c3, 1 so c3, 2 c2, 2
and b3, 2
27LCS Example (15)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
1
2
4
B
0
case i3 and j 3 C C so
c3, 3 c2, 21 and b3, 3
28LCS Example (16)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
1
2
2
4
B
0
case i3 and j 4 C ! A c2,
4 lt c3, 3 so c3, 4 c3, 3 and
b3, 3
29LCS Example (17)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
1
2
2
2
4
B
0
case i3 and j 5 C ! B c2,
5 c3, 4 so c3, 5 c2, 5 and
b3, 5
30LCS Example (18)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
1
2
2
2
4
B
0
1
case i4 and j1 B B so
c4, 1 c3, 01 and b4, 1
31LCS Example (19)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
1
2
2
2
4
B
0
1
1
case i4 and j2 B ! D c3,
2 c4, 1 so c4, 2 c3, 2 and
b4, 2
32LCS Example (20)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
1
2
2
2
4
B
0
1
1
2
case i4 and j 3 B ! C c3,
3 gt c4, 2 so c4, 3 c3, 3 and
b4, 3
33LCS Example (21)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
1
2
2
2
4
B
0
1
1
2
2
case i4 and j4 B ! A c3,
4 c4, 3 so c4, 4 c3, 4 and
b3, 5
34LCS Example (22)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
0
0
1
0
1
B
2
0
1
1
1
1
2
3
C
0
1
1
2
2
2
3
4
B
0
1
1
2
2
case i4 and j5 B B so c4,
5 c3, 41 and b4, 5
35LCS Algorithm
- LCS-Length(X, Y)
- m length(X), n length(Y)
- for i 1 to m do
- ci, 0 0
- for j 0 to n do
- c0, j 0
- for i 1 to m do
- for j 1 to n do
- if ( xi yj ) then
- ci, j ci - 1, j -
1 1 -
- else if ci - 1, jgtci, j - 1 then
- ci, j ci - 1, j
- else ci, j ci, j - 1
-
- return c and b
36LCS Algorithm Running Time
- LCS algorithm calculates the values of each entry
of the array cm,n - So the running time is clearly O(mn) as each
entry is done in 3 steps. - Now how to get at the solution?
- We use the arrows we created to guide us.
- We simply follow arrows back to base case 0
37Finding LCS
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
1
0
0
0
1
B
2
0
1
2
1
1
1
3
C
0
1
1
2
2
2
3
4
B
0
1
1
2
2
38Finding LCS (2)
j 0 1 2 3 4
5
i
Yj
B
B
A
C
D
Xi
0
0
0
0
0
0
0
A
1
0
1
0
0
0
1
B
2
0
1
2
1
1
1
3
C
0
1
1
2
2
2
3
4
B
0
1
1
2
2
B
C
B
LCS
39Finding LCS (3)
- Print_LCS (X, i, j)
- if i 0 or j 0 then return
- if bi, j then
- Print_LCS (X, i-1, j-1)
- Print Xi
- elseif bi, j then
- Print_LCS (X, i-1, j)
- else
- Print_LCS (X, i, j-1)
Cost O(mn)
40Element of Dynamic Programming
- Optimal Substructure
- Overlapping Subproblems
41Optimal Substructure
- A problem exhibits optimal substructure if an
optimal solution contains optimal solutions to
its subproblems. - Build an optimal solution from optimal solutions
to subproblems - solutions of subproblems are parts of the final
solution. - Example Longest Common Subsequence -
- An LCS contains within it optimal solutions
to the prefixes of the two input sequences. - Common with Greedy Solution
42Overlapping Subproblems
- Divide-and-Conquer is suitable when generating
brand-new problems at each step of the recursion. - Dynamic-programming algorithms take advantage of
overlapping subproblems by solving each
subproblem once and then storing the solution in
a table where it can be looked up when needed,
using constant time per lookup
43Dynamic VS Greedy
- Dynamic programming uses optimal substructure in
a bottom-up fashion - First find optimal solutions to subproblems and,
having solved the subproblems, we find an optimal
solution to the problem - Greedy algorithms use optimal substructure in a
top-down fashion - First make a choice the choice that looks best
at the time and then solving the resulting
subproblem