Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Floyd's Algorithm

Floyd's algorithm  uses the dynamic programming method to solve the all-pairs shortest-path problem on a dense graph. The method makes efficient use of an adjacency matrix to solve the problem. Consider an edge-weighted graph tex2html_wrap_inline69997, where C(v,w) represents the weight on edge (v,w). Suppose the vertices are numbered from 1 to tex2html_wrap_inline70423. That is, let tex2html_wrap_inline71341. Furthermore, let tex2html_wrap_inline71343 be the set comprised of the first k vertices in tex2html_wrap_inline69999. That is, tex2html_wrap_inline71349, for tex2html_wrap_inline71351.

Let tex2html_wrap_inline71353 be the shortest path from vertex v to w that passes only through vertices in tex2html_wrap_inline71343, if such a path exists. That is, the path tex2html_wrap_inline71353 has the form

displaymath71321

Let tex2html_wrap_inline71363 be the length of path tex2html_wrap_inline71353:

displaymath71322

Since tex2html_wrap_inline71367, the tex2html_wrap_inline66510 paths are correspond to the edges of G:

displaymath71323

Therefore, the tex2html_wrap_inline71373 path lengths correspond to the weights on the edges of G:

displaymath71324

Floyd's algorithm computes the sequence of matrices tex2html_wrap_inline71377. The distances in tex2html_wrap_inline71379 represent paths with intermediate vertices in tex2html_wrap_inline71381. Since tex2html_wrap_inline71383, we can obtain the distances in tex2html_wrap_inline71385 from those in tex2html_wrap_inline71379 by considering only the paths that pass through vertex tex2html_wrap_inline70161. Figure gif illustrates how this is done.

   figure51447
Figure: Calculating tex2html_wrap_inline71385 in Floyd's algorithm.

For every pair of vertices (v,w), we compare the distance tex2html_wrap_inline71407, (which represents the shortest path from v to w that does not pass through tex2html_wrap_inline70161) with the sum tex2html_wrap_inline71415 (which represents the shortest path from v to w that does pass through tex2html_wrap_inline70161). Thus, tex2html_wrap_inline71385 is computed as follows:

displaymath71325


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.