Dynamic Programming
Dynamic Programming
11-4
Divide and conquer vs. dynamic
programming
Elements of DP Algorithms
Sub-structure: decompose problem into smaller sub-
problems. Express the solution of the original
problem in terms of solutions for smaller problems.
Table-structure: Store the answers to the sub-
problem in a table, because sub-problem solutions
may be used many times.
Bottom-up computation: combine solutions on
smaller sub-problems to solve larger sub-problems,
and eventually arrive at a solution to the complete
problem.
The shortest path
1 4
S A B 5
T
5 6
2 5 13
S B E T
16 2
5
C 2
F
1 A d(A, T)
2
d(B, T) T
S B
d(C, T)
5
C
d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)}
4
A D
d(D, T)
11
E T
d(E, T)
8 -9
Dynamic programming
d(B, T) = min{9+d(D, T), 5+d(E, T), 16+d(F, T)}
= min{9+18, 5+13, 16+2} = 18.
d(C, T) = min{ 2+d(F, T) } = 2+2 = 4
d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)}
= min{1+22, 2+18, 5+4} = 9.
The above way of reasoning is called
9 D
backward reasoning. d(D, T)
d(E, T)
B
5
E T
16
F d(F,8T-)10
Backward approach
(forward reasoning)
d(S, A) = 1
d(S, B) = 2
d(S, C) = 5
d(S,D)=min{d(S, A)+d(A, D),d(S, B)+d(B, D)}
= min{ 1+4, 2+9 } = 5
d(S,E)=min{d(S, A)+d(A, E),d(S, B)+d(B, E)}
= min{ 1+11, 2+5 } = 7
d(S,F)=min{d(S, A)+d(A, F),d(S, B)+d(B, F)}
= min{ 2+16, 5+2 } = 7
8 -11
d(S,T) = min{d(S, D)+d(D, T),d(S,E)+
d(E,T), d(S, F)+d(F, T)}
= min{ 5+18, 7+13, 7+2 }
=9
8 -12
Principle of optimality
Principle of optimality: Suppose that in solving a
problem, we have to make a sequence of
decisions D1, D2, …, Dn. If this sequence is
optimal, then the last k decisions, 1 k n
must be optimal.
e.g. the shortest path problem
If i, i1, i2, …, j is a shortest path from i to j, then
i1, i2, …, j must be a shortest path from i1 to j
8 -13
1. Matrix Chain Multiplication
• Matrix-chain multiplication problem
– Given a chain A1, A2, …, An of n
matrices,with sizes p0 p1, p1 p2, p2 p3, …,
pn-1 pn
p×q
q×r
p×r
0 if i=j
m[i, j ] =
min {m[i, k] + m[k+1, j ] + pi-1pk pj } if i<j
i ≤ k< j
Dynamic Programming Approach
…contd
1 2 3 4
m(1,4)
1
M11=0 M22=0 M33=0 M44=0
m(1,3) m(2,4)
2 M12=120 M23=48 M34=84
s12=1 s23=2 s34=3 m(1,2) m(2,3) m(3,4)
3 M13=88 M24=104
s13=1 s24=3
4 M14=158 Optimal Parenthesization
s14=3
( A1 ( A2 A3 )) A4
Matrix Chain Multiplication Algorithm
Algorithm Matrix_Chain_Mul(p[], n)
{
for i:= 1 to n do
m[i, i]:= 0 ;
11-27
Example
• Copy the table of previous example and then
construct optimal parenthesization.
0/1 knapsack problem
n objects , weight W1, W2, ,Wn
profit P1, P2, ,Pn
capacity M
maximize P x i i
1in
subject to W x i i
1in
3
xi = 0 or 1, 1in
e. g. i Wi Pi
1 10 40 M=10
2 3 20
8
-
2
9
The multistage graph solution
The 0/1 knapsack problem can be described
by a multistage graph.
x3=0
x2=0 10 100
0
1 0
0
x1=1 011
40 x3=1
S 30 0
0
T
01 0 010
x =0 0 x2=1 x3=0
1
20
0
0 0
x3=1 001
0 30
x2=0 00 0
x3=0 000 8 -30
The dynamic programming
approach
The longest path represents the optimal solution:
x1=0, x2=1, x3=1
=P20+30 = 50
xi i
8 -31
Optimal binary search trees
e.g. binary search trees for 3, 7, 9, 12;
3 12
7 7 9
7
3 12 3 9
9 3
9 12 7
12
(a) (b) (c) (d)
8 -32
Optimal binary search trees
n identifiers : a1 <a2 <a3 <…< an
Pi, 1in : the probability that ai is searched.
Qi, 0in : the probability that x is searched
where ai < x < ai+1 (a0=-, an+1=).
n n
Qi 1
Pi i 0
i 1
8 -33
10
Identifiers : 4, 5, 8, 10, 11,
12, 14
5 14
Internal node : successful
search, Pi
4 8 11 E7
External node :
E0 E1 E2 E3 E4 12 unsuccessful search, Qi
E5 E6
P
n
i
)
n
i1 level( i (level(E i ) 1)
a Q
i i0
8 -34
The dynamic programming
approach
Let C(i, j) denote the cost of an optimal binary
search tree containing ai,…,aj .
The cost of the optimal binary search tree with ak
as its root :
n
k1
0 i k i
C(1, min Q P Q C 1, k 1 Q P Q C k 1, n
i
i
n) Pk i1 ik1
1kn
ak
P1...Pk-1 Pk+1...Pn
Q0...Qk-1 Qk...Qn
a1...ak-1 ak+1...an
C(1,k-1) C(k+1,n) 8 -35
General formula
k1
C(i, mi Pk Qi-1 Pm Qm C i, k 1
j) n
ik j mi
j
Qk
Pm Qm C k j
mk1
1, j
min i k 1 Ck j Qi-1 Qm
C
, 1, P m
ik j
mi
ak
P1...Pk-1 Pk+1...Pn
Q0...Qk-1 Qk...Qn
a1...ak-1 ak+1...an
C(1,3) C(2,4)
• Problem
– n identifiers : a1 <a2 <a3 <…< an
– p(i), 1in : the probability that ai is searched.
– q(i), 0in : the probability that x is searched
where ai < x < ai+1 (a0=-, an+1=).
while
if
if
do while
do
(b)
(a)
while do
do
do while
if
if if
while
(d) (e)
(c)
For the above example, Algorithm search(x)
{ found:=false; t:=tree;
cost( tree a ) = 15 / 7 while( (t≠0) and not found ) do
cost( tree b ) = 13/7 {
if( x=t->data ) then found:=true; else if( x<t->da
cost( tree c ) = 15/7 }
if( not found ) then return 0; else return 1;
cost( tree d ) = 15/7
cost( tree e ) = 15/7
Therefore, tree b is optimal
Ex. 2
}
p(1)=.5, p(2)=.1, p(3)=.05
q(0)=.15, q(1)=.1, q(2)=.05, q(3)=.05
Cost of searching a successful identifier
= frequencry * level
Cost of searching an unsuccessful identifier
cost( tree a ) = 2.65 = frequencry * ( level -1 )
cost( tree b ) = 1.9
cost( tree c ) = 1.5
cost( tree d ) = 2.05
cost( tree e ) = 1.6
Therefore, tree c is optimal
• Identifiers : stop, if, do
stop
Internal node : successful
if
search, p(i)
E3
• External node :
do E2 unsuccessful search, q(i)
E0 E1
Thenexpected cost
n
of a binary tree:
P lei)v
i
Qei(ll(eia)
n1 n0
8 -41
The dynamic programming approach
• Make a decision as which of the ai’s should be assigned
to the root node of the tree.
• If we choose ak, then it is clear that the internal nodes for
a1,a2,……,ak-1 as well as the external nodes for the
classes E0, E1,….,Ek-1 will lie in the left subtree l of the
root. The remaining nodes will be in the right subtree r.
ak
P1. .Pk-1 Pk+1..Pn
Q0..Qk-1 Qk..Qn
a1...ak-1 ak+1...an
C(1,k-1)
cost( l)= ∑ p(i)*level(ai) + ∑ q(i)*(level(Ei)-1)
1≤i<k 0≤i<k
cost(r)=
j
• Using w(i, j) to represent the sum q(i) +∑ l=i+1( q(l)+p(l) ),
we obtain the following as the expected cost of the
above search tree.
We can generalize the above formula for any c(i,j) as shown below
do int
while
Ex 2: Let n=4, and ( a1,a2,a3,a4 ) = (count, float, int,while).
Let p(1 : 4 ) =( 1/20, 1/5, 1/10, 1/20) and
q(0: 4) = ( 1/5,1/10, 1/5,1/20,1/20 ).
• Therefore, the total time to evaluate all the c(i, j)’s and
r(i, j)’s is
∑ ( mn – m2 ) = O(n3)
1≤m≤n
• We can reduce the time complexity by using the
observation of D.E. Knuth
• Observation:
• The optimal k can be found by limiting the search
to the range r( i, j – 1) ≤ k ≤ r( i+1, j )
for m:= 2 to n do
{
for i := 0 to n – m do
{
j:= i + m ;
w[ i, j ]:= p[ j ] + q[ j ] + w[ i, j -1 ];
c[ i, j ] := w[ i, j ] + c[ i, x -1 ] + c[ x, j ];
r[ i, j ] :=x;
}
}
Algorithm Find( c, r, i, j )
{
for k := r[ i, j -1 ] to r[ i+1, j ] do
{ min :=∞;
if ( c[ i, k -1 ] +c[ k, j ] < min ) then
{
min := c[ i, k-1 ] + c[ k, j ]; y:= k;
}
}
return y;
}
Traveling Salesperson Problem (TSP)
Problem:-
1 2 3 4
• Cost matrix: 1 0 2 10 5
2 2 9
0 0 4
3 4
3
4 6 8 7 0
The dynamic programming approach
gV(-
{11m,
}
{1)kcgi
V(-{nk
2kn
1
• The general form:
gS(im),
{iijcngS(-{j,
2
jS
• Equation 1 can be solved for g( 1, V- {1} ) if we know
g( k, V- {1,k} ) for all choices of k.
Clearly,
g( i, Ø ) = Ci1 , 1≤ i ≤ n.
g(4, Ø)=C41=6
We can obtain
g(2, {3})=C23 + g(3, Ø)=9+4=13
g(2, {4})=C24 + g(4, Ø)=∞
Floyd-Warshall Algorithm
All-Pairs Shortest Path Problem
• General formula
Vk d k-1[k, j]
d k-1[i, k]
Vj
Vi
d k-1[i, j]
d1 =
d2 =
d3 =
d4 =
d5 =
Algorithm
Algorithm AllPaths( c, d, n )
// c[1:n,1:n] cost matrix
// d[i,j] is the length of a shortest path from i to j
{
for i := 1 to n do
for j := 1 to n do
d [ i, j ] := c [ i, j ] ; // copy c into d
for k := 1 to n do
for i := 1 to n do
for j := 1 to n do
d [ i, j ] := min ( d [ i, j ] , d [ i, k ] + d [ k, j ] );
}
Time Complexity is O ( n 3 )
0/1 Knapsack Problem
Let xi = 1 when item i is selected and let xi = 0
when item i is not selected.
n
maximize pi xi
i=
1n
subject to wi xi <= c
i=1
and xi = 0 or 1 for all i
All profits and weights are positive.
Sequence Of Decisions
General formula
f(1,c)
f(2,c) f(2,c-w1)
f(5,c)
f(5,c-w1 –w3 –w4)
• We use set si is a pair ( P, W )
where P= fi(y), W=y
• Note That s0 =( 0, 0 )
D1 D2 D3 … Dn
D D
D
1
2
3 … D
D D n
1
D 3 D
2
D D n
maximize π Ф i ( mi )
1≤i≤n
subjected to ci mi ≤ c
1≤i≤n
m i ≥1 and integer,
1≤i≤n
Dynamic programming approach
ui=
( c + c i - ∑ c j )C i
1≤j≤n
General formula
fi(x)= m1≤ax
m i ≤ u i ( mi ) fi-1(x - ci mi ) }
Where f = f i( x )