The All-Pairs Shortest-Path Problem

The All-Pairs Shortest-Path Problem

In discussing connectivity in Chapter 13, we wanted to know whether it was possible to fly from Athens to Murmansk if we didn’t care how many stops we made. With weighted graphs we can answer the second question that might occur to you as you wait at the Hubris Airlines ticket counter: How much will the journey cost?

To find whether a trip was possible, we created a connectivity table. With weighted graphs we want a table that gives the minimum cost from any vertex to any other vertex using multiple edges. This is called the all-pairs shortest-path problem.

You can create such a table by running the path.java program using each vertex in turn as the starting vertex. This will yield something like Table 14.11.

TABLE 14.11 All-Pairs Shortest-Path Table

In the preceding chapter we found that Warshall’s algorithm was a quicker way to create a table showing which vertices could be reached from a given vertex using one or many steps. An analogous approach for weighted graphs uses Floyd’s algorithm, discovered by Robert Floyd in 1962. This is another way to create the kind of table shown in Table 14.11.

Let’s discuss Floyd’s algorithm with a simpler graph. Figure 14.11 shows a weighted directed graph and its adjacency matrix.

A weighted graph and its adjacency matrix.

The adjacency matrix shows the cost of all the one-edge paths. We want to extend this matrix to show the cost of all paths regardless of length. For example, it’s clear

The All-Pairs Shortest-Path Problem 709

from Figure 14.11 that we can go from B to C at a cost of 30 (10 from B to D plus 20 from D to C).

As in Warshall’s algorithm we systematically modify the adjacency matrix. We examine every cell in every row. If there’s a non-zero weight (say a 30 at row C column A), we then look in column C (because C is the row where the 30 is). If we find an entry in column C (say a 40 at row D), we know there is a path from C to A with a weight of 30 and a path from D to C with a weight of 40. From this, we can deduce that there’s a two-edge path from D to A with a weight of 70. Figure 14.12 shows the steps when Floyd’s algorithm is applied to the graph in Figure 14.11.

FIGURE 14.12 Floyd’s algorithm. Row A is empty, so there’s nothing to do there. In row B there’s a 70 in column A

and a 10 in column D, but there’s nothing in column B, so the entries in row B can’t

be combined with any edges ending on B. In row C, however, we find a 30 at column A. Looking in column C, we find a 20 at

row D. Now we have C to A with a weight of 30 and D to C with a weight of 20, so we have D to A with a weight of 50.

Row D shows an interesting situation: We will lower an existing cost. There’s a 50 in column A. There’s also 10 in row B of column D, so we know there’s a path from B to A with a cost of 60. However, there’s already a cost of 70 in this cell. What do we do? Because 60 is less than 70, we replace the 70 with 60. In the case of multiple paths from one vertex to another, we want the table to reflect the path with the lowest cost.

The implementation of Floyd’s algorithm is similar to that for Warshall’s algorithm. However, instead of simply inserting a 1 into the table when a two-edge path is found, we add the costs of the two one-edge paths and insert the sum. We’ll leave

710 CHAPTER 14 Weighted Graphs