Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
351
352
and
The latter equation shows that without using any intermediate vertices, there
is no path from 2 to 4 [A(O)(2,4)= w]. However, if 1 is allowed as an intermediate vertex, there is a shortest path (2,1,4) from 2 to 4 of length 9
[ ~ ' " ( 2 , 4 )= 91.
Similar computations show that
353
(i, k) and ~ ( ~ ) j)
( k=, ~ ( ~ - ' ) (j).
k,
(i, k)
(i, k).
Similarly,
(k, j) =
(k, j).
[These equations also follow from recurrence relation (8.5.1);see Exercise 9.1
Floyd's algorithm is given as Algorithm 8.5.3.
Algorithm 8.5.3 Floyd's Algorithm, Version 1. This algorithm computes
the length of a shortest path between each pair of vertices in a simple, undirected, weighted graph G. All weights are nonnegative. The input is the
adjacency matrix A of G. The output is the matrix A whose ijth entry is the
length of a shortest path from vertex i to vertex j.
Input Parameter:
Output Parameter:
A
A
all-paths(A) {
n = A.last
for k = 1 to n // compute
for i = 1 to n
for j = 1 to n
if (A[il[kl + A[kl[jl < A[il[jl)
A[il[jl = A[il[kl + A[kl[jl
354
I
The modified algorithm is Algorithm 8.5.4.
Algorithm 8.5.4 Floyd's Algorithm, Version 2. This algorithm computes
the length of a shortest path between each pair of vertices in a simple, undirected, weighted graph G and stores the vertex that follows the first vertex on
each shortest path. All weights are nonnegative. The input is the adjacency
matrix A of G. The output is the matrix A whose i j t h entry is the length of a
shortest path from vertex i to vertex j and the matrix next whose ijth entry
is the vertex that follows i on a shortest path from i to j .
Input Parameter:
Output Parameters:
A
A, next
all-paths(A, next) {
n = a.last
// initialize next: if no intermediate
// vertices are allowed n e x t [ i l [j ] = j
for i = 1 to n
for j = 1 to n
next[i][j ] = j
for k = 1 to n
for i = 1 to n
for j = 1 to n
if ( A [ i l [ k l +A [ k l [ j l < A [ i l [ j l ) {
A[il[jl = A[il[kl + A[kl[jl
next[i:l[jl= next[i:l[k]
I
1
After using Algorithm 8.5.4 to compute the matrix next, we can use next
to find a shortest path.
Algorithm 8.5.5 Finding a Shortest Path. This algorithm outputs a shortest
path from vertex i to vertex j in a simple, undirected, weighted graph G. It
assumes that matrix next has already been computed by Algorithm 8.5.4.
355
next, i, j
None
print-path(next, i, j) {
// if no intermediate vertices, just print i and j and return
if ( j== next[i][j]) {
print(i + " " + j)
return
1
Each time print-path is called, one or two vertices on a shortest path are
output. Therefore, the time of print-path is proportional to the length of the
path.
Example 8.5.6. For the graph of Figure 8.5.1, the matrix next computed by
Algorithm 8.5.4 is
1 4 4 4 4
3 2 3 3 5
and, row i, column j , of the matrix
Warshall's Algorithm
A relation R is transitive if, whenever ( a , b ) and ( b ,c ) are in R , ( a , c ) is also
in R. The transitive closure of a relation R is the smallest (in the sense of
set containment) transitive relation containing R. Definition 8.5.7 gives the
details.
Definition 8.5.7. Let R be a relation on X. The transitive closure of R is a
relation R' on X satisfying
356
the set
ordered pairs
( ~ 1 , ~ 2 ) , ( ~ 2 , ~ 3 ) (, x- k. -. i, , x k )E
and
( ~ k , ~ k + l ) , ( ~ k + l , ~ k + 2 ) t . . (xm-lrxm)
.,
E
with a
= xl,
b = xk, and c
R,
x,. Since
with a
Theorem 8.5.8 states that the transitive closure of a relation R is the set
of ordered pairs ( a ,b ) ,where there is a path from a to b in the digraph of R.
Example 8.5.9. The transitive closure of the relation
( ( l , 2 )(,2 , 3 ) (, 4 , 5 ) (, 5 , 4 ) ,( 5 , 5 ) 1
on ( 1 , 2 , 3 , 4 , 5 1is
R'
( ( 1 , 2 ) ,( l , 3 ) ,( 2 , 3 ) (, 4 , 4 ) (, 4 , 5 ) ,( 5 , 4 ) (, 5 , 5 ) 1 .
357
The digraphs of R and R' are shown in Figure 8.5.3. We see that (1,3) E R'
because there is a path (1,2,3) from 1 to 3 in the digraph of R. Similarly,
(4,4) E R' because there is a path (4,5,4) from 4 to 4 in the digraph of R.
Figure 8.5.3 (a) The digraph of the relation R of Example 8.5.9. (b)The
digraph of the closure R' of R.
Example 8.5.1 0. A telecornrnunications network has centers in Atlanta, Denver, Los Angeles, Miami, Philadelphia, and Seattle. There are direct, one-way
links from Atlanta to Denver, from Atlanta to Philadelphia, from Denver to
Los Angeles, from Los Angeles to Atlanta, and from Miami to Seattle. Consider the problem of determining whether there is a link, either direct or
indirect (i.e., going through intermediate cities), from one city to another.
Define a relation R on the cities as (ci, cj) E R if there is a direct, one-way
link from C i to cj. Then there is a direct or indirect link from C i to c j if and
only if (ci,cj) is in the transitive closure of R , since a direct or indirect link
from C i to c j is a path in the digraph of R.
358
A ( ~(i,
) j) = A ( ~ -(2,
~ j).
)
Input Parameter:
Output Parameter:
A
A
transitive-closure(A) {
n = A.last
for k = 1 to n
for i = 1 to n
for j = 1 to n
A[il[jl = A [ i l [ j l v (A[il[kl
A[kl[jl)
1
Since each for loop in Algorithm 8.5.12 runs in time O ( n ) , Warshall's
algorithm runs in time O ( n 3 ) .
Exercises
Trace Floyd's algorithm for each graph in Exercises 1-3. Show each of the
matrices
a n d the matrix next of Algorithm 8.5.4. Show the output of
Algorithm 8.5.5 for each pair of vertices.