Assignment 14 -- Due Thursday, December 12 (at the beginning of lab)
CS 5521 Fall Semester, 2013
20 Points

Topics: The Floyd-Warshall algorithm

The assignment consists of implementing the Floyd-Warshall all-pairs shortest-paths algorithm.

The Floyd-Warshall algorithm

Implement a version of the Floyd-Warshall algorithm that also computes the predecessor matrix PI. This can be done by iteratively computing the PI(k) matrices along with the D(k) matrices. The intialization for PI(0) is given in Equation (25.6) on page 695; the general case is given in Equation (25.7) on page 697.

As indicated by Exercise 25.2-4 on page 699, 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:

  1. The graph of Figure 24.2 on page 648. As in the previous assignment relabel the vertices as 1, 2, 3, 4, 5 instead of using the letters s, t, x, y, z respectively. Here is the input file for Figure 24.2 .
  2. The graph of Figure 24.6 on page 659. As in the previous test relabel the vertices as 1, 2, 3, 4, 5 instead of using the letters s, t, x, y, z respectively. Here is the input file for Figure 24.6 .
  3. The graph of Figure 25.2 on page 691 (which already has numbered vertices, and so doesn't need relabeling). Here is the input file for Figure 25.2 .

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 696 for the graph of Figure 25.1 page 690 (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 696.


Page URL: http://www.d.umn.edu /~ddunham/cs5521f13/assignments/a14/assignment.html
Page Author: Doug Dunham
Last Modified: Thursday, 17-Oct-2013 16:02:18 CDT
Comments to: ddunham@d.umn.edu