









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Floyd’s Algorithm is applied to solving the all–pairs shortest paths problem in graphs, both directed and undirected. It can be used to determine the shortest path from one specific vertex to all other vertices. However, there are algorithms that are more efficient for that limited problem. Floyd’s Algorithm is normally applied to weighted graphs, but can be extended to unweighted graphs simply by assigning a weight of 1 to each edge. Floyd’s algorithm starts with the adjacency matrix for the weighted graph. In the weighted graph, A[I, J] = W( (I, J) ), the weight assigned to edge (I, J). Question: What weight to assign to edges that do not exist. We return to this a bit later. Floyd’s Algorithm is quite similar to Warshall’s Algorithm, and uses the same basic strategy to generate the answers. Because of the similarity, the two algorithms are almost always discussed in sequence. Warshall’s Algorithm is first, then Floyd’s Algorithm.
Here is a sample graph. It is based on the one in the textbook. The adjacency matrix for this graph might be represented as follows. Vertex 1 2 3 4 1 0 0 3 0 2 2 0 0 0 3 0 7 0 1 4 6 0 0 0 This representation will not work for minimization problems. Algorithms for these problems need to avoid use of edges that are not there. In this representation, it appears that each of edge (2, 4) and (4, 2) exist and have weight 0.
The value works well for hand solutions, as we all know it is larger than any weight that can be assigned to an edge that really exists in the graph. The difficulty comes in representing as a value in the computer. Most real number systems may have difficulty doing computations with the pseudo–value. The basic idea behind infinite–weight edges is valid: represent nonexistent edges as existing with such large weights that a minimization algorithm will never use them. My solution is based on the sum of the weights of edges that do exist in the graph. Consider again the first adjacency matrix. Vertex 1 2 3 4 1 0 0 3 0 2 2 0 0 0 3 0 7 0 1 4 6 0 0 0 The sum of the weights for existent edges is 19. No real path through this graph can have a total weight more than 19. Double the sum of the edge weights and pick a value larger than that. I use 40.
Here is the version of the adjacency matrix that I favor. Note that the diagonal values of 0 will not cause any difficulty. The distance from a vertex to itself is 0 and will remain so throughout the computation. Vertex 1 2 3 4 1 0 40 3 40 2 2 0 40 40 3 40 7 0 1 4 6 40 40 0 Floyd’s algorithm is a minimization algorithm. We have selected the value 40 with some care so that no distance through existing edges will ever be 40 or greater. Given that constraint, Floyd’s algorithm will never replace a nonexistent edge with a supposed path containing a nonexistent edge.
Floyd’s Algorithm begins with the adjacency matrix, adjusted to show weights. We now call this matrix W. For our problem, we have the following. As in Warshall’s Algorithm, we generate a sequence of (N + 1) matrices. The initial definition is as follows. is the distance of the shortest path I J involving no vertices numbered above K.
The first observation is that This arrives from the observation that Floyd’s Algorithm is based on minimization, so that consideration of new paths will not drive up the total distance. The second observation is that we can compute the distances using subsets of the vertex set. Given distances involving only vertices in the set [1, K – 1], we now add vertex K to the calculation and examine the following two paths from vertex I to vertex J. If vertex K offers a “short cut”, it becomes part of the path.
The key step in the calculation is D (K) [I, J] = min( D (K-1) [I, J], D (K-1) [I, K] + D (K-1) [K, J] ) If I = J, then D (K-1) [I, J] = 0. We shall never decrease this value. If I = K, then the equation becomes D (K) [K, J] = min( D (K-1) [K, J], D (K-1) [K, K] + D (K-1) [K, J] ), which becomes D (K) [K, J] = min( D (K-1) [K, J], 0 + D (K-1) [K, J] ) = D (K-1) [K, J] If J = K, then the equation becomes D (K) [I, K] = min( D (K-1) [I, K], D (K-1) [I, K] + D (K-1) [K, K] ), which becomes D (K) [I, K] = min( D (K-1) [I, K], D (K-1) [I, K] + 0 ) = D (K-1) [I, K] For this reason, we may ignore the following in our example.
The key step: D (K) [I, J] = min( D (K-1) [I, J], D (K-1) [I, K] + D (K-1) [K, J] ) K = 1, I = 1 Skip as I = K K = 1, I = 2 J = 1 Skip as J = K J = 3 D[2, 3] = 40. D[2, 1] + D[1, 3] = 2 + 3 = 5 D[2, 3] = 5 J = 4 D[2, 4] = 40. D[2, 1] + D[1, 4] = 2 + 40 = 42 D[2, 4] = 40 K = 1, I = 3 J = 1 Skip as J = K J = 2 D[3, 2] = 7. D[3, 1] + D[1, 2] = 40 + 40 = 80 D[2, 3] = 7 J = 4 D[3, 4] = 1. D[3, 1] + D[1, 4] = 40 + 40 = 80 D[2, 4] = 1 K = 1, I = 4 J = 1 Skip as J = K J = 2 D[4, 2] = 40. D[4, 1] + D[1, 2] = 6 + 40 = 46 D[4, 2] = 40 J = 3 D[4, 3] = 40. D[4, 1] + D[1, 3] = 6 + 3 = 9 D[4, 3] = 9
The key step: D (K) [I, J] = min( D (K-1) [I, J], D (K-1) [I, K] + D (K-1) [K, J] ) K = 3, I = 1 J = 2 D[1, 2] = 40. D[1, 3] + D[3, 2] = 3 + 7 = 10 D[1, 2] = 10 J = 3 Skip as J = K J = 4 D[1, 4] = 40. D[1, 3] + D[3, 4] = 3 + 1 = 4 D[1, 4] = 4 K = 3, I = 2 J = 1 D[2, 1] = 2. D[2, 3] + D[3, 1] = 5 + 9 = 14 D[2, 1] = 2 J = 3 Skip as J = K J = 4 D[2, 4] = 40. D[2, 3] + D[3, 4] = 5 + 1 = 6 D[2, 4] = 6 K = 3, I = 3 Skip as I = K K = 3, I = 4 J = 1 D[4, 1] = 6. D[4, 3] + D[3, 1] = 9 + 9 = 18 D[4, 1] = 6 J = 2 D[4, 2] = 40. D[4, 3] + D[3, 2] = 9 + 7 = 16 D[4, 2] = 16 J = 3 Skip as J = K
The key step: D (K) [I, J] = min( D (K-1) [I, J], D (K-1) [I, K] + D (K-1) [K, J] ) K = 4, I = 1 J = 2 D[1, 2] = 10. D[1, 4] + D[4, 2] = 4 + 16 = 20 D[1, 2] = 10 J = 3 D[1, 3] = 3. D[1, 4] + D[4, 3] = 4 + 9 = 13 D[1, 3] = 3 J = 4 Skip as J = K K = 4, I = 2 J = 1 D[2, 1] = 2. D[2, 4] + D[4, 1] = 6 + 6 = 12 D[2, 1] = 2 J = 3 D[2, 3] = 5. D[2, 4] + D[4, 3] = 6 + 9 = 15 D[2, 3] = 5 J = 4 Skip as J = K K = 4, I = 3 J = 1 D[3, 1] = 9. D[3, 4] + D[4, 1] = 1 + 6 = 7 D[3, 1] = 7 J = 2 D[3, 2] = 16. D[3, 4] + D[4, 2] = 1 + 16 = 17 D[3, 2] = 16 J = 4 Skip as J = K K = 4, I = 4 Skip as I = K