Professional Documents
Culture Documents
1
Lecture 7
Dynamic
Programming
2
follow definition /
Brute Force & try all possibilities
Exhaustive
Search
break problem
Divide & into distinct
Iterative Conquer sub-problems
Improvement
Randomization
5
Dynamic Programming
6
The steps in the development of a dynamic
programming algorithm are as follows:
7
Example: Fibonacci Numbers
8
Overlapping subproblems
Fib(5)
Fib(4) Fib(3)
Fib(1) Fib(0)
If all leaves had the same depth, then there
9
would be about 2n recursive calls.
Save Work
Wasteful approach - repeat work unnecessarily
Ex : Fib(2) is computed three times
Instead, compute Fib(2) once, store result in a table,
and access it when needed
10
More Efficient Recursive Alg
F[0] := 0; F[1] := 1; F[n] := Fib(n);
Fib(n):
if n = 0 or 1 then return F[n]
if F[n-1] = NIL then F[n-1] := Fib(n-1)
if F[n-2] = NIL then F[n-2] := Fib(n-2)
return (F[n-1] + F[n-2])
11
Example of Memoized Fib
12
Get Rid of the Recursion
Recursion adds overhead
extra time for function calls
extra space to store information on the runtime stack about
each currently active function call
Avoid the recursion overhead by filling in the table
entries bottom up, instead of top down.
13
Subproblem Dependencies
Figure out which subproblems rely on which other
subproblems
Example:
F0 F1 F2 F3 … Fn-2 Fn-1 Fn
14
Order for Computing Subproblems
Then figure out an order for computing the
subproblems that respects the dependencies:
when you are solving a subproblem, you have already solved
all the subproblems on which it depends
Example: Just solve them in the order
F0 , F1 , F2 , F3 , …
15
Dynamic Programming Solution for Fibonacci
Fib(n):
F[0] := 0; F[1] := 1;
for i := 2 to n do
F[i] := F[i-1] + F[i-2]
return F[n]
Can perform application-specific optimizations
e.g., save space by only keeping last two numbers
computed
16
Longest Common Subsequence
17
For example, Find subsequences of “abcdefg”.
18
Subsequence
Let us consider a sequence S = <s1, s2, s3, s4, …,sn>.
A sequence Z = <z1, z2, z3, z4, …,zm> over S is called a
subsequence of S, if and only if it can be derived from S
deletion of some elements.
19
Common Subsequence
20
Longest Common Subsequence
21
Substring Vs. Subsequence
22
Example: Longest Common Subsequence
y: B D C A B A
LCS(x, y) = BCBA or BCAB
23
Example: Longest Common Subsequence
x: A B C B D A B
y: B D C A B A
LCS(x, y) = BCAB
24
Example: Longest Common Subsequence
1-Compare Y to X symbols
x: A B C B D A B
y: B D C A B A
LCS(x, y) = BCBA
25
Applications
Subsequences have applications in Bioinformatics, where computers
are used to compare, analyze, and store DNA strands.
26
LCS Algorithm
27
Another procedure to
improve number of
comparisons
28
Define a function LCS(X, Y): gives the longest subsequences
common to X and Y. That function relies on the following
two properties:
29
Case 1: If the two sequences both end in the same element
30
Case 2: If the two sequences don‟t end in the same symbol.
the LCS of X and Y is the longer of the two sequences LCS(Xi,Yj-1) and
LCS(Xi-1,Yj).
The last character of the LCS of these two sequences either ends with a
G (1) or doesn't (2).
31
Overlapping subproblems
32
LCS recursive solution
Let‟s first try to find the length c (not the actual letters) of the
LCS(X,Y)
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.
c[0,0] = 0)
LCS of empty string and any other string is empty, so for every i
and j: c[0, j] = c[i,0] = 0
33
LCS recursive solution
0 if i 0 or j 0
C[i, j ] c[i 1, j 1] 1 if x[i ] y[ j ],
max(c[i, j 1], c[i 1, j ]) otherwise
if i 0 or j 0
LCS [ Xi, Yj ] LCS ( X i 1 , Y j 1 ), xi if xi y j ,
longest ( LCS ( X i , Y j 1 ), LCS ( X i 1 , Y j )) if xi y j
34
LCS : Algorithm
35
LCS-Length(X,Y)
1. m = length(X) // get the # of symbols in X
2. n = length(Y) // get the # of symbols in Y
Let b[1..m, 1..n] and c[1..m, 0..n] be new tables
3. for i = 1 to m
c[i,0] = 0 // special case:Y0
4. for j = 1 to n
c[0,j] = 0 // special case: X0
5. for i = 1 to m // for all Xi
6. for j = 1 to n // for all Yj
7. if ( Xi == Yj ) {
8. c[i,j] = c[i-1,j-1] + 1
8. b[i,j] =“ ” }
9. else { c[i,j] = max( c[i-1,j], c[i,j-1] )
Print either “ “ or “ ” }
10. return c 36
LCS Example
We‟ll see how LCS algorithm works on the following
example:
X = ABCB
Y = BDCAB
37
ABCB
LCS Example (0) BDCAB
j 0 1 2 3 4
i 5 Yj B D C A B
0 Xi
A
1
2 B
3 C
4 B
X = ABCB; m = |X| = 4
Y = BDCAB; n = |Y| = 5
Allocate array c[5,4] – from 0-5, from 0-4
38
LCS Example (1) ABCB
j 0 1 2 3 4 BDCAB
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0
2 B
0
3 C 0
4 B 0
for i = 1 to m c[i,0] = 0
for j = 1 to n c[0,j] = 0
39
LCS Example (2) ABCB
j 0 1 2 3 4 BDCAB
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
40
LCS Example (3) ABCB
j 0 1 2 3 4 BDCAB
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
41
LCS Example (4) ABCB
j 0 1 2 3 4 BDCAB
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
42
LCS Example (5) ABCB
j 0 1 2 3 4 BDCAB
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
43
LCS Example (6) ABCB
j 0 1 2 3 4 BDCAB
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
44
LCS Example (7) ABCB
j 0 1 2 3 4 BDCAB
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
45
LCS Example (8) ABCB
j 0 1 2 3 4 BDCAB
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
46
LCS Example (9) ABCB
j 0 1 2 3 4 BDCAB
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
47
LCS Example ABCB
(10) BDCAB
j 0 1 2 3 4
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
48
LCS Example ABCB
(11) BDCAB
j 0 1 2 3 4
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
49
LCS Example ABCB
(12) BDCAB
j 0 1 2 3 4
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
50
LCS Example ABCB
(13) BDCAB
j 0 1 2 3 4
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
51
LCS Example ABCB
(14) BDCAB
j 0 1 2 3 4
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
52
LCS Example ABCB
(15) BDCAB
j 0 1 2 3 4
i 5 Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
53
LCS Algorithm Running Time
O(m*n)
since each c[i,j] is calculated in
constant time, and there are m*n
elements in the array
54
How to find actual LCS
So far, we have just found the length of LCS, but not
LCS itself.
We want to modify this algorithm to make it output
Longest Common Subsequence of X and Y
Each c[i,j] depends on c[i-1,j] and c[i,j-1]
or c[i-1, j-1]
For each c[i,j] we can say how it was acquired:
2 2
For example, here
2 3
c[i,j] = c[i-1,j-1] +1 = 2+1=3
55
How to find actual LCS - continued
Remember that
c[i 1, j 1] 1 if x[i ] y[ j ],
c[i, j ]
max( c[i, j 1], c[i 1, j ]) otherwise
56
Finding LCS
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
57
Finding LCS (2)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
LCS (reversed order ): B C B
LCS (straight order): B C B
58 A palindrome!
Your Turn
Find the LCS for input Sequences
x = XMJYAUZ and y = MZJAWXU
59
Your Turn
Find the LCS for input Sequences
“AGGTAB” and “GXTXAYB”
60
Examples:
LCS for input Sequences “ABCDGH” and
“AEDFHR” is “ADH” of length 3.
LCS for input Sequences “AGGTAB” and
“GXTXAYB” is “GTAB” of length 4.
61
Your Turn
Longest Common Subsequence
62
Thank You
63