Sei sulla pagina 1di 9

350

Chapter 8 / Dynamic Programming

8.5 The Algorithms of Floyd and Warshall


In Section 7.4, we discussed Dijkstra's algorithm, which finds shortest-paths
from a designated vertex to all other vertices in a graph. In the present
section, we discuss the all-pairs, shortest-path problem-find a shortest
path between each pair of vertices in a weighted graph-and a dynamicprogramming algorithm due to Floyd that solves this problem. Essentially
the same algorithm, known as Warshall's algorithm, solves the transitiveclosure problem. The transitive closure of a relation R is the smallest transitive relation containing R. We begin with Floyd's algorithm.
Floyd's Algorithm
Throughout this subsection, we assume that G is a simple, undirected,
weighted graph with vertices 1,... ,n. We let w (i, j) denote the weight of
edge ( i ,j ) . We assume that w ( i ,j) 0 for all i and j. We represent G using
an adjacency matrix. If there is an edge incident on i and j , the entry in row
i, column j , is w ( i ,j ) . If no edge is incident on i and j , i + j, the entry in
row i, column j, is 00. If i = j , the entry in row i, column j , is zero.
Example 8.5.1. The adjacency matrix of the graph shown in Figure 8.5.1 is

Figure 8.5.1 A graph to illustrate the all-pairs, shortest-path problem.

A dynamic-programming algorithm to solve the all-pairs, shortest-path


problem must begin by computing simple cases and end by solving the problem of computing a shortest path between each pair of vertices. In order
to simplify the problem, we restrict the possible intermediate vertices on a
path-starting by allowing no intermediate vertices (i.e., the path must go
directly from the start vertex to the end vertex). In this case, the adjacency
matrix gives the length of a shortest path between each pair of vertices. If
no intermediate vertices are allowed, the shortest path from i to j , i * j, is
the edge ( i ,j ) , whose length is w (i,j ) , if such an edge exists. If there is no

8.5 / The Algorithms of Floyd and Warshall

351

edge incident on i and j, i + j , there is no path from i to j , which is denoted


oo. Regardless of which intermediate vertices are allowed, the shortest path
from i to i is always a path of length zero.
To explain Floyd's dynamic-programming algorithm, it is helpful to introduce some notation. For now, we concentrate on computing lengths of
shortest paths rather than computing shortest paths themselves. We let
denote the matrix whose ijth entry is the length of a shortest path from i
to j , where the only intermediate vertices allowed are 1,...,k. Matrix A ( O ) is
interpreted as allowing no intermediate vertices on paths, so A(O) is the adjacency matrix of the graph. The ijth entry in
is the length of a shortest
path from i to j, where the intermediate vertices allowed are 1,. ..,n; that
is, any intermediate vertices may appear on a path from i to j. Since
has no restrictions on the vertices that make up the paths, the i j t h entry in
is the length of a shortest path from i to j . Thus,
is a solution to
the all-pairs, shortest-path problem.
The algorithm begins with
the adjacency matrix of the graph. It next
computes A('), then
and so on, until it computes
which solves the
all-pairs, shortest-path problem.
Consider the problem of computing
given that
has already
been computed. The only difference between
and
is that an additional vertex-namely k-may be used as an intermediate vertex to obtain
a shortest path for
Let P be a shortest path from i to j, where the only
intermediate vertices allowed are 1,... , k. If P does not use k as an intermediate vertex, P is also a shortest path from i to j, where the only intermediate
vertices allowed are 1,...,k - 1. In this case,

If P does use k as an intermediate vertex, we have the situation shown in


Figure 8.5.2. The part of P from i to k must be a shortest path from i to k
that uses as intermediate vertices only 1,... , k - 1. For, if there is a shorter
path P' from i to k that uses as intermediate vertices only 1,. ..,k - 1, we
could replace the portion of P from i to k with P' to obtain a path shorter
than P from i to j that uses as intermediate vertices only 1,. ..,k. We have
shown that the optimal substructure property holds for shortest paths. Sim-

Figure 8.5.2 P is a shortest path from i to j in which k appears as an


intermediate vertex. The only vertices allowed as intermediate vertices on P
are 1,. ..,k. It follows that the part of P from i to k is a shortest path that
uses as intermediate vertices only 1 , ... , k - 1, and the part of P from k to j
is a shortest path that uses as intermediate vertices only 1 , .. ., k - 1.

352

Chapter 8 / Dynamic Programming


ilarly, the part of P from k to j is a shortest path from k to j that uses as
intermediate vertices only 1,...,k - 1. We have shown that in this case,

Either k is used on a shortest path or it is not; thus, in general,

The preceding recurrence relation is precisely what is needed to compute the


lengths of aU shortest paths in a graph.
Example 8.5.2. We use the recurrence relation (8.5.1) to compute the lengths
of all shortest paths in the graph of Figure 8.5.1. Since matrix A(O) is the
adjacency matrix, we have

Sample computations of A(') are

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

8.5 / The Algorithms of Floyd and Warshall

Suppose that when we compute


using
instead of creating a
with the new values of
new matrix, we overwrite the old values in
Such a computation is said to be done, in place or, for lovers of Latin, in situ.
When we compute ~ ( ~ ) j),
( i we
, use A ( ~ -values:
~ )

(k, j) have already been computed and have


If the values
(i, k) and
overwritten ~ ( ~ - " (k)i , and ~ ( ~ - ' ) (j k) ,, there is a problem if the original
values of ~ ( ~ - ' ) (k)i ,and ~ ( ~ - l ) (j)
k changed.
,
However, these values do trot
change; that is,
~ ( ~ ' k)
( i =,

(i, k) and ~ ( ~ ) j)
( k=, ~ ( ~ - ' ) (j).
k,

The first equation follows from the fact that ~ ( ~ ) k)


( i is, the length of a
shortest path from vertex i to vertex k that uses as intermediate vertices
only 1,..., k. However, such a path does not use vertex k as an intermediate
vertex because k is the end vertex. Therefore,

(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

Chapter 8 / Dynamic Programming


Since each for loop in Algorithm 8.5.3 is executed n times, Floyd's algorithm runs in time @ ( n 3 ) .
Inorder to find a shortest path between each pair of vertices, we introduce
a second matrix next; next[i][ j ] is the vertex that follows i on a shortest path
from i to j . We initialize next[i:l[ j l to j for all i and j since, at the start of
the algorithm, no vertices are allowed as intermediate vertices. Whenever we
find a shorter path that results from introducing an intermediate vertex, we
update next [ i ][j ] :
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
n e x t [ i : l [ j ]= next[i][ k ]

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.

8.5 / The Algorithms of Floyd and Warshall


Input Parameters:
Output Parameters:

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

// output i and then the path from the vertex


// after i (next[i][ j ] ) to j
print(i + " ")
print-path(next, next[i][j], j)

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

shows the output of print-path(i, j ) .


Although our discussion of Floyd's algorithm was restricted to undirected
graphs, the same algorithm can be applied to directed graphs. In this case,
" p a t h means directed path, that is, a path of the form ( v l , . . .,vk), where
(vi,v ~ +is~a )directed edge from vi to vi+l, i = 1,. ..,k - 1.

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

Chapter 8 / Dynamic Programming


(a) R E R'.
(b) R' is transitive.
(c) If R" is any transitive relation on X and R c R", then R' E R".
Consider constructing the transitive closure R' of a relation R. If

in order for R' to be transitive, we must have ( x l ,xk)


shows that R' is precisely the set of all such ( x 1 , x k ) .

R'. Theorem 8.5.8

Theorem 8.5.8. Let R be a relation on a set X . The transitive closure of R is

the set

Proof. Taking k = 2 in (8.5.2),we see that R L R'.


Next, we show that R' is transitive. Let ( a ,b ) ,( b ,c ) E R'. Then there are

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

( a , ~E )R'. Therefore R' is transitive.


Let R" be any transitive relation on X containing R. We must show that
R' s R". Let ( a ,b ) E R'. Then there are ordered pairs

with a

xl and b = xk. Since R E R",

Because R" is transitive, ( a ,b ) = ( x l , x k )E R". Therefore, R'


proof is complete.

R" and the

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 .

8.5 / The Algorithms of Floyd and Warshall

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.

To compute the transitive closure of a relation R , Warshall's algorithm


uses the same logic as Floyd's algorithm. The adjacency matrix of Floyd's
algorithm is replaced by the matrix of R , and instead of computing the length
of a shortest path from i to j , Warshall's algorithm determines whether there
are elements in R of the form

that is, whether (i,j ) is in the transitive closure of R . In order to compare


Warshall's algorithm with Floyd's algorithm, we call (8.5.3) a path from i to j .
Example 8.5.1 1. The matrix of the relation R of Example 8.5.9 is

Warshall's algorithm uses matrices


similar to the ones used by Floyd's
algorithm. In Warshall's algorithm, ~ ( ~ ' j)
( i=, 1 if there is a path from i to

358

Chapter 8 / Dynamic Programming


j that uses as intermediate elements only 1,. ..,k, and 0 if there is no such
path. Matrix A(O) is the matrix of the relation. If there is a path from i to
j that uses as intermediate elements only 1,. .. ,k - 1, then this same path
uses as intermediate elements only 1,..., k. In this case,

A ( ~(i,
) j) = A ( ~ -(2,
~ j).
)

If there is no path from i to j that uses as intermediate elements only


1,. ..,k - 1, there is a path from i to j that uses as intermediate elements
only 1,. . .,k if and only if there are paths from i to k and from k to j that
use as intermediate elements only 1, ... , k - 1. In this case,
~ ( ~ ' j)
( i =, ~ ( ~ - ' )k() iA, ~ ( ~ - ~ j).
)(k,
It follows that, in general,

can be computed in place for exactly the same reason that


The matrices
they can be computed in place in Floyd's algorithm. Warshall's algorithm is
given as Algorithm 8.5.12.
Algorithm 8.5.1 2 Warshall's Algorithm. This algorithm computes the transitive closure of a relation R on { 1,. ..,n ) . The input is the matrix A of R.
The output is the matrix A of the transitive closure of R.

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.

Potrebbero piacerti anche