The assignment consists of implementing the Floyd-Warshall all-pairs shortest-paths algorithm.
As indicated by Exercise 25.2-4 on pages 634-635, you can simplify the algorithm by omitting all the dependencies on k when computing D. Note that this also works for computing PI. You can use the values 99 and 0 for "infinity" and "NIL" respectively.
Your program should read in n, the number of vertices, and then the n-by-n matrix of weights W (actually, you can read the weights directly into the matrix D).
Run your implementation of the Floyd-Warshall algorithm on each of the following graphs:
For each of the tests, have your program print out the matrices
D
and
PI
initially and at the end of each iteration of the outer
k-loop,
as is done in Figure 25.4 on page 631 for the
graph of Figure 25.1 (this is actually the hardest part).
The format can be as shown in Figure 25.4 with
PI(k)
printed to the right of
D(k),
or
PI(k)
can be printed below
D(k)
as:
D(0)
PI(0)
D(1)
PI(1)
D(2)
PI(2)
.
.
.
D(n)
PI(n)
Also,
at the end of each test run, use the values in
PI(n)
to
draw
the
n
shortest paths trees rooted at each of the vertices.
Hint: To possibly add infinity, I implemented an
add( int p, int q)
function that simply returned
(( p == INF ) || ( q == INF )) ? INF : p + q ;