You are on page 1of 3

Appendix: The Floyd-Warshall Algorithm

This is a summary of the Floyd-Warshall algorithm. It should be sucient for your needs in this
problem set, but you can nd a full discussion of the algorithm on pages 558-562 of Introduction to
Algorithms, by Cormen, Leiserson, and Rivest (CLR). You are also free to use any other algorithm
you wish to compute shortest paths.
Note that the pseudo-code presented here assumes that edge costs are non-negative.
The pseudo-code in CLR is more general.
The Floyd-Warshall algorithm is used to solve the all-pairs shortest-paths problem on a directed
graph G = (V,E). The algorithm pre-computes a distance matrix D and a path matrix P in (V 3 )
time. (You should make sure your implementation actually achieves a running time of (V 3 );
otherwise your TAs might get tired of waiting for your code to run...) D[v,w] contains the cost
of the lowest cost path from vertex v to vertex w. P[v,w] holds a vertex k which is the direct
predecessor of w on the least cost path between v and w. Once D and P have been computed,
calculating the minimum cost between two vertices is a constant time lookup in D, and calculating
the minimum cost path is a (V) computation using P.

0.1 Pre-computing the Distance and Path Matrices


The distance matrix D and the path matrix P are initialized as follows:
8 9
>
< 0 i=j >
=
8 D [i; j ] =
> 1
: cost of (i; j )
6
if (i; j)  E and i = j6 >
;
6
i;j

if (i; j)  E and i = j
( )
8 [ ]= NIL if (i; j) 6  E or i = j
i if (i; j)  E and i 6= j
i;j P i; j

(You will need to come up with some way to represent 1 and NIL in your code.)
The matrices are then computed as follows:
for k = 1 to N
for i = 1 to N
for j = 1 to N
if D[i; k] + D[k; j ] < D[i; j ] then
[ ] = D[i; k] + D[k; j ]
D i; j
[ ] = P [k; j ]
P i; j

0.2 Computing the Shortest Path Given a Path Predecessor Matrix


For each pair of nodes v and w, the path matrix entry P[v,w] (as computed above) contains a node
k which is the direct predecessor of w on the least cost path from v to w. (A NIL value indicates

1
that there is no path.) So the least cost path from v to w is the least cost path from v to k (where
k = P[v,w]), followed by w:
path v; w( ) = path(v; P [v; w])!w
where path(i; j ) = i ! ::: ! j
and path(i; i) = i

0.3 An Example (modi ed from CLR)

2
3 4

1 3
8
7 1
4 2 5

5 4
6

Figure 1: Example Graph

Consider the graph in Figure 1. For this graph, we would initialize D and P to be:
0 0 3 8 1 4 1 0 NIL 1 1 NIL 1
1
BB 1 0 1 1 7 CC BB NILNIL NIL 2 2 CC
B
D=B 1 4 1 CCC B CC
B@ 2 1 50 10 1 A
P = B NIL
B@ 4 3 NIL NIL NIL
NIL 4 NIL NIL CA
1 1 1 6 0 NIL NIL NIL 5 NIL
and our nal values for D and P are:
00 3 8 4 4
1 0 NIL 1 1 2 1
1
BB 3 0 6 1 7 CC BB 4 NIL 4 2 2 CC
B
D=B 7 4 0 5 11 CC B
P =B 4 3 NIL 2 2 CC
B@ 2 5 5 0 6 CA B@ 4 1 4 NIL 1 CA
8 11 11 6 0 4 1 4 5 NIL

2
From the nal matrices, we can see that the shortest path from vertex 5 to vertex 2 has a cost
of 11 (D[5,2]) and moves from 5 to 2 via 1 (P[5,2]), and from 5 to 1 via 4 (P[5,1]), for a path of
5 ! 4 ! 1 ! 2.