Dynamic Programming
Dynamic Programming
Dynamic Programming
An algorithm design technique (like divide and
conquer)
Divide and conquer
Partition the problem into independent subproblems
Solve the subproblems recursively
Combine the solutions to solve the original problem
Dynamic Programming
Applicable when subproblems are not independent
Subproblems share subsubproblems
E.g.: Combinations:
n
k
n
1
=1
n-1
k
n-1
k-1
n
n
=1
Example: Combinations
Comb (6,4)
=
Comb (5, 3)
Comb (4,2)
Comb (4, 3)
+ Comb (2,
+ 1) + Comb (2, 2) + +Comb (2, 1) + Comb (2,
+ 2) +
Comb (3, 2)
Comb (3, 2)
Comb (5, 4)
n-1
k
Comb (4, 3)
Comb
+ (3, 3)
Comb
+ (3, 2)
Comb (4, 4)
Comb
+ (3, 3) +
1+
n
k
n-1
k-1
4
Dynamic Programming
Used for optimization problems
A set of choices must be made to get an optimal
solution
solution
3. Compute the value of an optimal solution in a
bottom-up fashion
One Solution
Brute force
Enumerate all possibilities of selecting stations
Compute how long it takes in each case and choose
the best one
Solution:
0 if choosing line 2
at step j (= 3)
1 if choosing line 1
at step j (= n)
11
S1,j-1
S1,j
a1,j-1
a1,j
t2,j-1
Line 2
a2,j-1
S2,j-1
12
S1,j-1
S1,j
a1,j-1
a1,j
t2,j-1
Optimal Substructure
Line 2
a2,j-1
S2,j-1
13
Optimal Substructure
Generalization: an optimal solution to the
problem find the fastest way through S1, j contains
within it an optimal solution to subproblems: find
the fastest way through S1, j - 1 or S2, j 1.
This is referred to as the optimal substructure
property
We use this property to construct an optimal
solution to a problem from optimal solutions to
subproblems
14
2. A Recursive Solution
Define the value of an optimal solution in terms of the optimal
solution to subproblems
15
16
17
f1[j - 1] + a1,j
the way through S2, j - 1, transfer from line 2 to line 1, then through S1, j
Line 1
S1,j-1
S1,j
a1,j-1
a1,j
Line 2
t2,j-1
a2,j-1
S2,j-1
18
f1[j] =
f2[j] =
e1 + a1,1
if j = 1
if j = 1
19
f1[j]
f1(1)
f1(2)
f1(3)
f1(4)
f1(5)
f2[j]
f2(1)
f2(2)
f2(3)
f2(4)
f2(5)
4 times
2 times
f1[j]
f2[j]
Bottom-up approach
First find optimal solutions to subproblems
Find an optimal solution to the problem from the
subproblems
21
Example
e1 + a1,1,
f1[j] = min(f1[j - 1] + a1,j ,f2[j -1] + t2,j-1 + a1,j)
1
f1[j]
18[1]
20[2]
24[1]
32[1]
f2[j]
12
16[1]
22[2]
25[1]
30[2]
if j = 1
if j 2
f* = 35[1]
22
FASTEST-WAY(a, t, e, x, n)
1. f1[1] e1 + a1,1
2. f2[1] e2 + a2,1
3. for j 2 to n
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
O(N)
Compute the values of
f1[j] and l1[j]
l1[j] 2
if f2[j - 1] + a2, j f1[j - 1] + t1, j-1 + a2, j
then f2[j] f2[j - 1] + a2, j
l2[j] 2
else f2[j] f1[j - 1] + t1, j-1 + a2, j
l2[j] 1
23
FASTEST-WAY(a, t, e, x, n) (cont.)
14. if f1[n] + x1 f2[n] + x2
15.
16.
17.
18.
then f* = f1[n] + x1
l* = 1
else f* = f2[n] + x2
l* = 2
24
f1[j]/l1[j]
18[1]
20[2]
24[1]
32[1]
f2[j]/l2[j]
12
16[1]
22[2]
25[1]
30[2]
l* = 1
25
Overlapping Subproblems
If a recursive algorithm revisits the same subproblems
over and over the problem has overlapping
subproblems
26
Assembly line
(n) subproblems (n stations)
(n) overall
Matrix multiplication:
(n2) subproblems (1 i j n)
(n3) overall
28
A, B, D, B, C, D, B, etc.
29
Example
X = A, B, C, B, D, A, B
X = A, B, C, B, D, A, B
Y = B, D, C, A, B, A
Y = B, D, C, A, B, A
Brute-Force Solution
For every subsequence of X, check whether its
a subsequence of Y
There are 2m subsequences of X to check
Each subsequence takes (n) time to check
scan Y for first letter, from there scan for second, and
so on
Notations
Given a sequence X = x1, x2, , xm we define
the i-th prefix of X, for i = 0, 1, 2, , m
Xi = x1, x2, , xi
33
A Recursive Solution
Case 1: xi = yj
e.g.:
Xi = A, B, D, E
Yj = Z, B, E
c[i, j] = c[i - 1, j - 1] + 1
A Recursive Solution
Case 2: xi yj
e.g.:
Xi = A, B, D, G
Yj = Z, B, D
c[i, j] = max { c[i - 1, j], c[i, j-1] }
Overlapping Subproblems
To find a LCS of X and Y
we may need to find the LCS between X and Yn-1 and
that of Xm-1 and Y
Both the above subproblems has the subproblem of
finding the LCS of Xm-1 and Yn-1
0
c[i-1, j-1] + 1
max(c[i, j-1], c[i-1, j])
xi
x1
x2
m xm
0
yj:
1
y1
2
y2
if i = 0 or j = 0
if xi = yj
if xi yj
n
yn
0
first
0
0
0
0
0
second
j
37
Additional Information
0
if i,j = 0
c[i, j] = c[i-1, j-1] + 1
if xi = yj
max(c[i, j-1], c[i-1, j]) if xi yj
b & c:
0
xi
m D
0
yj:
1
A
2
C
3
D
0
0
0
0
0
0
0
c[i-1,j]
c[i,j-1]
n
F
LCS-LENGTH(X, Y, m, n)
1. for i 1 to m
2.
do c[i, 0] 0
The length of the LCS if one of the sequences
3. for j 0 to n
is empty is zero
4.
do c[0, j] 0
5. for i 1 to m
6.
do for j 1 to n
7.
do if xi = yj
8.
then c[i, j] c[i - 1, j - 1] + 1 Case 1: xi = yj
9.
b[i, j ]
10.
else if c[i - 1, j] c[i, j - 1]
11.
then c[i, j] c[i - 1, j]
12.
b[i, j]
Case 2: xi yj
13.
else c[i, j] c[i, j - 1]
14.
b[i, j]
15. return c and b
39
Example
0
if i = 0 or j = 0
if xi = yj
max(c[i, j-1], c[i-1, j]) if xi yj
X = A, B, C, B, D, A
c[i, j] = c[i-1, j-1] + 1
Y = B, D, C, A, B, A
If xi = yj
b[i, j] =
Else if
c[i - 1, j] c[i, j-1]
b[i, j] =
else
b[i, j] =
0
yj
1
B
2
D
3
C
4
A
5
B
6
A
xi
2
3
4
40
4. Constructing a LCS
Start at b[m, n] and follow the arrows
When we encounter a in b[i, j] xi = yj is an element
of the LCS
0
yj
1
B
2
D
3
C
4
A
5
B
6
A
xi
1
2
2
3
4
41
PRINT-LCS(b, X, i, j)
1. if i = 0 or j = 0
Running time: (m + n)
2. then return
3. if b[i, j] =
4.
then PRINT-LCS(b, X, i - 1, j - 1)
5.
print xi
6. elseif b[i, j] =
7.
then PRINT-LCS(b, X, i - 1, j)
8.
else PRINT-LCS(b, X, i, j - 1)
Initial call: PRINT-LCS(b, X, length[X], length[Y])
42
43
44