ALL-PAIRS SHORTEST PATHS

10.5 ALL-PAIRS SHORTEST PATHS

E) is given, as shown, for example, in Fig. 10.6. For convenience, we shall refer in this section to the weight of edge

A directed and weighted graph G =

as its length. For every pair of vertices and

in it is required to find the shortest path from

to along edges in E. Here the length of a path or cycle is the sum of the lengths of the edges forming it. In Fig. 10.6, the shortest path from

to is along Formally, the all-pairs shortest paths problem is stated as follows: An n-vertex

graph

G is given by its n x n weight matrix W; construct an n x n matrix D such that is the length of the shortest path from to in

G for all i and j. We shall assume that W has positive, zero, or negative entries as long as there is no

of negative length in G. Let

denote the length of the shortest path from to that goes through at most k - 1 intermediate vertices. Thus

that is, the weight of the edge from to

In particular, if there is no edge from to where i and j are distinct,

Also

Given that G has no cycles of negative length, there is no advantage in visiting any vertex more than once in a shortest path from

to (even if our

Graph Theory Chap. 10

Figure 10.5 Computing connected components of graph.

definition of a path allowed for a vertex to appear more than once on a path). It follows that

In order to compute

for k

1 we can use the fact that

= min

that is, is equal to the smallest

+ over all values of Therefore matrix D

10.5 All-Pairs Shortest Paths

Figure

Directed and weighted

graph.

can be generated from

and then taking D = In order to obtain D k from

by computing D 2 ,D 4 ,. ..,

by the preceding expression, we can use a special form of matrix multiplication in which the standard operations of matrix multiplication, that is, x and + are replaced by + and min, respectively. Hence if a matrix multiplication procedure is available, it can be modified to generate

from Exactly

such matrix products are required. The algorithm is implemented in parallel using any of the matrix multiplication

procedures described in section

7.3 adapted to perform ( +, min) multiplication. Once

again, as we did in the previous two sections, we shall invoke procedure CUBE MATRIX MULTIPLICATION. The resulting algorithm is given in what follows as procedure CUBE SHORTEST PATHS. The procedure runs on a cube-connected

SIMD computer with N = n 3 processors, each with three registers A, B, and C. As before, the processors can be regarded as being arranged in an n x n x n array pattern. Initially,

- 1, that is, the processors in positions j, k) contain the weight matrix of G. If is not connected to or i f j = k, then

When the procedure terminates, j, k) contains the length of the shortest path from to for

j, k n - 1.

procedure CUBE SHORTEST PATHS (A, C) Step 1: {Construct the matrix

and store it in registers A and B)

for j = to -

1 do in parallel

for k = to n - 1 do in parallel

(1.1) if j

end for end for.

260 Graph Theory

Chap. 10 Step 2: {Construct the matrices

D 4 ,. ..,

through repeated matrix multiplication}

for i = 1 to

do

(2.1) CUBE MATRIX MULTIPLICATION (A, B, C) (2.2) for j = to n

1 do in parallel

for k = to - 1 do in parallel

end for end for end for.

Analysis. Steps

1 and 2.2 take constant time. There are

iterations of step 2.1 each requiring n) time. The overall running time of

Figure 10.7 Computing all-pairs shortest paths for graph in Fig. 10.6.

10.6 Computing the Minimum Spanning Tree 261 procedure CUBE SHORTEST PATHS is therefore

= O(log z n). Since = n 3 , = O(n 3 10g 2 n).

for the graph in Fig. 10.6 are shown in Fig. 10.7.