You are on page 1of 25

DYNAMIC PROGRAMMING

ELEMENTARY SHORTEST PATH PROBLEMS

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.

Figure 1.1 (arc lengths are different)

Computational Effort for Total (Complete) Enumeration

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

If we know two numbers, SC and SD, we could make the decision at A.

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.

Continue with Dynamic Programming

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

Optimal Value Function ( Sx ) :

Rule that assigns the optimal value to each subproblem.

Argument(s) of OVF ( the x in Sx ) :

Symbol(s) that designates a particular subproblem.

Optimal Policy Function ( Px ) :

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

Length of Shortest Path: SA = 19,

Shortest Path: A - C - F - J - M - O - B.

4
Computational Efficiency

Dynamic Programming (DP)

At nodes H, L, O, K, N, P, one add, no compares.

At nodes A, C, E, D, F, I, G, J, M, two adds, one compare.

Computational effort (DP) = 1 6 + 2 9 = 24 adds and 1 9 = 9 compares.

Total Enumeration (TE)

Computational effort (TE) = 100 adds and 19 compares.

Computational effort (DP) < Computational effort (TE)

Backward Dynamic Programming Formulation for Example # 1

OVF: S(x , y ) = value of min path from (x , y ) to (6,0 ) .

ARG: (x , y ) = node.

OPF: P(x , y ) = up or down (decision at node (x , y )) .

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),

where a u (x , y ) = length of arc going up from node (x , y ) ,


and a d (x , y ) = length of arc going down from node (x , y ) .

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.

Figure 1.2 (with rebates at the end nodes)

Backward Dynamic Programming Formulation for Example # 2

OVF: = value of min path from to line B.

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)

Length of Shortest Path: S(0,0) = 5,

Shortest Path: (0,0) - (1,1) - (2,0) - (3,-1) - (4,-2).

7
COMPUTATIONAL EFFICIENCY

Computational Efficiency for Problem Type 1

C
E

A B

G
D

Shortest path from to .


Assume N stages, N even.

Dynamic Programming

N nodes with 1 add and 0 compares.

8
2
N
nodes with 2 adds and 1 compare.
2
N2 N2
Computational effort (DP) = + N adds and compares.
2 4

Total Enumeration

A path includes N ups and downs.


Half are ups and half are downs.
N
Number of paths = N .

2
N 1 adds / path.

N
Total adds = (N 1) N ,

2
N
Total compares N .

2

N = 20
Adds Compares
DP 220 100
TE 3 106 184,000

Computational Efficiency for Problem Type 2

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

A path is a string of N ups and downs. For example,

Position 1 2 3 ... N-2 N-1 N


U or D U D D ... U D U

So, there are a total of 2 N paths.

Effort to compute length (cost) of a path = (N 1) adds.


Effort to compare 2 N paths = 2 N 1 compares.

Computational effort (TE) = (N 1) 2 N adds and 2 N 1 compares.

N = 20
Adds Compares
DP 420 210
TE 2010 6 10 6

Forward Dynamic Programming for Example # 1

Now, let us consider a forward DP formulation.

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.

Forward Dynamic Programming Formulation

OVF: S(x , y ) = value of min path from (0,0 ) to (x , y ) .

ARG: (x , y ) = node.

OPF: P(x , y ) = up or down (decision going int o node (x , y )) .

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 ) .

SHORTEST PATH PROBLEM WITH TURN PENALTIES (Example # 3)

Now, every time we have to turn, we have a turn penalty.

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)

ARG: (x , y ) = current node, and


z = move from (x , y ) is up ( z = 0 ) or down ( z = 1 ).

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

S(x + 1, y 1, 0 ) + 3 (turn at next node )


S(x , y, 1) = a d (x , y ) + min
(continue down )
,
S(x + 1, y 1, 1)

x = 5, 4, 3 ; y = (6 x ) , (6 x ) 2 , . . . , (6 x ) ,
x = 2, 1, 0 ; y = x , x 2, . . ., x .

BC: S(6,0,0 ) = S(6,0,1) = 0 .

S(0,0,0 )
ANS: min .
S(0,0,1)

Solution of Shortest Path Example with Turn Penalties

Solution is provided in the figure below.


Note that S(x , y ) provides the values of min path from (x , y ) to (6,0 ) going up () and going
down ().


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+

Length of Shortest Path: S(0,0,1) = 17,

Shortest Path: (0,0) - (1,-1) - (2,-2) - (3,-3) - (4,-2) - (5,-1) - (6,0).

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.

Computational Effort of Dynamic Programming

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

Exercise: solve this problem with forward DP formulation.

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?

(a) Where are you? Answer: (x , y ) .


(b) How many turns so far? Answer: w.
(c) How did you get to (x , y ) ? Answer: up ( z = 0 ) or down ( z = 1 ).

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.

ARG: (x , y ) = current node,


z = arrival at (x , y ) going up ( z = 0 ) or down ( z = 1 ), and
w = number of prior direction changes.

OPF: P(x , y, z, w ) = up or down.

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 .

BC: S(N,0,0, w ) = S(N,0,1, w ) = 0 , w = 0 , 1, . . ., N .

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.

Forward Dynamic Programming Formulation for Problem # 1.11

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

Shortest path from A to C has cost = 6 (3 turns)


Second shortest path from A to C has cost = 8 (1 turn)

Shortest path from A to C is used to find the shortest path from A to B:

Shortest path from A to B has cost = 6 + 4 = 10 (4 turns) (Incorrect!)

This DP formulation does not work!


The length of true shortest path from A to B is just 8, based on second shortest path from A to C.
The principle of optimality does not hold. (Information stored in each node is not enough.)

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:

(a) Where are you? Answer: (x , y ) .


(b) Value of shortest path and longest arc so far? Answer: S(x , y, ) ,
where = value of longest arc.
16
IDEA: Have you spent your coupon for a free arc?

Backward Dynamic Programming Formulation for Problem # 1.14 (Correct!)

OVF: S(x , y, z ) = value of min path from (x , y ) to (N,0 ) starting with z { 0, 1 } coupons.

ARG: (x , y ) = current node,


z = number of coupons available at (x , y ) .

OPF: P(x , y, z ) = up or down, and if z = 1 , with or w/o spending coupon.

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)

a u (x , y ) + S(x + 1, y + 1, 1) up w / o spending coupon


S(x + 1, y + 1, 0 )
up spending coupon
S(x , y, 1) = min ,
a d (x , y ) + S(x + 1, y 1, 1) down w / o spending coupon
S(x + 1, y 1, 0 ) down spending coupon

; 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,0 ) = S(N,0,1) = 0 , w = 1 , 2, . . ., N 1 .

ANS: S(0,0,1) .

Forward Dynamic Programming Formulation for Problem # 1.14 (Incorrect!)

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 ) .

ARG: (x , y ) = current node,

OPF: P(x , y ) = up or down, with or without swapping longest arc.

S(x-1, y+1) ad (x-1, y+1)

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

BC: S(0,0 ) = L(0,0) = 0 .

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 1 au = 800


10
D
S = 821
800 L = 800
1 800
10

800 800

True Optimal solution: S(6,0 ) = 23 , L(6,0 ) = 200 .

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

(a) Where are you? Answer: (x , y ) .


(b) How did you get to (x , y ) ? Answer: up ( z = 0 ) or down ( z = 1 ).
(c) Is a turn allowed? Answer: allowed ( w = 0 ) or not allowed ( w = 1 ).

Backward Dynamic Programming Formulation for Problem # 1.19

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 ).

ARG: (x , y ) = current node,


z = arrival at (x , y ) going up ( z = 0 ) or down ( z = 1 ), and
w = a turn is allowed ( w = 0 ) or not allowed ( w = 1 ).

OPF: P(x , y, z, w ) = up or down.

RR: S(x , y, 0, 1) = a u (x , y ) + S(x + 1, y + 1, 0, 0 ) ,

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)

(x-1, y-1) (x+1, y-1) ad (x+1, y-1)

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

RR: S( y1, y 2 , 2k ) = min { S( y1, y, k ) + S( y, y 2 , k ) }, y1 = 0, 1, 2 ; y 2 = 0, 1, 2 ; k = 1, ..., 4 .


y = 0,1,2

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 ,

where c00 = 5 , c01 = 4 , c02 = 1 , c10 = 2 , c11 = 3 , c12 = 6 , c 20 = 5 , c 21 = 2 , c 22 = 4 .

ANS: min { t 0 ( y1) + S( y1, y2 , 8) + t8 ( y2 ) },


y1 = 0,1, 2,
y 2 = 0,1, 2

where t 0 (0) = 3 , t 0 (1) = 2 , t 0 (2) = 1 , t 8 (0) = 5 , t 8 (1) = 4 , t 8 (2) = 1 .

SOLUTION OF DOUBLING-UP EXAMPLE

S(0,0,1) = 5, S(0,1,1) = 4, S(0,2,1) = 1,


S(1,0,1) = 2, S(1,1,1) = 3, S(1,2,1) = 6,
S(2,0,1) = 5, S(2,1,1) = 2, S(2,2,1) = 4,
S( 0,0,1) + S( 0,0,1) 5 + 5

, ) + S(1,0,1) = min 4 + 2 = 6,
S(0,0,2) = min S(0,11 P(0,0,2) = 1 or 2,
S(0,2,1) + S(2,0,1) 1 + 5

S(0,0,1) + S(0,11, ) 5 + 4

, ) + S(111
S(0,1,2) = min S(0,11 , , ) = min 4 + 3 = 3, P(0,1,2) = 2,
S(0,2,1) + S(2,11 , )
1 + 2
S( 0,0,1) + S(0,2,1) 5 + 1

, ) + S(1,2,1) = min 4 + 6 = 5,
S(0,2,2) = min S(0,11 P(0,2,2) = 2,
S(0,2,1) + S(2,2,1) 1 + 4

S(1,0,1) + S(0,0,1) 2 + 5

, , ) + S(1,0,1) = min 3 + 2 = 5,
S(1,0,2) = min S(111 P(1,0,2) = 1,
6 + 5
S(1,2,1) + S(2,0,1)
S(1,0,1) + S(0,11, ) 2 + 4

, , ) + S(111
S(1,1,2) = min S(111 , , ) = min 3 + 3 = 6, P(1,1,2) = 0 or 1,
S(1,2,1) + S(2,11 , ) 6 + 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,0,4) 8+8



S(0,0,8) = min S(0,1,4) + S(1,0,4) = min 9 + 7 = 16, P(0,0,8) = 0, 1 or 2,
S(0,2,4) + S(2,0,4) 6 + 10

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,0,4) 7+8



, ,4) + S(1,0,4) = min 8 + 7 = 15,
S(1,0,8) = min S(11 P(1,0,8) = 0 or 1,
S(1,2,4) + S(2,0,4) 9 + 10

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

t 0 (0) + S(0,0,8) + t 8 (0) 3 + 16 + 5


t (0) + S(0,1,8) + t (1) 3 + 13 + 4
0 8
t 0 (0) + S(0,2,8) + t 8 (2) 3 + 14 + 1

t 0 (1) + S(1,0,8) + t 8 (0) 2 + 15 + 5
min t 0 (1) + S(11 , ,8) + t 8 (1) = min 2 + 16 + 4 = 2 + 13 + 1 = 16 , y1* = 1, y2* = 2,
t (1) + S(1,2,8) + t (2) 2 + 13 + 1
0 8
t 0 (2) + S(2,0,8) + t 8 (0) 1 + 14 + 5
t (2) + S(2,1,8) + t (1) 1 + 15 + 4
0 8
t 0 (2) + S(2,2,8) + t 8 (2) 1 + 16 + 1

Length of Shortest Path: 16,

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

You might also like