chapter 9 shortest paths and discrete dynamic programming

85
Chapter 9 Shortest Paths and Discrete Dynamic Programming

Upload: niel

Post on 23-Feb-2016

66 views

Category:

Documents


7 download

DESCRIPTION

Chapter 9 Shortest Paths and Discrete Dynamic Programming. Example 9.1 Littleville. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Chapter 9

Shortest Paths andDiscrete Dynamic Programming

Page 2: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.1 Littleville

Suppose that you are the city traffic engineer for the town of Littleville. Figure 9.1(a) depicts the arrangement of one- and two-way streets in a proposed improvement plan for Littleville’s downtown, including the estimated average time in seconds that a car will require to transit each block.

From survey and other data we can estimate how many driver trips originate at various origin points in the town, and the destination for which each trip is bound. But such survey data cannot indicate what streets will be selected by motorists to move from origin to destination in a street network that does not yet exist.

Page 3: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.1 Littleville

One of the tasks of a traffic engineer is to project the route that drivers will elect, so that city leaders can evaluate whether flows will concentrate where they hope. A good starting point is to assume that drivers will do the most rational thing-follow the shortest time path from their origin to their destination. We need to compute all such shortest paths.

Page 4: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.1 Littleville

20 18

12

18

13

28

38

32

30

1828

36

25

49

21

40

Page 5: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.1 Littleville

1

2

3

4 10

9

7

6

5 820

1218

13

25

2128

49

1828

3640

18

38

32

30

Page 6: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.1 Shortest Path Models

• Urban traffic, Satellite communications, or surface of a microchip.

• Mathematical graphs model travel, flow, and adjacency patterns in a network. [9.1]

• The nodes or vertices of a graph represent entities, intersections, and transfer points of the network. [9.2]

• The arcs of a graph model available directed (one-way) links between nodes. Edges represent undirected (two-way) links. [9.3]

Page 7: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.1 Shortest Path Models

• A path is a sequence of arcs or edges connecting two specified nodes in a graph. Each arc or edge must have exactly one node in common with its predecessors in the sequence, any arcs must passed in the forward direction, and no node may be visited more than once. [9.4]

Page 8: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Shortest Path Problems

• Shortest path problems seek minimum total length paths between specified pairs of nodes in a graph. [9.5]

Page 9: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.1 Littleville

1

2

3

4 10

9

7

6

5 820

1218

13

25

2128

49

1828

3640

18

38

• Name: Littleville• Graph: arcs and edges• Costs: nonnegative• Output: shortest paths• Pairs: all nodes to all

others

Page 10: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.2 Texas Transfer

Texas Transfer, a major trucker in the southwest, ships goods from its hub warehouse in Ft. Worth to all the cities shown. Trucks leave the hub and proceed directly to their destination city, with no intermediate dropoffs or pickups.

Texas Transfer drivers are allowed to choose their own route from Ft. Worth to their destination. However, management’s proposal in current labor negotiations calls for drivers to be paid on the basis of shortest standard mileage to the location. That is, they will be paid according to the length of the shortest path from Ft. Worth to their destination city in the network of Figure 9.3. To see the impact of this proposal, we need to compute such shortest path distances for all cities.

Page 11: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.2 Texas Transfer

Notice that the character of this shortest path task differs significantly from the Littleville example. Here we need only optimal path lengths, not the paths themselves. Also, we need shortest path lengths for only one origin or source—the Ft. Worth hub.

Page 12: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.2 Texas Transfer1

23

4

10

9

7

6

5

8

35912

2

345

443 92

199

246

213

195

79

215

153

180167

415

El Paso

Lubbock

Amarillo

Ft. Worth

Abilene

San Angelo

San Antonio

Corpus Christi

HoustonAustin

• Name: Texas Transfer• Graph: edges only• Costs: nonnegative• Output: shortest path

lengths• Pairs: one node to all

others

Page 13: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.3 Two Ring Circus

The Two Ring Circus is nearing the end of its season and planning a return to winter headquarters near Tallahassee, Florida. Present commitments will end in Lincoln, Nebraska, but there are still some opportunities for bookings in cities along the route home.

Figure 9.4(a) shows the travel routes available and the estimated costs (in $1000) of moving the circus over those routes. It also designates the cities where bookings have been offered and the anticipated net receipts (in thousands of dollars).

We want to compute the optimal path home for Two Ring. Notice that the cost of a path is the difference of travel costs and net receipts from performances along the way. But receipts occur at nodes of the network, not on edges as shortest path models require.

Page 14: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.3 Two Ring Circus

Chattanooga(3.0)

1

3

2

9

87

5

4

6

3.6

2.8

3.8

4.5

2.1

2.6

5.5

4.5

4.5

2.4

2.25.1Wichita

(3.2)

Lincoln(0.0)

Davenport (1.6)

Little Rock(3.0)

Jackson(1.0)

Tallahassee(0.0)

Montgomery (1.5)

Springfield (1.5)

4.6

1.74.6

Page 15: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Undirected and Directed Graph (Digraph)

• A shortest path problems including edges (i, j) of cost ci,j can be converted to an equivalent one on a digraph by replacing each edge with a pair of opposed arcs as [9.6]

i jci,j i j

ci,j

cj,i

Page 16: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.3 Two Ring Circus

Chattanooga (3.0)

1

3

2

9

87

5

4

6

3.6

2.8

3.8

4.5

2.1

2.6

5.5

4.5

4.5

2.4

2.25.1Wichita

(3.2)

Lincoln(0.0)

Davenport (1.6)

Little Rock(3.0)

Jackson(1.0)

Tallahassee(0.0)

Montgomery (1.5)

Springfield (1.5)

4.6

1.7

4.6

3.6

1.74.62.8

5.14.5 4.5

5.5

4.6

2.1

3.8 2.4

2.6

2.24.5

Page 17: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Two Ring Example Model

Chattanooga

1

3

2

9

87

5

4

6

3.6

-0.4

0.8

1.5

1.1

1.1

2.5

3.5

3.0

0.9

2.2

3.6Wichita

Lincoln Davenport

Little Rock

Jackson

Tallahassee

Montgomery

Springfield

4.6

0.2

1.6

2.0

0.13.12.8

2.31.3 1.5

4.0

1.6

-0.9

2.8 -0.6

1.60.74.5

• Name: Two Ring Circus• Graph: directed• Costs: arbitrary• Output: shortest path

Pairs: one source to one destination

Page 18: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.2 Dynamic Programming Approach to Shortest Paths

• Dynamic programming methods exploit the fact that it is sometimes easiest to solve one optimization problem by taking on an entire family.

• Shortest path algorithms exploit relationships among optimal paths (and path lengths) for different pairs on the nodes. [9.7]

Page 19: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Functional Notationfor One Source to All Other Nodes

[k] length of a shortest path from the source node to the node k (= + if no path exists)

xi,j[k] 1 if arc/edge (i, j) is part of the optimal path from the source node to node k 0 otherwise

Page 20: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Functional Notationfor All Nodes to All Other Nodes

[k, l] length of a shortest path from node k to the node l (= + if no path exists)

xi,j[k, l] 1 if arc/edge (i,j) is part of the optimal path from the node k to node l 0 otherwise

Page 21: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Optimal Paths and Subpaths

If the highlighted path (through Austin and San Antonio) is the shortest from Ft. Worth to Corpus Christi, the subpath from Ft. Worth to San Antonio must also be the shortest. (No negative dicycle)

1

23

4

10

9

7

6

5

8

35912

2

345

443 92

199

246

213

195

79

215

153

180167

415

El Paso

Lubbock

Amarillo

Ft. Worth

Abilene

San Angelo

San Antonio

Corpus Christi

HoustonAustin

Page 22: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Negative Dicycles Exception

• A dicycle is a path that begins and ends at the same node, and a negative dicycle is a dicycle of negative total length. [9.8]

• Shortest path models with negative dicycles are much less tractable than other cases because dynamic programming methods usually do not apply. [9.9]

1 2

3 4

2

10 -253

12

[1]=0 [2]=-3

[3]=5 [4]=17

Page 23: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Principle of Optimality

• In a graph with no negative dicycles, optimal paths must have optimal subpaths. [9.10]

• Functional equations of a dynamic program encode the recursive connections among optimal solution values that are implied by a principle of optimality. [9.11]

Page 24: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Functional Equationsfor One Node to All Others

The functional equations for shortest path problems from a single source s in a graph with no negative dicycles are [9.12]

[s] = 0

[k] = min {[i] + ci,k : (i, k) exists} all k≠s

Page 25: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Sufficiency of Functional Equationsin One to All Case

Node value [k] in a graph with no negative dicycles are lengths of shortest paths from a given source s if and only if they satisfy functional equations in [9.12]. [9.13]

Page 26: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Sufficiency of Functional Equationsin One to All Case

[4]=[5]+ c5,4

[5]=[3]+ c3,5

[4]+[5]=[5]+ c5,4+ [3]+ c3,5

[4]=cs,5+ c5,4

[4] cs,1+ c1,2+c2,4

1

23

4

10

9

7

6

5

8

35912

2

345

443 92

199246

213

195

79

215

153

180167

415El Paso

[4]=623

Lubbock[2]=347

Amarillo[1]=359 Ft. Worth

[3]=0

Abilene[5]=180

San Angelo[6]=272

San Antonio[7]=274

Corpus Christi [10]=427

Houston[9]=246

Austin[8]=195

Page 27: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Functional Equationsfor All Nodes to All Others

• The functional equations for shortest path problems from a single source s in a graph with no negative dicycles are [9.14]

[k,k] = 0 all k[k,l] = min {ck,l, {[k,i] + [i,l] : i ≠ k, l} } all k≠l

• Node pair values [k,l] in a graph with no negative dicycles are lengths of shortest paths from k to l if and only if they satisfy functional equations in [9.14]. [9.15]

Page 28: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Solving Shortest Path Problems byLinear Programming

• Although shortest path problems over graphs with no negative dicycles can be solved by linear programming, dynamic programming methods based on the principle of optimality are far more efficient. [9.16]

Page 29: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.3 Shortest Paths from One Node to All Others:

Bellman-Ford• Solving the functional equations can be carried out by

calculating the single value [k] (one-pass evaluation).• Dicycles introduce circular dependencies in functional

equations that preclude their solution by one-pass evaluation, even if the length of all dicycles is non-negative. [9.17]

Page 30: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Repeated Evaluation Algorithm: Bellman-Ford

• (t)[k] value of [k] obtained on the tth iteration• d[k]node preceding k in the best known path from s to

k.

Page 31: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Algorithm 9A: One to All (no Negative Dicycles) Bellman-Ford

Step 0: Initiation. With s the source node, initialize optimal path lengths

(0)[k] = 0 if k=s = + otherwise

and set iteration counter t 1Step 1: Evaluation. For each k evaluate

(t)[k] min {(t-1) [i] + ci,k : (i, k) exists} if (t)[k] < (t-1)[k], also set d[k] the number of a neighboring node i achieving the minimum (t)[k].

Page 32: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Algorithm 9A: One to All (no Negative Dicycles) Bellman-Ford

Step 2: Stopping. Terminate if (t)[k] < (t-1)[k] for every k, or if t= the number of nodes in the graph. Value (t)[k] then equal the required shortest path lengths unless some (t)[k] changed at the last t, in which case the graph contains a negative dicycle. Step 3: Advance. If some [k] changed and t < the number of nodes, increment t t+1 and return to Step 1.

Page 33: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Two Ring Example Model

Chattanooga

1

3

2

9

87

5

4

6

3.6

-0.4

0.8

1.5

1.1

1.1

2.5

3.5

3.0

0.9

2.2

3.6Wichita

Lincoln Davenport

Little Rock

Jackson

Tallahassee

Montgomery

Springfield

4.6

0.2

1.6

2.0

0.13.12.8

2.31.3 1.5

4.0

1.6

-0.9

2.8 -0.6

1.60.74.5

Page 34: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Bellman-Ford Solution of the Two Ring Circus Example

t (t)[1] (t)[2] (t)[3] (t)[4] (t)[5] (t)[6] (t)[7] (t)[8] (t)[9] 0 0 + + + + + + + +1 3.6 2.8 4.62 3.7 4.13 5.7 3.24 4.8 6.75 5.5t d[1] d[2] d[3] d[4] d[5] d[6] d[7] d[8] d[9] 1 1 1 12 2 33 5 54 7 75 8

Page 35: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Bellman-Ford Algorithm

• At the completion of Bellman-Ford Algorithm, a shortest path from source s to any other node k can be recovered by starting at k, backtracking to neighboring node d[k], and continuing with an optimal path from s to the neighbor until source s is encountered. [9.18]

• If Bellman-Ford Algorithm encounters negative dicycles, it will demonstrate their presence by continuing to change (t)[k] on iteration t = the number of nodes. Any node k with such changing (t)[k] belongs to a negative dicycle. [9.19]

Page 36: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Two Ring Example Model

Chattanooga

1

3

2

9

87

5

4

6

3.6

-0.4

0.8

1.5

1.1

1.1

2.5

3.5

3.0

0.9

2.2

3.6Wichita

Lincoln Davenport

Little Rock

Jackson

Tallahassee

Montgomery

Springfield

4.6

0.2

1.6

2.0 0.1

3.1

2.8

2.31.3 1.5

4.0

1.6

-0.9

2.8 -0.6

1.60.7

4.5

Page 37: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.4 Shortest Paths from All Nodes to All Others:

Floyd-Warshall• (t)[k, l] length of a shortest path for k to l using only

intermediate nodes numbered less than or equal to t. • d[k, l] node just before l on the current path from k to

l.

Page 38: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Algorithm 9B: All to All (no Negative Dicycles) Floyd-Warshall

Step 0: Initiation. All nodes should have consecutive positive numbers starting with 1. For all arcs and edges (k, l) in the graph, initialize

(0)[k, l] ck,l d[k, l] k For k, l pairs with no arc/edge (k, l), assign

(0)[k, l] = 0 if k=l =+ otherwisealso set iteration counter t 1

Step 1: Evaluation. For all k, l ≠ t update(t)[k, l] min {(t-1) [k, l], (t-1) [k, t] + (t-1) [t, l] } if (t)[k, l] < (t-1)[k, l], also set d[k, l] d[t, l]

Page 39: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Algorithm 9B: All to All (no Negative Dicycles) Floyd-Warshall

Step 2: Stopping. Terminate if t = the number of nodes in the graph, or if (t)[k, k] < 0 for any node k. Value (t)[k, l] then equal the required shortest path lengths unless some (t)[k, k] is negative, in which case the graph contains a negative dicycle through k. Step 3: Advance. If t < the number of nodes and (t)

[k,k]0, increment t t+1 and return to Step 1.

Page 40: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.1 Littleville

1

2

3

4 10

9

7

6

5 820

1218

13

25

2128

49

1828

3640

18

38

32

30

Page 41: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Floyd-Warshall Solution of the Littleville Example

Nu l=1 l=2 l=3 l=4 l=5 l=6 l=7 l=8 l=9 l=10k=1 0 12 ¥ ¥ ¥ ¥ ¥ ¥ ¥ ¥k=2 ¥ 0 18 ¥ ¥ 32 ¥ ¥ ¥ ¥k=3 ¥ ¥ 0 13 ¥ ¥ 30 ¥ ¥ ¥k=4 ¥ ¥ ¥ 0 ¥ ¥ ¥ ¥ ¥ 38k=5 20 ¥ ¥ ¥ 0 18 ¥ ¥ ¥ ¥k=6 ¥ 32 ¥ ¥ 18 0 ¥ ¥ 25 ¥k=7 ¥ ¥ 30 ¥ ¥ 28 0 ¥ 21 49k=8 ¥ ¥ ¥ ¥ 18 ¥ ¥ 0 36 ¥k=9 ¥ ¥ ¥ ¥ ¥ 25 21 36 0 40k=10 ¥ ¥ ¥ ¥ ¥ ¥ 49 28 40 0dk=1 1k=2 2 2k=3 3 3k=4 4k=5 5 5k=6 6 6 6k=7 7 7 7 7k=8 8 8k=9 9 9 9 9k=10 10 10 10

Page 42: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Floyd-Warshall Solution of the Littleville Example

Nu l=1 l=2 l=3 l=4 l=5 l=6 l=7 l=8 l=9 l=10k=1 0 12 ¥ ¥ ¥ ¥ ¥ ¥ ¥ ¥k=2 ¥ 0 18 ¥ ¥ 32 ¥ ¥ ¥ ¥k=3 ¥ ¥ 0 13 ¥ ¥ 30 ¥ ¥ ¥k=4 ¥ ¥ ¥ 0 ¥ ¥ ¥ ¥ ¥ 38k=5 20 32 ¥ ¥ 0 18 ¥ ¥ ¥ ¥k=6 ¥ 32 ¥ ¥ 18 0 ¥ ¥ 25 ¥k=7 ¥ ¥ 30 ¥ ¥ 28 0 ¥ 21 49k=8 ¥ ¥ ¥ ¥ 18 ¥ ¥ 0 36 ¥k=9 ¥ ¥ ¥ ¥ ¥ 25 21 36 0 40k=10 ¥ ¥ ¥ ¥ ¥ ¥ 49 28 40 0d  k=1   1                k=2   2 2k=3   3 3k=4   4k=5 5 1 5k=6   6 6 6k=7   7 7 7 7k=8   8 8k=9   9 9 9 9k=10   10 10 10

Page 43: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Floyd-Warshall Solution of the Littleville Example

Nu l=1 l=2 l=3 l=4 l=5 l=6 l=7 l=8 l=9 l=10k=1 0 12 30 ¥ ¥ 44 ¥ ¥ ¥ ¥k=2 ¥ 0 18 ¥ ¥ 32 ¥ ¥ ¥ ¥k=3 ¥ ¥ 0 13 ¥ ¥ 30 ¥ ¥ ¥k=4 ¥ ¥ ¥ 0 ¥ ¥ ¥ ¥ ¥ 38k=5 20 32 50 ¥ 0 18 ¥ ¥ ¥ ¥k=6 ¥ 32 50 ¥ 18 0 ¥ ¥ 25 ¥k=7 ¥ ¥ 30 ¥ ¥ 28 0 ¥ 21 49k=8 ¥ ¥ ¥ ¥ 18 ¥ ¥ 0 36 ¥k=9 ¥ ¥ ¥ ¥ ¥ 25 21 36 0 40k=10 ¥ ¥ ¥ ¥ ¥ ¥ 49 28 40 0d  k=1 1 2 2k=2     2     2        k=3   3 3k=4   4k=5 5 1 2 5k=6 6 2 6 6k=7   7 7 7 7k=8   8 8k=9   9 9 9 9k=10   10 10 10

Page 44: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Floyd-Warshall Solution of the Littleville Example

Nu l=1 l=2 l=3 l=4 l=5 l=6 l=7 l=8 l=9 l=10k=1 0 12 30 43 ¥ 44 60 ¥ ¥ ¥k=2 ¥ 0 18 31 ¥ 32 48 ¥ ¥ ¥k=3 ¥ ¥ 0 13 ¥ ¥ 30 ¥ ¥ ¥k=4 ¥ ¥ ¥ 0 ¥ ¥ ¥ ¥ ¥ 38k=5 20 32 50 63 0 18 80 ¥ ¥ ¥k=6 ¥ 32 50 63 18 0 80 ¥ 25 ¥k=7 ¥ ¥ 30 43 ¥ 28 0 ¥ 21 49k=8 ¥ ¥ ¥ ¥ 18 ¥ ¥ 0 36 ¥k=9 ¥ ¥ ¥ ¥ ¥ 25 21 36 0 40k=10 ¥ ¥ ¥ ¥ ¥ ¥ 49 28 40 0d  k=1 1 2 3 2 3k=2 2 3 2 3k=3       3     3      k=4   4k=5 5 1 2 3 5 3k=6 6 2 3 6 3 6k=7 7 3 7 7 7k=8   8 8k=9   9 9 9 9k=10   10 10 10

Page 45: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Floyd-Warshall Solution of the Littleville Example

Nu l=1 l=2 l=3 l=4 l=5 l=6 l=7 l=8 l=9 l=10k=1 0 12 30 43 62 44 60 105 69 81k=2 70 0 18 31 50 32 48 93 57 69k=3 96 90 0 13 76 58 30 79 51 51k=4 104 116 117 0 84 102 87 66 78 38k=5 20 32 50 63 0 18 80 79 43 83k=6 38 32 50 63 18 0 80 61 25 65k=7 66 60 30 43 46 28 0 57 21 49k=8 38 50 68 81 18 36 96 0 36 76k=9 63 57 51 64 43 25 21 36 0 40k=10 66 78 79 92 46 64 49 28 40 0d  k=1 1 2 3 6 2 3 9 6 4k=2 6 2 3 6 2 3 9 6 4k=3 7 7 3 7 7 3 10 7 4k=4 10 10 10 10 10 10 10 10 4k=5 5 1 2 3 5 3 9 6 9k=6 5 6 2 3 6 3 9 6 9k=7 6 6 7 3 6 7 9 7 7k=8 5 5 5 5 8 5 5 8 9k=9 6 6 7 7 6 9 9 9 9k=10 8 8 7 7 8 8 10 10 10  

Page 46: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Floyd-Warshall Algorithm

• At the completion of Floyd-Warshall Algorithm, a shortest path from any node k to any other node l can be recovered by starting at l, backtracking to neighboring node d[k, l], and continuing with an optimal path from k to the neighbor until source k itself is encountered. [9.20]

• If Floyd-Warshall Algorithm is applied to a graph with negative dicycles, it will demonstrate their presence by making some (t)[k,k] < 0 and terminating. Under those circumstances, the implied negative dicycle includes node k. [9.21]

Page 47: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.5 Shortest Path from One Node to All Others with

Cost Non-negative: Dijkstra• The bellman-Ford and Floyd-Warshall algorithms can

solve any of the shortest path models without negative dicycles.

• With further assumptions, other algorithms may be more efficient.

Page 48: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Algorithm 9C: One to All (Non-Negative Costs): Dijkstra

Step 0: Initiation. With s the source node, initialize optimal path lengths

[i] = 0 if i=s = + otherwise

Then mark all nodes temporary, and choose p s as the next permanently labeled node.

Step 1: Processing. Mark node p permanent, and for every arc/edge (p, i) leading from p to a temporary node, update [i] min {[i], [p] + cp,i }

if [i] changed in value, also set d[k] p.

Page 49: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Step 2: Stopping. If no temporary nodes remain, stop; values [i] now reflect the required shortest path lengths. Step 3: Next Permanent. Choose as next permanently labeled node p a temporary node with least current value [i], that is,

[p] = min {[i] : i temporary } Then return to Step 1.

Algorithm 9C: One to All (Non-Negative Costs): Dijkstra

Page 50: Chapter  9 Shortest Paths and Discrete Dynamic Programming

• Once a node is classified permanent by Dijkstra Algorithm, its [p] and d[p] labels never change again. Nodes that are not yet permanently labeled are classified temporary. [9.22]

• Dijkstra Algorithm is the most efficient method available for computing shortest paths from one node to all others in (general) graphs and digraphs having all are/edge costs non-negative. [9.23]

• Each iteration of Dijkstra’s Algorithm selects as the new permanently labeled node p a temporary node of minimum [i]. [9.24]

Permanently and Temporary Labeled Nodes

Page 51: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Dijkstra Algorithm Solution of the Texas Transfer Example

1

23

4

10

9

7

6

5

8

359

122

345

443 92

199

246

21319

579

215

153

180167

415

El Paso

Lubbock

Amarillo

Ft. Worth

Abilene

San Angelo

San Antonio

Corpus Christi

HoustonAustin

Page 52: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Dijkstra Algorithm Solution of the Texas Transfer Example

p n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] n[10]init. ¥ ¥ 0 ¥ ¥ ¥ ¥ ¥ ¥ ¥3 359 Perm 180 195 2465 347 623 Perm 2728 274 Perm9 Perm 4616 Perm7 Perm 4272 Perm1 Perm

10 Perm4 Perm

p d[1] d[2] d[3] d[4] d[5] d[6] d[7] d[8] d[9] d[10]3 3 3 3 35 5 5 58 89 967 721

104

Page 53: Chapter  9 Shortest Paths and Discrete Dynamic Programming

• After each major iteration, Dijkstra Algorithm values [i] represent lengths of shortest paths from source s to i that use only permanently labeled nodes. [9.25]

Justification of the Dijkstra Algorithm

s

i

p

All permanent path length [p]

All permanent path length [i]

Non-negative cost

[i] [p]

Page 54: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.6 Shortest Path from One Node to All Others in

Acyclic DigraphAcyclic Digraphs• A digraph (completely directed graph) is acyclic if and

only if its nodes can be numbered so that every arc (I, j) has I < j. [9.26].

1

2

3

5

4

Acyclic

1

2

3

5

4

Has edges

1

2

3

5

4

Has dicycles

Page 55: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.6 Shortest Path from One Node to All Others in

Acyclic Digraph• Any acyclic digraph can be numbered as required in

principle [9.26] by transiting the graph in depth-first fashion, numbering nodes in decreasing sequence as soon as all their outbound arcs lead to nodes already visited. [9.27].

c

a

d

b

e

1st

2nd

3rd

5

43

2

1c

a

d

b

e

1st

2nd

3rd

4th5 4

3

21

Page 56: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Algorithm 9D: One to All (Acyclic Digraph)

Step 0: Initiation. Number nodes so that each arc (i, j) of the digraph has i<j. Then set the source s optimal path length [s] 0 Step 1: Stopping. Terminate if all [k] have now been fixed. Otherwise, let p be the lowest number of an unprocessed node.Step 2: Processing. If there are no inbound arcs at node p, set [p] +. Otherwise, compute

[p] min {[i] + ci,p : (i, p) exists } And let d[p] the number of node i achieving the minimum. Then return to Step 1.

Page 57: Chapter  9 Shortest Paths and Discrete Dynamic Programming

• Algorithm 9D is the most efficient possible for computing shortest paths from one node to all others in acyclic digraphs. [9.28]

Permanently and Temporary Labeled Nodes

Page 58: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Figure 9.11 Acyclic Shortest path Example

1 3

2

8

7

5

4

6

5

16

4-1

0

1

13

7

18

3

89

9

2

5

[1]=0[2]=5; d[2]=1 [3]=-5; d[3]=2 [4]=-3; d[4]=3 [5]=; [6]=; [7]=9; d[7]=2 [8]=22; d[8]=7 [9]=4; d[9]=4

Page 59: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.7 CPM Project Scheduling and Longest Paths

Project Management• For planning and control purposes, projects are usually

subdivided into a collection of work activities. Each activity has an estimated duration,

ak time required to accomplish activity k• Activity j is a predecessor of activity k if activity j must

be completed before activity k can begin. [9.29].

Page 60: Chapter  9 Shortest Paths and Discrete Dynamic Programming

CPM Project Networks

• CPM project networks have special start and finish nodes, plus one node for each activity. Arcs of zero length connect start to all activities without predecessors. Other arcs of length ak connect each activity node k to all activities of which it is a predecessor, or to finish if there are no such nodes. [9.30]

Page 61: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.4: We Build Construction

We Build is developing a 1-story medical office building on available land near a hospital.k Activity Duration Predecessors1 Foundation 15 --

2 Rough plumbing 5 --

3 Concrete slab 4 1, 2

4 Structural members 3 3

5 Roof 7 4

6 Rough electrical 10 4

7 Heating & AC 13 2, 4

8 Walls 18 4, 6, 7

9 Interior finish 20 5, 8

To plan materials deliveries and arrange subcontractors to do the various activities, We Build needs a schedule. In particular, they want to know the earliest time after project start that each activity can begin.

Page 62: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Figure 9.12 We Build Construction Project Network

Start 3

1

9

6

2

8

0

0

4

157

13

3

2010

5

4

5

7

Finish

5

3

3

3

18

Page 63: Chapter  9 Shortest Paths and Discrete Dynamic Programming

CPM Schedules and Longest Paths

• The earliest start time of any activity k in a project equals the length of the longest path from start to node k in the corresponding network. [9.31]

• The minimum time to complete a project equals the length of the longest or critical path from start to finish in the corresponding project network. [9.32]

Page 64: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Critical Paths

k Activity Early Start

Critical Path

1 Foundation 0 Start-12 Rough

plumbing0 Start-2

3 Concrete slab 15 Start-1-34 Structural

members19 Start-1-3-4

5 Roof 22 Start-1-3-4-56 Rough

electrical22 Start-1-3-4-6

7 Heating & AC 22 Start-1-3-4-78 Walls 35 Start-1-3-4-7-89 Interior finish 53 Start-1-3-4-7-8-9

Start 3

1

9

6

2

8

0

0

4

157

13

3

2010

5

4

5

7

Finish

5

3

3

3

18

Page 65: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Computing an Early Start Schedule for the We Build Construction Example

ak time required to accomplish activity k[k] length of the longest path from start to node kd[k] immediate predecessor of node k in a longest path

The minimum time to complete a project equals the length of the longest or critical path from start to finish in the corresponding project network. [9.32]

Page 66: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Algorithm 9E: CPM Early Start Scheduling

Step 0: Initiation. Number activity nodes so that each precedence arc (i, j) of the CPM project network has i<j. Then initialize the schedule time of the project start node as

[start] 0 Step 1: Stopping. Terminate if the early start of the project finish node has been fixed. Otherwise, let p be the lowest number of an unprocessed node.Step 2: Processing. Compute the activity p early start schedule time

[p] max {[i] + ai : i is a predecessor of p } And let d[p] the number of node i achieving the maximum. Then return to Step 1.

Page 67: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Early Start Schedules

[s]=0;

[1]=0; d[1]=s [2]=0; d[2]=s

[3]=15; d[3]=1

[4]=19; d[4]=3

[5]=22; d[5]=4 [6]=22; d[6]=4 [7]=22; d[7]=4 [8]=35; d[8]=7

[9]=53; d[9]=8

Start 3

1

9

6

2

8

0

0

4

157

13

3

2010

5

4

5

7

Finish

5

3

3

3

18

[f]=73; d[f]=9

Page 68: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Late Start Schedules and Schedule Slack

• The latest start time of any activity k in a project equals the due date less the length path from k to finish node in the corresponding CPM network. [9.33]

• Schedule slack is the difference between the earliest and latest times that an activity can begin. [9.34]

Page 69: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Late Start Schedules

[s]=0; d[s]=1 [1]=0; d[1]=3 [2]=10; d[2]=3

[3]=15; d[3]=4 [4]=19; d[4]=7

[5]=46; d[5]=9 [6]=25; d[6]=8 [7]=22; d[7]=8

[8]=35; d[8]=9

[9]=53; d[9]=f

Start 3

1

9

6

2

8

0

0

4

157

13

3

2010

5

4

5

7

Finish

5

3

3

3

18

[f]=73;

Page 70: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Schedule Slack

k Activity Early Start

Late Start

Slack

1 Foundation 0 0 02 Rough plumbing 0 10 103 Concrete slab 15 15 04 Structural members 19 19 05 Roof 22 46 246 Rough electrical 22 25 37 Heating & AC 22 22 08 Walls 35 35 09 Interior finish 53 53 0

Page 71: Chapter  9 Shortest Paths and Discrete Dynamic Programming

More on CPM Network

Difficulty of General Longest Path Problems• Longest path problems are the maximize analogs of the

shortest path problem. The difficult case of negative dicycle in shortest path problems becomes the case of positive dicycles in longest path models – dicycles of positive total length. Positive dicycles are far more common than their negative relatives.

Acyclic Character of Project Network• Every well-formed project network is acyclic. [9.35]

Page 72: Chapter  9 Shortest Paths and Discrete Dynamic Programming

9.8 Discrete Dynamic Programming Models

• In a sequential decision problem, decision can be arranged in a clear sequence. Then the decisions can be confronted one by one in sequence. That same sequence produces an acyclic digraph on which a shortest or longest path problem can be posed.

• States in dynamic programming characterize conditions of incomplete solution at which decisions should be considered. [9.36]

Page 73: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.5: Wagner-Whitin Lot Sizing

Wagner-Whitin lot sizing – production planning in an environment where there is a substantial setup cost incurred with each production run.

Period, k1 2 3 4 5 6

Requirement, rk 10 40 20 5 5 15

Setup cost, sk 50 50 50 50 50 50

Production cost, pk 1 3 3 1 1 1

Holding cost, hk 2 2 2 2 2 2

rk product required at time k

sk setup cost at time k

pk variable prod. cost at time k

hk unit holding cost through k

An optimal solution must carefully balance production and holding costs.

Page 74: Chapter  9 Shortest Paths and Discrete Dynamic Programming

States in Dynamic Programming

• Produce or hold for period k, but not both.• Definition of states: The process has reached state k

when requirements for periods l < k have been fulfilled and there is no on-hand inventory.

• Nodes of the digraph associated with any dynamic program corresponding to state of incomplete solution. [9.37]

• Arcs of the digraph associated with any dynamic program corresponding to decisions. They link state nodes with a subsequent state to which the decision leads. [9.38]

Page 75: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Figure 9.13 Digraph for Wagner-Whitin Example

3 76260 170

525

554 5

180

1101 55 65

360315

280

135

170

305

100

270 70

305 145

350

515

Period, k

1 2 3 4 5 6

rk 10 40 20 5 5 15

sk 50 50 50 50 50 50

pk 1 3 3 1 1 1

hk 2 2 2 2 2 2

C3,6= setup + production + holding=s3 + (r3 + r4 + r5)p3 + [(r4 + r5)h3 +r5h4]=50 + (20+5+5)3 + [(5+5)2 + (5)2]=170

Page 76: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Dynamic Programming Solutionsas an Optimal Path

• Optimal solutions to elementary dynamic programs correspond to shortest or longest paths from a beginning to an ending state in the associated digraph. [9.39]

• Dynamic programming functional equation recursions encode connections among optimal values for different states of incomplete solution. [9.40]

Page 77: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Solution for Wagner-Whitin Example

3 76260 170

525

554 5

180

1101 55 65

360315

280

135

170

305

100

270 70

305 145

350

515

[1]=0 [2]=60; d[2]=1

[3]=180; d[3]=1

[4]=280; d[4]=1 [5]=315; d[5]=1, or 3

[6]=350; d[6]=3, or 4[7]=415; d[7]=5, or 6

Page 78: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Dynamic Programming Models with Both Stages and States

• In dynamic programs with both stages and states, stages delineate the sequence of required decisions and states encode the conditions within which decisions can be considered. [9.41]

Page 79: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Example 9.6: President’s Library

The retiring president is collecting all his papers in a new presidential library. Materials for its archives are stored in covered cardboard boxes. All are 1.25 feet wide, but their height varies. .

1 2 3 4 5 6 7Height (ft.), hi 0.25 0.40 0.80 1.00 1.50 2.00 3.00

# of boxes (1000), bi 10 2 12 30 8 6 4

Boxes in the archives will be placed on metal shelves, with no box stacked on top of another. The issue is how much shelving will be required. Figure 9.14 shows that if, for example, two shelf spacings are employed. Whether they choose to provide 1, 2 ,..., 7 spacings, they want to use the space as efficiently as possible.

Page 80: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Figure 9.14 President’s Library Shelving Face Areas (two shelf spacings)

0 10 20 30 40 50 60 70

Feet of Shelves(000’s)

Box Height(feet)

3

2

1

0

Page 81: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Dynamic Programming Modeling of the President’s Library Example

stage k kth-to-last choice of a spacingstate i having provided shelves for box sizes 0,1,.. i

ci,j choice to provide a spacing for box sizes through j

Page 82: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Figure 9.15 Digraph for President’s Library Example

7

0 0 0 0 0 0 0

6

1 1 1 1 1 1

2 2 2 2 2

3 3 3 3

4 4 4

5 5

K=7 K=6 K=5 K=4 K=3 K=2 K=1 K=0

S=1 S=2 S=3 S=4 S=5 S=6 S=7S=6 15S=5 15 37.5S=4 15 35 67.5S=3 37.5 71.25 110 180S=2 12 52.5 93.75 140 225S=1 1 14 55 97.5 145 232.5S=0 3.125 6 24 67.5 116.25 170 270

Page 83: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Backward Solution of Dynamic Programs

• Elementary dynamic programs are often most easily conceptualized in backward sequence, that is, proceeding from final to initial conditions. [9.42]

• Functional equation for the President’s library example:

• k[i] optimal cost to finish from stage k, state i7[7] = 0k[i] = min {ci,j + k-1[j] : j > i } k = 1,2,..,6; i (7 - k)

Page 84: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Backward Solution of Dynamic Programs

Stage k

State i

k[i] dk[i]

1 0 270.00 71 232.50 72 225.00 73 180.00 74 67.50 75 37.50 76 15.00 7

2 0 135.00 41 122.50 42 120.00 43 105.00 44 50.00 65 30.00 6

Stage k

State i

k[i] dk[i]

3 0 117.50 41 105.00 42 102.50 43 87.50 44 45.00 5

4 0 108.13 11 100.00 42 97.50 43 82.50 4

5 0 103.13 11 96.50 32 94.50 3

Stage k

State i

k[i] dk[i]

6 0 99.63 11 95.50 2

7 0 98.63 1

Page 85: Chapter  9 Shortest Paths and Discrete Dynamic Programming

Multiple Problem Solutions Obtained Simultaneously

• The dynamic programming strategy of exploiting connections among optimal solutions for a number of related problems often implies that optima for multiple problem scenarios are produced by a single shortest or longest path computation. [9.43]