Dynamic Programming
Dynamic Programming
Programming
Dynamic Programming
Dynamic
f(3)
f(2)
f(1)
f(2)
f(1)
Programming calculates
bottom to top
Values are stored for later use.
This reduces repetitive calculation.
from
Multistage Graphs
11
18
16
13
1
11
d(A, T)
18
16
B
5
d(B, T)
13
d(C, T)
d(S, T) =
4
11
D
E
d(D, T)
d(E, T)
Dynamic programming
4
1
11
18
16
13
Backward approach
(forward reasoning)
d(S, A) = 1
d(S, B) = 2
d(S, C) = 5
1
11
18
16
13
d(S,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
Principle of optimality-
the shortest
path problem
Dynamic programming
2
3
1
2
4
3
5
3
2
2
4
3
2
5
2
3
Applications of TSP
Collection
Robotics
Board
drilling
g(1,V-{1}) =
g(i,S) =
Example
For |S|=0
g(2,)=c =5
21
g(3,)=c31=6
g(4,)=c41=8
For |S|=1
g(2,{3})=c +g(3,)=15
23
g(2,{4})=c24+g(4,)=18
g(3,{2})=c32+g(2,)=18
g(3,{4})=c34+g(4,)=20
g(4,{2})=c42+g(2,)=13
g(4,{3})=c43+g(3,)=15
3
(a)
(b)
Example
For |S|=2
g(2,{3,4}) = min {c +g(3,{4}), c +g(4,{3})} = 25
23
24
For |S|=3
g(1,{2,3,4} = min {c +g(2,{3,4}), c +g(3,{2,4}),
12
13
c14+g(4,{2,3})} =
= min {35, 40, 43} = 35
g(1,V-{1}) =
Example 2
Time complexity
Let N be the number of g(i,S) that have to be
computed before it can be used to compute g(1,V-{1})
N=
for
while
do
return
if
4 comparisons
in worst case
return
do
if
3 comparisons
in worst case
while
for
while
do
return
return
do
if
while
if
(b)
(a)
if
while
if
do
if
while
while
do
(c)
(b)
(a)
while
do
do
while
if
(d)
if
(e)
Let rij be the root of Tij and let wij be the weight of Tij, where
OPTIMALBST(p,q,n)
Example:
i
pi
qi
0.05
0.15
0.10
0.05
0.10
0.20
0.10
0.05
0.05
0.05
0.10
e
1
5
3
0.45
0.05
1.75
0.90
1
0
1.20
0.70
0.10
0.05
1.30
0.60
0.25
0.40
2.00
0.90
0.05
0.55
0.45
0.30
0.10
0.05
0.80
0.50
0.35
0.25
0.10
1.00
0.70
0.50
0.30
0.05
2.75
1.25
0.60
0.30
0.15
0.05
0.20
0.05
0.05
3
2
2
2
2
4
2
2
1
2
3
5
4
5
4
4
0.35
root
5
0.50
5
5
0.10
k2
k1
d0
k5
d1
d5
k4
i
d4
k3
pi
0.15
0.10
0.05
0.10
0.20
d2
qi
0.05
0.10
0.05
0.05
0.05
0.10
d3
Example: key
A B C D
probability 0.1 0.2 0.4 0.3
The tables below are filled diagonal by diagonal: the left one is filled
using the recurrence and the right one, for trees roots, records ks
values giving the minima
.1
.4
1.1 1.7
A
optimal BST
.2
.8 1.4
Example
Let n=4, (a1, a2, a3, a4) = (do, if return, while). Let (p1, p2, p3,
p4)=(3,3,1,1) and (q0, q1, q2, q3, q4)=(2,3,1,1,1). wii = qii, cii=0,
and rii=0, 0 i 4.
Example
0
3
4
0
w00=2
c00=0
r00=0
w01=8
c01=8
r01=1
w02=12
c02=19
r02=1
w03=14
c03=25
r03=2
w04=16
c04=32
r04=2
1
w11=3
c11=0
r11=0
w12=7
c12=7
r12=2
w13=9
c13=12
r13=2
w14=11
c14=19
r14=2
2
w22=1
c22=0
r22=0
w23=3
c23=3
r23=3
w24=5
c24=8
r24=3
3
w33=1
c33=0
r33=0
w34=3
c34=3
r34=4
4
w44=1
c44=0
r44=0
Computation Complexity of
Optimal Binary Search Tree
3
5
v5
3
v2
9
v4
2
2
4
v3
The subproblems
How can we define the shortest distance di,j in
terms of smaller problems?
One way is to restrict the paths to only include
vertices from a restricted subset.
Initially, the subset is empty.
Then, it is incrementally increased until it
includes all the vertices.
The sub-problems
Let
D(0)=W
D(n)=D which is the goal matrix
How
Floyds Algorithm 95
Vi
Vj
Shortest Path using intermediate vertices { V1, . . . Vk -1 }
Since
D(k)[i,j]= D(k-1)[i,j] or
D(k)[i,j]= D(k-1)[i,k]+ D(k-1)[k,j].
We conclude:
D(k)[i,j]= min{ D(k-1)[i,j], D(k-1)[i,k]+ D(k-1)[k,j] }.
Vi
Vj
Shortest Path using intermediate vertices { V1, . . . Vk -1 }
Floyd's Algorithm
//Computes shortest distance between all pairs of
//nodes, and saves P to enable finding shortest paths
1. D0 W // initialize D array to W [ ]
2. P 0 // initialize P array to [0]
3. for k 1 to n
4.
do for i 1 to n
5.
do for j 1 to n
6.
if (Dk-1[ i, j ] > Dk-1 [ i, k ] + Dk-1 [ k, j ] )
7.
then Dk[ i, j ] Dk-1 [ i, k ] + Dk-1 [ k, j ]
8.
P[ i, j ] k;
9.
else Dk[ i, j ] Dk-1 [ i, j ]
1. D0 W // initialize D array to W [ ]
2. P 0 // initialize P array to [0]
3. for k 1 to n
4.
do for i 1 to n
5.
do for j 1 to n
6.
Dk[i, j ] min(Dk-1[i, k] +Dk-1[k,j ])
Example
W=D =
0
1
4
-3
3
-3
P=
5
2
-3
D1 =
P=
D =
0
-3
-3
k=1
Vertex 1 can be
intermediate
node
5
2
-3
D2 =
P=
D1 = 1
-3
-1
-3
k=2
Vertices 1, 2 can
be intermediate
2
-3
D =
3
P=
-1
-3
D2 =
1
3
-1
-3
k=3
Vertices 1, 2, 3
can be
intermediate
Time Complexity
FLOYD(G)
for i,j in [1..n]
d[i,j]=w(ui,uj)
for k in [1..n]
for i in [1..n]
for j in [1..n]
d[i,j]=min(d[i,j],d[i,k]+d[k,j])
Time complexity: O(n3)
Advantages of Dynamic
programming
A
Space reduction
To
3
w
Space reduction
Specifying