Floyds Algorithm for the APSP Problem

J.E.D.I

6.7.2 Floyds Algorithm for the APSP Problem

In finding all-pairs shortest path, Dijkstras algorithm may be used with every pair of sources and destinations. However, it is not the best solution there is for the APSP problem. A more elegant approach is to use the algorithm created by Floyd. The algorithm makes use of cost adjacency matrix representation of a graph. It has a dimension of n x n for n vertices. In this algorithm, let COST be the given adjacency matrix. Let A be the matrix to contain the cost of the shortest path, initially equal to COST. Another n x n matrix, PATH, is needed to contain the immediate vertices along the shortest path: PATH i,j = 0 initially, indicates that the shortest path between i and j is the edge i,j if it exists = k if, including k in the path from i to j at the k th iteration, yields a shorter path The algorithm is as follows: 1. Initialize A to be equal to COST: Ai, j = COSTi, j, 1 ≤ i, j ≤ n 2. If the cost of passing through the intermediate vertex k from vertex i to vertex j will cost less than direct access from i to j, replace Ai,j with this cost and update PATHi,j as well: For k = 1, 2, 3, ..., n a Ai, j = minimum [ A k-1 i, j, A k-1 i, k + A k-1 k, j ] , 1 ≤ i, j ≤ n b If Ai, j == A k-1 i, k + A k-1 k, j set PATHi, j = k For example, solve the APSP problem on the following graph using Floyds algorithm: Figure 1.101 A Weighted Directed Graph for APSP Example Figure 1.102 Since no self-referencing is allowed, there is no need to compute for Aj, j, for 1 ≤ j ≤ n. Data Structures 119 J.E.D.I Also, for the k th iteration, there will be no changes for the k th row and column in A and PATH since it will add only 0 to the current value. For example, if k = 2: A2, 1 = minimum A2, 1, A2,2 + A2,1 since A2, 2 = 0, there will always be no change to k th row and column. The following shows the execution of Floyds algorithm: 1 2 3 4 1 2 3 4 1 2 ∞ 12 1 2 8 ∞ 7 2 3 5 10 7 3 4 ∞ ∞ 1 4 A PATH For the first iteration k=1: A2, 3 = minimum A2, 3 , A2, 1 + A1, 3 = minimum∞, ∞ = ∞ A2, 4 = minimum A2, 4 , A2, 1 + A1, 4 = minimum12, 20 = 12 A3, 2 = minimum A3, 2 , A3, 1 + A1, 2 = minimum10, 7 = 7 A3, 4 = minimum A3, 4 , A3, 1 + A1, 4 = minimum7, 17 = 7 A4, 2 = minimum A4, 2 , A4, 1 + A1, 2 = minimum∞, ∞ = ∞ A4, 3 = minimum A4, 3 , A4, 1 + A1, 3 = minimum1, ∞ = 1 1 2 3 4 1 2 3 4 1 2 ∞ 12 1 k = 1 2 8 ∞ 7 2 3 5 7 7 3 1 4 ∞ ∞ 1 4 A PATH For k=2: A1, 3 = minimum A1, 3 , A1, 2 + A2, 3 = minimum∞, ∞ = ∞ A1, 4 = minimum A1, 4 , A1, 2 + A2, 4 = minimum12, 9 = 9 A3, 1 = minimum A3, 1 , A3, 2 + A2, 1 = minimum5, 15 = 5 A3, 4 = minimum A3, 4 , A3, 2 + A2, 4 = minimum7, 12 = 7 A4, 1 = minimum A4, 1 , A4, 2 + A2, 1 = minimum∞, ∞ = ∞ A4, 3 = minimum A4, 3 , A4, 2 + A2, 3 = minimum1, ∞ = ∞ 1 2 3 4 1 2 3 4 1 2 ∞ 9 1 2 k = 2 2 8 ∞ 7 2 3 5 7 7 3 1 Data Structures 120 J.E.D.I 1 2 3 4 1 2 3 4 4 ∞ ∞ 1 4 A PATH For k=3: A1, 2 = minimum A1, 2 , A1, 3 + A3, 2 = minimum2, ∞ = ∞ A1, 4 = minimum A1, 4 , A1, 3 + A3, 4 = minimum9, ∞ = ∞ A2, 1 = minimum A2, 1 , A2, 3 + A3, 1 = minimum8, ∞ = ∞ A2, 4 = minimum A2, 4 , A2, 3 + A3, 4 = minimum7, ∞ = ∞ A4, 1 = minimum A4, 1 , A4, 3 + A3, 1 = minimum∞, 6 = 6 A4, 2 = minimum A4, 2 , A4, 3 + A3, 2 = minimum∞, 8 = 8 1 2 3 4 1 2 3 4 1 2 ∞ 9 1 2 k = 3 2 8 ∞ 7 2 3 5 7 7 3 1 4 6 8 1 4 3 3 A PATH For k=3: A1, 2 = minimum A1, 2 , A1, 4 + A4, 2 = minimum2, 17 = 2 A1, 3 = minimum A1, 3 , A1, 4 + A4, 3 = minimum∞, 10 = 10 A2, 1 = minimum A2, 1 , A2, 4 + A4, 1 = minimum8, 13 = 8 A2, 3 = minimum A2, 3 , A2, 4 + A4, 3 = minimum∞, 8 = 8 A3, 1 = minimum A3, 1 , A3, 4 + A4, 1 = minimum5, 13 = 5 A3, 2 = minimum A3, 2 , A3, 4 + A4, 2 = minimum7, 15 = 7 1 2 3 4 1 2 3 4 1 2 10 9 1 4 2 k = 4 2 8 8 7 2 4 3 5 7 7 3 1 4 6 8 1 4 3 3 A PATH After the n th iteration, A contains the minimum cost while PATH contains the path of the minimum cost. To illustrate how to use the resulting matrices, let us find the shortest path from vertex 1 to vertex 4: A1, 4 = 9 PATH1, 4 = 2 -- Since not 0, we have to get PATH2, 4: PATH2, 4 = 0 Therefore, the shortest path from vertex 1 to vertex 4 is 1 -- 2 -- 4 with cost 9. Even if there is a direct edge from 1 to 4 with cost 12, the algorithm returned another path. This example shows that it is not always the direct connection that is returned in getting Data Structures 121 J.E.D.I the shortest path in a weighted, directed graph.

6.8 Summary