IE 519 Dynamic Programming
IE 519 Dynamic Programming
EXAMPLE # 1
Suppose the network below represents the streets of a city. The numbers in the arcs represent
time (distance or cost) required to traverse each block. Find the shortest path from node A to
node B.
6 links/path.
A path contains 3 ups and 3 downs.
6 6!
Number of paths from A to B = = = 20 .
3 3! (6 3)!
Effort to find length (cost) of a path = 5 adds.
Effort to compare 20 paths = 19 compares.
Computational effort to find shortest path = ( 5 20 = 100 adds and 19 compares).
Compare addition.
1
Dynamic Programming Approach
C
SC = value of shortest path from C to B.
2
1
SD = value of shortest path from C to B.
D
2 + SC
SA = min .
1 + SD
Principle of Optimality
C
A B
Best path from A to B has the property that, whatever the next point (node) is after A, the
remaining path to B must be the best path from that point to B.
6 + SE
SC = min ,
5 + SF
8 + SF
SD = min .
4 + SG
....
Note that SH = 4 + SL ,
or, equivalently, SH = min .
4 + SL
....
2
Dynamic Programming: Terminology and Formulation
Rule that associates the best first decision with each subproblem.
2 + SC
Recurrence Relation(s) ( e.g., SA = min ):
1 + SD
Relation(s) among various values of S obtained after applying the principle of optimality.
Boundary Condition(s) ( SB = 0 ) :
Obvious values of Sx for certain argument values obtained from the statement of the
problem.
Answer ( SA ) :
Optimal value and decision for the value of the argument(s) that represent the whole
problem.
SOLUTION OF EXAMPLE # 1
SB = 0,
SO = 3 + SB = 3, PO = B,
SP = 2 + SB = 2, PP = B,
SL = 6 + SO = 9, PL = O,
3 + SO
SM = min = 6, PM = O,
9 + S P
SN = 5 + SP = 7, PN = P,
SH = 4 + SL = 13, PH = L,
4 + SL
SI = min = 11, PI = M,
5 + S M
3
3 + S M
SJ = min = 9, PJ = M,
3 + S N
SK = 3 + SN = 10, PK = N,
3 + S H
SE = min = 13, PE = I,
2 + SI
2 + S I
SF = min = 12, PF = J,
3 + S J
6 + SJ
SG = min = 15, PG = J or K,
5 + S K
6 + S E
SC = min = 17, PC = F,
5 + S F
8 + SF
SD = min = 19, PD = G,
4 + S G
2 + S C
SA = min = 19, PA = C,
1 + S D
Shortest Path: A - C - F - J - M - O - B.
4
Computational Efficiency
ARG: (x , y ) = node.
a (x , y ) + S(x + 1, y + 1)
RR: S(x , y ) = min u ,
a d (x , y ) + S(x + 1, y 1)
x = 5, 4, 3 ; y = (6 x ) , (6 x ) 2 , . . . , (6 x ) ,
x = 2, 1, 0 ; y = x , x 2, . . ., x
(note that y decreases in intervals of 2),
BC: S(6,0 ) = 0 .
ANS: S(0,0 ) .
5
EXAMPLE # 2
Find the shortest path from node A to line B. Note that there are rebates at the end nodes.
ARG: = node.
OPF: = up or down .
RR: ,
x = 3, 2, 1, 0;
(note that y decreases in intervals of 2).
BC: , , , , .
ANS: .
6
SOLUTION OF EXAMPLE # 2
S(4,4) = -2, S(4,2) = -1, S(4,0) = -3, S(4,-2) = -4, S(4,-4) = -3,
3 + S(4,4)
S(3,3) = min = 0, P(3,3) = down,
1 + S(4,2)
2 + S(4,2)
S(3,1) = min = 1, P(3,1) = up,
5 + S(4,0)
5 + S(4,0)
S(3,-1) = min = -1, P(3,-1) = down,
3 + S(4,2)
6 + S(4,2)
S(3,-3) = min = 2, P(3,-3) = up or down,
5 + S(4,4)
6 + S(3,3)
S(2,2) = min = 3, P(2,2) = down,
2 + S(3,1)
3 + S(3,1)
S(2,0) = min = 1, P(2,0) = down,
2 + S(3,1)
4 + S(3,1)
S(2,-2) = min = 3, P(2,-2) = up,
2 + S(3,3)
2 + S(2,2)
S(1,1) = min = 4, P(1,1) = down,
3 + S(2,0)
5 + S(2,0)
S(1,-1) = min = 6, P(1,-1) = up,
4 + S(2,2)
1 + S(11
,)
S(0,0) = min = 5, P(0,0) = up,
0 + S(1,1)
7
COMPUTATIONAL EFFICIENCY
C
E
A B
G
D
Dynamic Programming
8
2
N
nodes with 2 adds and 1 compare.
2
N2 N2
Computational effort (DP) = + N adds and compares.
2 4
Total Enumeration
N
Total adds = (N 1) N ,
2
N
Total compares N .
2
N = 20
Adds Compares
DP 220 100
TE 3 106 184,000
E C
Line B
G D
Stage 0 1 2 3 4 5 6 7 8 ( N = 8)
# vertices 1 2 3 4 5 6 7 8 9 ( N +1 = 9 )
9
Shortest path from node A = (0,0 ) to line B.
Assume N stages.
Dynamic Programming
2 adds and 1compare / node, except (N + 1) nodes at last stage (stage N).
N (N + 1)
N
Number of nodes = 1 + 2 + 3 + ... + N = i = .
i =1 2
N (N + 1)
Computational effort (DP) = N (N + 1) adds and compares.
2
Total Enumeration
N = 20
Adds Compares
DP 420 210
TE 2010 6 10 6
S(x-1, y+1)
ad (x-1, y+1) S(x-1, y+1) = value of shortest path from (0, 0) to (x-1, y+1).
S(x, y)
au (x-1, y-1)
S(x-1, y-1) = value of shortest path from (0, 0) to (x-1, y-1).
S(x-1, y-1)
10
Principle of Optimality for Forward Dynamic Programming Formulation
A B
If C is last node in optimal A to B path, then the sub-path from A to C is optimal for the min
path problem from A to C.
ARG: (x , y ) = node.
a (x 1, y 1) + S(x 1, y 1)
RR: S(x , y ) = min u ,
a d (x 1, y + 1) + S(x 1, y + 1)
x = 1, 2, 3 ; y = x , x 2, . . ., x ,
x = 4, 5, 6 ; y = (6 x ) , (6 x ) 2 , . . . , (6 x )
(note that y decreases in intervals of 2).
BC: S(0,0 ) = 0 .
ANS: S(6,0 ) .
No penalty
Penalty = add 3
11
Find the shortest path from node A = (0,0 ) to node B = (6,0 ) considering turn penalties.
3
2 3
1 3 5
5
1
1 1 2
4 4 2
A B
1 2 3 4 5 6
7 2 2 8 1
0
-1
2
5 4
3
-2
4 2
-3
Backward Dynamic Programming Formulation for Shortest Path Problem with Turn
Penalties
OVF: S(x , y, z ) = value of min path from (x , y ) to (6,0 ) if the move from (x , y ) is up ( z = 0 ) or
down ( z = 1 ).
(x+1, y+1)
S(x+1, y+1, 0)
S(x+1, y+1, 1)
(x, y) au(x, y)
S(x, y, 0)
OPF: P(x , y, z ) = decision at the next node (continue or turn), given that move from (x , y ) is up
( z = 0 ) or down ( z = 1 ).
12
S(x + 1, y + 1, 0 ) (continue up )
S(x , y, 0) = a u (x , y ) + min
(turn at next node)
RR: ,
S(x + 1, y + 1, 1) + 3
x = 5, 4, 3 ; y = (6 x ) , (6 x ) 2 , . . . , (6 x ) ,
x = 2, 1, 0 ; y = x , x 2, . . ., x .
S(0,0,0 )
ANS: min .
S(0,0,1)
S10
3
2 3
S15
15 S
7
2
1 3 5
5
S13
S
20 14
S16 2
1
1 1 2
4 4 2
7
20 S14
12 S12 B S00
S17
A 1 2 3 4 5 6
7 2 2 8 1
0
-1
21
S17 9 S1
S10 2 4
5
3
-2
S14
14 S5
4 2
-3
S7
13
Note that S(4,0 ) = S(4,0 )12
7
,
2+ 8 +1+ 3
where = 7 = min and = 12 = min .
2+2+3 8+
Note also that the state space of this problem has increased by a factor of 2 with respect to the
state space of Example # 1.
N stage problem.
N
4 adds and 2compares / node, except end nodes at stages to (N 1) .
2
N 2
Number of nodes .
4
N2
Computational effort (DP) N 2 adds and compares.
2
CONSULTANT QUESTION
Suppose the process is ongoing. To determine the arguments (ARG) of OVF in a backward
formulation, we need to decide the minimum information a consultant needs to know to take
over the decision making process.
PROBLEM # 1.11
Solve shortest path from (0,0 ) to (N,0 ) in a network with diamond structure (see Figure 1.1)
with a turn penalty of k at the kth turn.
Finding the argument of OVF: What questions must the consultant ask to be able to take over the
decision making process?
14
Backward Dynamic Programming Formulation for Problem # 1.11
OVF: S(x , y, z, w ) = value of min path from (x , y ) to (N,0 ) , where z { 0, 1 } indicates arrival
at (x , y ) going up ( z = 0 ) or down ( z = 1 ), and w is the number of prior direction
changes.
a (x , y ) + S(x + 1, y + 1, 0, w )
RR: S(x , y, 0, w ) = min u ,
a d (x , y ) + S(x + 1, y 1, 1, w + 1) + (w + 1)
a u (x , y ) + S(x + 1, y + 1, 0, w + 1) + (w + 1)
S(x , y, 1, w ) = min ,
a d (x , y ) + S(x + 1, y 1, 1, w )
; y = (N x ) , (N x ) 2 , . . . , (N x ) ,
N
x = N 1, N 2, . . . ,
2
N N
x= 1, 2, . . . , 0 ; y = x , x 2, . . ., x ,
2 2
w = 0, 1, . . . , x .
S(0,0,0,0)
ANS: min .
S(0,0,1,0)
Concerns:
N2
Storage requirements (# nodes ) (values of z ) (values of w ) = 2 (N 1) .
2
Curse of dimensionality.
At each node (x , y ) , save value of shortest path, number of turns in it, and also possibly z,
indicating how you arrive to (x , y ) .
15
3
0 1 0
80 80 7
0
C
1
0 80 4 0
80 80 0
A 2 B
1 2 3 4 5 6
0 0 0 80 80
0 1 3
-1
80
80
80
80
-2
80 80
-3
PROBLEM # 1.14
Solve shortest path from (0,0 ) to (N,0 ) in a network with diamond structure (see Figure 1.1),
given that longest arc does not count.
Consultant questions:
OVF: S(x , y, z ) = value of min path from (x , y ) to (N,0 ) starting with z { 0, 1 } coupons.
a (x , y ) + S(x + 1, y + 1, 0)
RR: S(x , y, 0) = min u ,
a d (x , y ) + S(x + 1, y 1, 0)
; y = (N x ) , (N x ) 2 , . . . , (N x ) ,
N
x = N 1, N 2, . . . ,
2
N N
x= 1, 2, . . . , 0 ; y = x , x 2, . . ., x .
2 2
ANS: S(0,0,1) .
OVF: S(x , y ) = value of min path from (0,0 ) to (x , y ) without including length of longest arc.
L(x , y ) = length of arc not included in S(x , y ) .
S(x, y)
au (x-1, y-1)
S(x-1, y-1)
17
S(x 1, y + 1) + L(x 1, y + 1)
L(x , y ) = a d (x 1, y + 1)
swap
( + 1) + a d (x 1, y + 1)
do not swap
S x 1, y
L(x , y ) = L(x 1, y + 1)
S(x , y ) = min ,
S(x 1, y 1) + L(x 1, y 1)
RR:
swap
L(x , y ) = a u (x 1, y 1)
S(x 1, y 1) + a u (x 1, y 1)
do not swap
L(x , y ) = L(x 1, y 1)
N
x = 1, 2, . . . , ; y = x , x 2, . . ., x ,
2
+ 1, . . . , N 1, N ; y = (N x ) , (N x ) 2 , . . . , (N x ) .
N
x=
2
ANS: S(N,0 ) .
Counter-example:
100 1
800 1
1 800
S=4
L = 100
C
1 800 800 ad = 200
800 1
A B S = 104
L = 200
800 800
18
PROBLEM # 1.19
Solve shortest path from (0,0 ) to (N,0 ) in a network with diamond structure (see Figure 1.1),
given that you cannot turn at a node if you turned at the previous node.
Consultant questions
OVF: S(x , y, z, w ) = value of min path from (x , y ) to (N,0 ) , where z { 0, 1 } indicates arrival
at (x , y ) going up ( z = 0 ) or down ( z = 1 ), and w { 0, 1 } indicates that a turn is allowed
( w = 0 ) or not allowed ( w = 1 ).
a (x , y ) + S(x + 1, y + 1, 0, 0 )
S(x , y, 0, 0 ) = min u ,
a d (x , y ) + S(x + 1, y 1, 1, 1)
S(x , y, 1, 1) = a d (x , y ) + S(x + 1, y 1, 1, 0 ) ,
a (x , y ) + S(x + 1, y + 1, 0, 1)
S(x , y, 1, 0 ) = min u ,
a d (x , y ) + S(x + 1, y 1, 1, 0 )
; y = (N x ) , (N x ) 2 , . . . , (N x ) ,
N
x = N 1, N 2, . . . ,
2
N N
x= 1, 2, . . . , 0 ; y = x , x 2, . . ., x .
2 2
BC: S(N,0, z, w ) = 0 , z { 0, 1 } , w { 0, 1 } .
S(0,0,0,0)
ANS: min .
S(0,0,1,0)
19
Easier Backward DP Formulation
OVF: S(x , y, z ) = value of min path from (x , y ) to (N,0 ) , where z { 0, 1 } indicates arrival at
(x, y ) going up ( z = 0 ) or down ( z = 1 ).
(x+1, y+1)
S(x+1, y+1, 0)
au (x, y)
S(x, y, 0)
(x, y) ad (x, y)
S(x+2, y-2, 1)
(x+2, y-2)
a (x , y ) + S(x + 1, y + 1, 0 )
RR: S(x , y, 0 ) = min u ,
a d (x , y ) + a d (x + 1, y 1) + S(x + 2, y 2, 1)
S(x+2, y+2, 0)
au (x+1, y+1)
(x-1, y+1)
au (x, y)
(x+1, y+1)
S(x, y, 1)
(x, y) ad (x, y)
S(x+1, y-1, 1)
(x+1, y-1)
a (x , y ) + a u (x + 1, y + 1) + S(x + 2, y + 2, 0 )
S(x , y, 1) = min u .
a d (x , y ) + S(x + 1, y 1, 1)
20
DOUBLING-UP EXAMPLE
Arc costs depend on their initial and final y coordinates, but they are independent of the stage (x
coordinate). This pattern is called stage-invariant.
We want to go from line y = 0 to line y = 8 using the shortest path. At each iteration, we want to
double the duration (in stages) of the problem solved.
DP Formulation
OVF: = value of the min path of length k stages connecting y1 and y2.
4 4
y1 = 2 y2 = 2
2 6 2 6
5 5
y1 = 1 3 3
y2 = 1
1 1
4 2 4 2
y1 = 0 y2 = 0
5 5
0 k=1 2k= 2
8 8
y1 = 2 y2 = 2
7 3 7 3
10 10
y1 = 1 6 6 y2 = 1
5 5
3 5 3 5
y1 = 0 y2 = 0
6 6
k=2 2k= 4
21
Then, the value of the min path of length 2k stages is
OPF: P( y1, y 2 ,2k ) = optimal midpoint on the min path of length 2k stages connecting y1 and
y2 .
BC: S( y1, y 2 , 1) = c y1 , y 2 , y1 = 0, 1, 2 ; y 2 = 0, 1, 2 ,
22
S(1,0,1) + S(0,2,1) 2 + 1
, , ) + S(1,2,1) = min 3 + 6 = 3,
S(1,2,2) = min S(111 P(1,2,2) = 0,
S(1,2,1) + S(2,2,1) 6 + 4
S(2,0,1) + S(0,0,1) 5 + 5
, ) + S(1,0,1) = min 2 + 2 = 4,
S(2,0,2) = min S(2,11 P(2,0,2) = 1,
S(2,2,1) + S(2,0,1) 4 + 5
S(2,0,1) + S(0,11, ) 5 + 4
S(2,1,2) = min S(2,11 , , ) = min 2 + 3 = 5,
, ) + S(111 P(2,1,2) = 1,
S(2,2,1) + S(2,11 , )
4 + 2
S(2,0,1) + S(0,2,1) 5 + 1
, ) + S(1,2,1) = min 2 + 6 = 6,
S(2,2,2) = min S(2,11 P(2,2,2) = 0,
S(2,2,1) + S(2,2,1) 4 + 4
S(0,0,2) + S(0,0,2) 6 + 6
S(0,0,4) = min S(0,1,2) + S(1,0,2) = min 3 + 5 = 8, P(0,0,4) = 1,
S(0,2,2) + S(2,0,2) 5 + 4
S(0,0,2) + S(0,1,2) 6 + 3
S(0,1,4) = min S(0,1,2) + S(11
, ,2) = min 3 + 6 = 9, P(0,1,4) = 0 or 1,
S(0,2,2) + S(2,1,2) 5 + 5
S(0,0,2) + S(0,2,2) 6 + 5
S(0,2,4) = min S(0,1,2) + S(1,2,2) = min 3 + 3 = 6, P(0,2,4) = 1,
S(0,2,2) + S(2,2,2) 5 + 6
S(1,0,2) + S(0,0,2) 5 + 6
, ,2) + S(1,0,2) = min 6 + 5 = 7,
S(1,0,4) = min S(11 P(1,0,4) = 2,
S(1,2,2) + S(2,0,2) 3 + 4
S(1,0,2) + S(0,1,2) 5 + 3
S(1,1,4) = min S(11 , ,2) = min 6 + 6 = 8,
, ,2) + S(11 P(1,1,4) = 0 or 2,
S(1,2,2) + S(2,1,2) 3 + 5
S(1,0,2) + S(0,2,2) 5 + 5
, ,2) + S(1,2,2) = min 6 + 3 = 9,
S(1,2,4) = min S(11 P(1,2,4) = 1 or 2,
S(1,2,2) + S(2,2,2) 3 + 6
23
S(2,0,2) + S(0,0,2) 4 + 6
S(2,0,4) = min S(2,1,2) + S(1,0,2) = min 5 + 5 = 10, P(2,0,4) = 0, 1 or 2,
S(2,2,2) + S(2,0,2) 6 + 4
S(2,0,2) + S(0,1,2) 4 + 3
, ,2) = min 5 + 6 = 7,
S(2,1,4) = min S(2,1,2) + S(11 P(2,1,4) = 0,
S(2,2,2) + S(2,1,2) 6 + 5
S(2,0,2) + S(0,2,2) 4 + 5
S(2,2,4) = min S(2,1,2) + S(1,2,2) = min 5 + 3 = 8, P(2,2,4) = 1,
S(2,2,2) + S(2,2,2) 6 + 6
S(0,0,4) + S(0,1,4) 8 + 9
S(0,1,8) = min S(0,1,4) + S(11
, ,4) = min 9 + 8 = 13, P(0,1,8) = 2,
S(0,2,4) + S(2,1,4) 6 + 7
S(0,0,4) + S(0,2,4) 8 + 6
S(0,2,8) = min S(0,1,4) + S(1,2,4) = min 9 + 9 = 14, P(0,2,8) = 0 or 2,
S(0,2,4) + S(2,2,4) 6 + 8
S(1,0,4) + S(0,1,4) 7 + 9
, ,4) + S(11
S(1,1,8) = min S(11 , ,4) = min 8 + 8 = 16, P(1,1,8) = 0, 1 or 2,
S(1,2,4) + S(2,1,4) 9 + 7
S(1,0,4) + S(0,2,4) 7 + 6
, ,4) + S(1,2,4) = min 8 + 9 = 13,
S(1,2,8) = min S(11 P(1,2,8) = 0,
S(1,2,4) + S(2,2,4) 9 + 8
24
S(2,0,4) + S(0,0,4) 10 + 8
S(2,0,8) = min S(2,1,4) + S(1,0,4) = min 7 + 7 = 14, P(2,0,8) = 1,
S(2,2,4) + S(2,0,4) 8 + 10
S(2,0,4) + S(0,1,4) 10 + 9
, ,4) = min 7 + 8 = 15,
S(2,1,8) = min S(2,1,4) + S(11 P(2,1,8) = 1 or 2,
S(2,2,4) + S(2,1,4) 8+7
S(2,0,4) + S(0,2,4) 10 + 6
S(2,2,8) = min S(2,1,4) + S(1,2,4) = min 7 + 9 = 16, P(2,2,8) = 0, 1 or 2,
S(2,2,4) + S(2,2,4) 8+8
Shortest Path:
Stage (x) 0 (2) 1 (1) 2 (2) 3 (2) 4 (1) 5 (2) 6 (2) 7 (1) 8
+2 +1
State (y) 1 0 2 1 0 2 1 0 2
25