Loading...

PPT – Shortest Path Problems: PowerPoint presentation | free to download - id: 413c01-ODFlY

The Adobe Flash plugin is needed to view this content

Shortest Path Problems

- Floyd-Warshall Algorithm for the All-Pairs

Shortest Path Problem with Arbitrary Arc Costs - Updated 18 February 2008

Floyd-Warshall Algorithm (AMO pg 148)

begin for all node pairs i,j ? N x N do

di,j ?, predi,j 0 for all nodes i ?

N do di,i 0 for all arcs (i, j) ? A do

di, j cij and predi, j i for k

1 to n do for all node pairs i, j ? N x

N do if di, j gt di, k dk, j

then di, j di, k dk, j and

predi, j predk, j end

Interpretation of d and pred Matrices

- At the end of iteration k, di, j is the length

of a shortest path from i to j that uses only

nodes in the set 1, 2, , k as internal nodes. - predi, j is the node prior to node j on the

(current) shortest path from i to j.

The Triangle Operation Iteration k

k

i

j

Check if di, j gt di, k dk, j

The Triangle Operation Update predi,j

k

i

predk, j

old predi, j

j

new predi, j predk, j

Floyd-Warshall (FW) Example 1

12

2

1

1

2

1

4

3

4

3

FW Example 1 End of Iteration 1

12

2

1

1

2

1

4

3

4

3

FW Example 1 End of Iteration 2

12

2

1

1

2

1

4

3

4

3

FW Example 1 End of Iteration 3

12

2

1

1

2

1

4

3

4

3

FW Example 1 Solution

12

2

1

1

2

1

4

3

4

3

FW Example 1 Shortest Path from 1 to 2

12

2

1

1

2

1

4

3

4

3

pred1,2 3

pred1,3 4

pred1,4 1

Complexity of Floyd-Warshall

- Each triangle operation is O(1)
- Each iteration does n2 triangle operations
- There are n iterations
- Complexity is O(n3)

Testing for Negative-Cost Cycles

for k 1 to n do for all node pairs i,

j ? N x N do if di, j gt di, k

dk, j then begin di, j

di, k dk, j and predi, j predk,

j if i j and di, i lt 0 then

exit (G has a negative-cost cycle)

end

FW Example 2 (From Papadimitriou and Steiglitz)

2

FW Example 2 (after k 1)

2

FW Example 2 (after k 2)

2

FW Example 2 d4, 4 1

2

2

1

-4

1

1

3

4

3

pred4,4 1

pred4,1 2

pred4,2 4