10 - APS - Dynamic Programming
10 - APS - Dynamic Programming
Dynamic programming
Damjan Strnad
2
Dynamic programming
●
dynamic programming is a method for solving
optimization problems that:
– consist of similar subproblems that overlap (i.e., have
common subsubproblems)
– have optimal substructure property (i.e., optimal solution is
built from optimal solutions of subproblems)
●
dynamic programming is most often used for solving
optimization problems in four steps:
1)characterize the structure of an optimal solution
2)recursively define the value of an optimal solution
3)compute the value of an optimal solution in a bottom-up
fashion
4)construct an optimal solution from computed information
3
Dynamic programming
●
DP is similar to divide-and-conquer strategy, but the latter
solves problems with independent subproblems:
– with overlapping subproblems, divide-and-conquer
redundantly solves the same subproblems multiple times
– dynamic programming solves each subproblem once and
remembers its solution
– the term „programming“ in the name of the strategy does
not refer to writing code but to tabular storage of already
calculated results (tabulation)
●
DP is similar to greedy strategy:
– both are based on optimal substructure property
– greedy method does not construct the problem solution
from subproblem solutions but in each step adds a „locally“
optimal solution element to the complete solution
4
Floyd-Warshall algorithm
●
solves the all-pairs shortest paths problem for a weighted
directed graph G=(V,E) with cost function w: E ℝ
●
it is based on dynamic programming and is more efficient
than a sequential search of shortest paths from single
source
●
the graph is described by adjacency (weight) matrix W:
{
0, if i= j
w ij = weight (i , j), if i≠ j and (i , j)∈E
∞, if i≠ j and (i , j)∉E
●
the weights can be negative but we assume there are no
cycles with negative weight
6
Floyd-Warshall algorithm
●
optimal solution structure:
– let V={1,2,…,n} be the set of nodes of graph G
– let p be the shortest path from node i to node j that goes through
a subset of nodes Q={1,2,…,k}:
●
all intermediate nodes on path p are in Q (have index ≤k)
●
the reverse does not hold necessarily (some nodes from Q are not
in p)
– we can define a relation between p and shortest paths whose
intermediate nodes are in Q'={1,2,…,k-1}:
●
if node k is not in p, then p is
equal to the shortest path from
i to j with intermediate nodes in Q'
●
if node k is in p, then p can be
split into two subpaths p1 and p2
whose intermediate nodes are in Q'
7
Floyd-Warshall algorithm
● the optimal solution is built in matrix D=(dij), where dij is
the weight of the shortest path from i to j
●
the algorithm iterations are labelled using the superscript
index: D(k)=(dij(k)), where dij(k) is the weight of the shortest
path from i to j through the set of nodes {1,2,3,...,k}
●
recursive solution definition:
– if k=0, then d (k)=w (direct link between nodes)
ij ij
– if k≥1, then the weight of the shortest path from i to j is the
shorter of paths going through and paths going past node
k; their lengths are already computed in D(k-1)
(k )
d =
ij
{
w ij ,
min ( d (k−1)
ij ,d (k−1)
ik +d (k−1)
kj ),
if k=0
if k≥1
8
Floyd-Warshall algorithm
● the optimal solution is built in matrix D=(dij), where dij is
the weight of the shortest path from i to j
●
the algorithm iterations are labelled using the superscript
index: D(k)=(dij(k)), where dij(k) is the weight of the shortest
path from i to j through the set of nodes {1,2,3,...,k}
●
recursive solution definition:
– if k=0, then d (k)=w (direct link between nodes)
ij ij
– if k≥1, then the weight of the shortest path from i to j is the
shorter of paths going through and paths going past node
k; their lengths are already computed in D(k-1)
{
w ijthe, length of shortest path from i to j past
if node k (already known)
(k ) k=0
d =
ij
min ( d (k−1)
ij ,d (k−1)
ik +d (k−1)
kj ), if k≥1
9
Floyd-Warshall algorithm
● the optimal solution is built in matrix D=(dij), where dij is
the weight of the shortest path from i to j
●
the algorithm iterations are labelled using the superscript
index: D(k)=(dij(k)), where dij(k) is the weight of the shortest
path from i to j through the set of nodes {1,2,3,...,k}
●
recursive solution definition:
– if k=0, then d (k)=w (direct link between nodes)
ij ij
– if k≥1, then the weight of the shortest path from i to j is the
shorter of paths going through and paths going past node
k; their lengths arethealready computed in D (k-1)
length of shortest path from i to j through node k (the
{
lengths of both parts from i to k and from k to j already known)
(k ) w ij , if k=0
d =
ij
min ( d (k−1)
ij ,d (k−1)
ik +d (k−1)
kj ), if k≥1
10
Floyd-Warshall algorithm
●
bottom-up calculation of optimal solution value:
FLOYD-WARSHALL(W)
n ← number of rows in W
D(0) ← W
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
dij(k) ← min(dij(k-1),dik(k-1) + dkj(k-1))
return D(n)
Floyd-Warshall algorithm
●
construction of optimal solutions:
– based on matrix D(n) we can construct the predecessor
matrix Π=(πij), where πij is the predecessor of node j on the
shortest path from node i
– Π can also be calculated by constructing the sequence Π(0),
Π(1), …, Π(n), where π(k)ij denotes the predecessor of node j
on the shortest path from node i through subset {1,2,…,k}
(0)
{
π ij = NIL , if i= j or w ij =∞
i, if i≠ j and w ij <∞
{
(k−1) (k−1) (k−1) (k−1)
(k ) π
π = (k−1)
ij , if d ij ≤d ik +d kj
ij
π kj , if d (k−1)
ij >d (k−1)
ik +d (k−1)
kj
– the shortest paths can be reconstructed from Π=Π(n)
12
3
8 1
-3 -4
9 5 -2
4 2
7
13
7 2 6
3
8 1
-3 -4
9 5 -2
4 2
7
[ ]
0 5 8 −4 ∞
−2 0 ∞ ∞ ∞
(0)
D =W= ∞ −3 0 9 ∞
∞ 7 ∞ 0 2
6 ∞ 7 ∞ 0
[ ]
NIL 1 1 1 NIL
2 NIL NIL NIL NIL
(0)
Π = NIL 3 NIL 3 NIL
NIL 4 NIL NIL 4
5 NIL 5 NIL NIL
14
[ ]
– 5→4: as d(0)54 > d(0)51 + d(0)14 (∞>6+(-4)) 0 5 8 −4 ∞
−2 0 ∞ ∞ ∞
(0)
D =W= ∞ −3 0 9 ∞
∞ 7 ∞ 0 2
6 ∞ 7 ∞ 0
[ ]
NIL 1 1 1 NIL
2 NIL NIL NIL NIL
(0)
Π = NIL 3 NIL 3 NIL
NIL 4 NIL NIL 4
5 NIL 5 NIL NIL
15
[ ]
– 5→4: as d(0)54 > d(0)51 + d(0)14 (∞>6+(-4)) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(1)
D =W= ∞ −3 0 9 ∞
∞ 7 ∞ 0 2
6 11 7 2 0
[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(1)
Π = NIL 3 NIL 3 NIL
NIL 4 NIL NIL 4
5 1 5 1 NIL
16
[ ]
– 4→3: as d(1)43 > d(1)42 + d(1)23 (∞>7+6) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(1)
D =W= ∞ −3 0 9 ∞
∞ 7 ∞ 0 2
6 11 7 2 0
[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(1)
Π = NIL 3 NIL 3 NIL
NIL 4 NIL NIL 4
5 1 5 1 NIL
17
[ ]
– 4→3: as d(1)43 > d(1)42 + d(1)23 (∞>7+6) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(2)
D =W= −5 −3 0 −9 ∞
5 7 13 0 2
6 11 7 2 0
[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(2)
Π = 2 3 NIL 1 NIL
2 4 1 NIL 4
5 1 5 1 NIL
18
[ ]
– 5→4: as d(2)54 > d(2)53 + d(2)34 (2>7+(-9)) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(2)
D =W= −5 −3 0 −9 ∞
5 7 13 0 2
6 11 7 2 0
[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(2)
Π = 2 3 NIL 1 NIL
2 4 1 NIL 4
5 1 5 1 NIL
19
[ ]
– 5→4: as d(2)54 > d(2)53 + d(2)34 (2>7+(-9)) 0 5 8 −4 ∞
−2 0 6 −6 ∞
(3)
D =W= −5 −3 0 −9 ∞
5 7 13 0 2
2 4 7 −2 0
[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(3)
Π = 2 3 NIL 1 NIL
2 4 1 NIL 4
2 3 5 1 NIL
20
[ ]
– 2→5: as d(3)25 > d(3)24 + d(3)45 (∞>-6+2) 0 5 8 −4 ∞
– 3→5: as d(3)35 > d(3)34 + d(3)45 (∞>-9+2) (3)
−2 0 6 −6 ∞
D =W= −5 −3 0 −9 ∞
5 7 13 0 2
2 4 7 −2 0
[ ]
NIL 1 1 1 NIL
2 NIL 1 1 NIL
(3)
Π = 2 3 NIL 1 NIL
2 4 1 NIL 4
2 3 5 1 NIL
21
[ ]
– 2→5: as d(3)25 > d(3)24 + d(3)45 (∞>-6+2) 0 3 8 −4 −2
– 3→5: as d(3)35 > d(3)34 + d(3)45 (∞>-9+2) (4)
−2 0 6 −6 −4
D =W= −5 −3 0 −9 −7
5 7 13 0 2
2 4 7 −2 0
[ ]
NIL 4 1 1 4
2 NIL 1 1 4
(4)
Π = 2 3 NIL 1 4
2 4 1 NIL 4
2 3 5 1 NIL
22
[ ]
– 4→1: as d(3)41 > d(3)45 + d(3)51 (5>2+2) 0 3 8 −4 −2
– 4→2: as d(3)42 > d(3)45 + d(3)52 (7>2+4) −2 0 6 −6 −4
(4)
D =W= −5 −3 0 −9 −7
– 4→3: as d(3)43 > d(3)45 + d(3)53 (13>2+7) 5 7 13 0 2
2 4 7 −2 0
[ ]
NIL 4 1 1 4
2 NIL 1 1 4
(4)
Π = 2 3 NIL 1 4
2 4 1 NIL 4
2 3 5 1 NIL
23
[ ]
– 4→1: as d(3)41 > d(3)45 + d(3)51 (5>2+2) 0 2 5 −4 −2
– 4→2: as d(3)42 > d(3)45 + d(3)52 (7>2+4) −2 0 3 −6 −4
(5)
D =W= −5 −3 0 −9 −7
– 4→3: as d(3)43 > d(3)45 + d(3)53 (13>2+7) 4 6 9 0 2
2 4 7 −2 0
[ ]
NIL 3 5 1 4
2 NIL 5 1 4
(5)
Π = 2 3 NIL 1 4
2 3 5 NIL 4
2 3 5 1 NIL
24
example: 7 2 6
– shortest path 1→2: 8
3 1
● the parent of 2 on path from 1 is π(5)12=3
-3 -4
● the parent of 3 on path from 1 is π(5)13=5 9 5 -2
● the parent of 5 on path from 1 is π(5)15=4 4 2
7
the parent of 4 on path from 1 is π(5)14=1
[ ]
●
0 2 5 −4 −2
●
shortest path is 1-4-5-3-2 −2 0 3 −6 −4
(5)
D =W= −5 −3 0 −9 −7
– shortest path 4→3: 4 6 9 0 2
● the parent of 3 on path from 4 is π(5)43=5 2 4 7 −2 0
[ ]
● the parent of 5 on path from 4 is π(5)45=4 NIL 3 5 1 4
●
shortest path is 4-5-3 2 NIL 5 1 4
(5)
Π = 2 3 NIL 1 4
2 3 5 NIL 4
2 3 5 1 NIL
25
Bellman-Held-Karp algorithm
●
optimal solution structure: suppose the start node is node 1
from which we continue to node k: k
– the circular path consists of edge (1,k) and path
from node k back to node 1 through all other 1
nodes V – {1,k} V-{1,k}
– let g(i,S) denote the length of shortest path that
starts in i, goes through all nodes in set S exactly once and
finishes in node 1
– S can contain all graph nodes except node 1, so its size can be |
S|=1, 2,…, n-1
– the length of the shortest circular path is given by g(1,S) for
S=V – {1}, |S|=n-1
– according to the optimality principle the following holds:
g(1 , V −{1})= min {c [1 , k ]+ g(k , V −{1 , k })}
2≤k≤n
27
Bellman-Held-Karp algorithm
●
recursive solution definition: assume more generally that
we are in node i and still have to visit the nodes in S:
– for the continuation of the circular path, we will select the
node j∈S for which the sum of edge (i,j) cost and the cost of
the rest of circular path is minimal (Bellman equation):
g(i , S)=min {c [i , j]+ g( j , S−{ j})}, for 1<i≤n
j∈S
– if the set S is empty, we have visited all nodes and only need
to return to the start node:
g(i , ∅)=c [i , 1] , for 1<i≤n
●
Bellman equation represents the recursive description of a
solution, which is constructed bottom-up (from |S|=0
towards |S|=n-1)
28
Bellman-Held-Karp algorithm
●
reconstruction of optimal circular path:
– let J(i,S) denote the value of j that minimizes the right
side of the Bellman equation:
g(i , S)=min {c [i , j]+ g( j , S−{ j})}, for 1<i≤n
j∈S
[ ]
– for |S|=0 we get: 0 5 11 4
g(2 , ∅)=c [2,1]=3 3 0 6 2
C=
1 8 0 7
g(3 , ∅)=c [3,1]=1
6 4 9 0
g(4 , ∅)=c [ 4,1]=6
30
[ ]
– for |S|=0 we get: 0 5 11 4
g(2 , ∅)=c [2,1]=3 3 0 6 2
C=
1 8 0 7
g(3 , ∅)=c [3,1]=1
6 4 9 0
g(4 , ∅)=c [ 4,1]=6
– for |S|=1 we get:
g(2 , {3})=c [2,3]+ g(3 , ∅)=6+1=7 J (2 , {3})=3
g(2 , {4 })=c [2,4 ]+ g(4 , ∅)=2+6=8 J (2 , {4 })=4
g(3 , {2})=c [3,2]+ g(2 , ∅)=8+3=11 J (3 , {2})=2
g(3 , {4 })=c [3,4 ]+ g(4 , ∅)=7+6=13 J (3 , {4 })=4
g(4 , {2})=c [ 4,2]+ g(2 , ∅)=4+3=7 J (4 , {2})=2
g(4 , {3})=c [ 4,3]+ g(3 , ∅)=9+1=10 J (4 , {3})=3
31
[ ]
– for |S|=2 we get: 0 5 11 4
g(2 , {3,4 })= min {c [2,3]+ g(3 , {4 }), c [2,4 ]+ g(4 , {3})}= 3 0 6 2
j∈{3,4} C=
1 8 0 7
=min {6+13,2+10}=12
6 4 9 0
g(3 , {2,4 })= min {c [3,2]+ g(2 , {4 }), c [3,4 ]+ g(4 , {2})}=
j∈{2,4}
J (2 , {3,4 })=4
=min {8+8,7+7}=14
J (3 , {2,4 })=4
g(4 , {2,3})= min {c [ 4,2]+ g(2 , {3}), c [ 4,3]+ g(3 , {2})}=
j∈{2,3} J (4 , {2,3})=2
=min {4+7,9+11}=11
32
[ ]
– for |S|=2 we get: 0 5 11 4
g(2–, {3,4 })= min {c [2,3]+ g(3 , {4 }), c [2,4 ]+ g(4 , {3})}= 3 0 6 2
j∈{3,4} C=
1 8 0 7
=min {6+13,2+10}=12
6 4 9 0
g(3 , {2,4 })= min {c [3,2]+ g(2 , {4 }), c [3,4 ]+ g(4 , {2})}=
j∈{2,4}
J (2 , {3,4 })=4
=min {8+8,7+7}=14
J (3 , {2,4 })=4
g(4 , {2,3})= min {c [ 4,2]+ g(2 , {3}), c [ 4,3]+ g(3 , {2})}=
j∈{2,3} J (4 , {2,3})=2
=min {4+7,9+11}=11
– for |S|=3 we get:
g(1 , {2 , 3,4 })= min {c [1,2]+ g(2 , {3,4 }), c [1,3]+ g(3 , {2,4 }), c [1,4 ]+ g(4 , {2,3})}=
j∈{2,3,4}
=min {5+12,11+14,4+11}=15
J (1 , {2,3,4 })=4 the length of shortest tour
33
●
reconstruction of optimal path:
– the first intermediate node is J(1,{2,3,4})=4
– the next intermediate node is J(4,{2,3})=2
– the last intermediate node is J(2,{3})=3
– optimal circular path is 1-4-2-3-1
Bellman-Held-Karp algorithm
●
algorithm time complexity:
– the size of set S is increased from 1 to n-1 (k=1,...,n-1)
if z3 z0 if for z3
z0 z1 z2 z3 z1 z2
38
if z3 z0 z1 z2 z3
z1 z2
39
k=r0,n
a=r0,k-1 b=rk,n
OPTIMAL-BINARY-SEARCH-TREE(n,a,p,q,T)
% inicialization of empty trees and trees with one node
for i ← 0 to n-1 do
[wii,cii,rii] ← [qi,0,0]
[wi,i+1,ci,i+1,ri,i+1] ← [qi+pi+1+qi+1,qi+pi+1+qi+1,i+1]
% inicialization of empty tree Tnn
[wnn,cnn,rnn] ← [qn,0,0]
for m ← 2 to n do % find optimal trees with m nodes
for i ← 0 to n-m do
j ← i + m
wij ← wi,j-1 + pj + qj
k ← value [t] that minimizes {ci,t-1 + ct,j} given
ri,j-1 ≤ t ≤ ri+1,j
cij ← wij + ci,k-1 + ckj
rik ← k
50
r i , j−1≤k≤r i+1 , j
51
●
a=[for, if, while], p=[4/15, 2/15, 1/15], q=[2/15, 3/15, 2/15, 1/15]
● we can safely eliminate fractions: p=[p1,p2,p3]=[4,2,1],
q=[q0,q1,q2,q3]=[2,3,2,1] and summarize the procedure in a table:
●
m\i 0 1 2 3
0 T00 : 2, 0, 0 T11 : 3, 0, 0 T22 : 2, 0, 0 T33 : 1, 0, 0
1 T01 : 9, 9, 1 T12 : 7, 7, 2 T23 : 4, 4, 3
2 T02 : 13, 20, 1 T13 : 9, 13, 2
3 T03 : 15, 28, 1 (2)
– m is the number of keys included in a tree and i is the index of the
first key (i.e., the included keys are ai+1,...,ai+m)
– for each tree Ti,i+m three values are reported: wi,i+m, ci,i+m, ri,i+m
52
T00 T13
m\i 0 1 2 3
0 T00 : 2, 0, 0 T11 : 3, 0, 0 T22 : 2, 0, 0 T33 : 1, 0, 0
1 T01 : 9, 9, 1 T12 : 7, 7, 2 T23 : 4, 4, 3
2 T02 : 13, 20, 1 T13 : 9, 13, 2
3 T03 : 15, 28, 1 (2)
53
m\i 0 1 2 3
0 T00 : 2, 0, 0 T11 : 3, 0, 0 T22 : 2, 0, 0 T33 : 1, 0, 0
1 T01 : 9, 9, 1 T12 : 7, 7, 2 T23 : 4, 4, 3
2 T02 : 13, 20, 1 T13 : 9, 13, 2
3 T03 : 15, 28, 1 (2)
54