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.
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(p,q)
macro that simply returned
(( p == INFINITY ) || ( q == INFINITY )) ? INFINITY : p + q
Helpful code: Here is a .h file for a new Graph class graph.h and skeleton code for the Graph class implementation graph.cpp (you implement parts associated with the Floyd-Warshall algorithm). Note: the Graph class has been designed to work with both weighted and unweighted graphs, using either the adjacency list or adjacency matrix representation. Here is a "driver" program that tests the Floyd-Warshall algorithm: driver.cpp. Here is a "Makefile" that puts it all together: Makefile. When you download these files, be sure to remove the ".txt" suffixes (by moving them to the same file name without the ".txt").
Also, in order to test your program, here is the
input file for Figure 25.1
- it should produce the output shown in Figure 25.4 on page 631.