Docsity

Warshall's and Floyd-Warshall Algorithms for Graphs: Transitive Closure and Shortest Paths, Study notes of Computer Architecture and Organization

Toggle sidebar
bg1
Chapter 8 – Dynamic Programming
Dynamic programming is one of the major strategies for designing algorithms. Put another
was, there are many algorithms written that use the approach of dynamic programming.
Dynamic programming is a method of solving problems where the solution to the problem is
based on the solution of a number of overlapping subproblems. Rather than push for a
precise but confusing definition of dynamic programming, we shall illustrate.
As in the discussion of any programming strategy, we shall use dynamic programming as a
solution to a problem even when a more efficient solution to that problem exists. We must
have some good examples in order to clarify our discussion.
One of the simplest problems solvable by dynamic programming has already been discussed
in this course: the computation of Fibonacci numbers. Fibonacci numbers are defined by the
recurrence relation
Fib(N) = Fib(N – 1) + Fib(N – 2), for N 2
with Fib(0) = 0 and Fib(1) = 1
It should be immediately obvious that this recurrence relation can be translated into a
recursive algorithm. It should also be obvious that such an algorithm is not very efficient in
that it repeatedly computes the values of certain numbers. The figure below shows the call
tree for computing the 5th Fibonacci number using the recursive algorithm. Note that Fib(0)
is called 3 times, Fib(1) is called 5 times, Fib(2) is called 3 times and Fib(3) is called twice.
The difficulty with this solution is that solutions to subproblems are discarded, even when
they can be reused in later computations.
Page 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Warshall's and Floyd-Warshall Algorithms for Graphs: Transitive Closure and Shortest Paths and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Chapter 8 – Dynamic Programming

Dynamic programming is one of the major strategies for designing algorithms. Put another was, there are many algorithms written that use the approach of dynamic programming. Dynamic programming is a method of solving problems where the solution to the problem is based on the solution of a number of overlapping subproblems. Rather than push for a precise but confusing definition of dynamic programming, we shall illustrate. As in the discussion of any programming strategy, we shall use dynamic programming as a solution to a problem even when a more efficient solution to that problem exists. We must have some good examples in order to clarify our discussion. One of the simplest problems solvable by dynamic programming has already been discussed in this course: the computation of Fibonacci numbers. Fibonacci numbers are defined by the recurrence relation Fib(N) = Fib(N – 1) + Fib(N – 2), for N  2 with Fib(0) = 0 and Fib(1) = 1 It should be immediately obvious that this recurrence relation can be translated into a recursive algorithm. It should also be obvious that such an algorithm is not very efficient in that it repeatedly computes the values of certain numbers. The figure below shows the call tree for computing the 5th^ Fibonacci number using the recursive algorithm. Note that Fib(0) is called 3 times, Fib(1) is called 5 times, Fib(2) is called 3 times and Fib(3) is called twice. The difficulty with this solution is that solutions to subproblems are discarded, even when they can be reused in later computations.

The dynamic programming solution to the computation of the Fibonacci number makes use of an array. Following the example of the textbook on page 82, we call the array F and assume that it is big enough – specifically, if we compute Fib(K), we assume that the array F has at least (K + 1) elements, indexed from 0 to K. Here is the algorithm. Algorithm Fib(K) // Computes the Kth Fibonacci Number // Uses an integer array F to hold the intermediate results. Long F[N] // Assume N is a constant, N > K. If (K < 1) Return 0 // Skip the dull stuff F[0] = 0 F[1] = 1 For I = 2 to K Do F[I] = F[I – 1] + F[I – 2] End Do Return F[K] As we noted earlier, there is another dynamic programming solution to this problem that is based on the fact that the algorithm must remember only the previous two results. This algorithm is shown below. Algorithm Fib(K) // Computes the Kth Fibonacci number by retaining the // results of the previous two computations. // If (K < 1) Return 0 // F1 = 0 F2 = 1 For J = 2 to K Do F0 = F1 // F0 is Fib(J – 2) F1 = F2 // F1 is Fib(J – 1) F2 = F0 + F1 // F2 is Fib(J) End Loop Return F Each of the two algorithms on this page are better than the recursive algorithm. The first algorithm is preferable if one wants to compute each of the first (K + 1) Fibonacci numbers. The second algorithm is preferable if one wants to compute only Fib(K).

To be specific, we give a few obvious definitions. R(1)IJ = 1 if and only if there is either a direct path from vertex I to J or if there is a path of length 2 that goes through only vertex 1. R(2)IJ = 1 if and only if there is either

  1. a direct path from vertex I to J, or
  2. a path of length 2 that goes through only vertex 1, or
  3. a path of length 2 that goes through only vertex 2, or
  4. a path of length 3 that goes through only vertices 1 and 2, in either order. So far all we have is a bunch of interesting definitions and observations. There is one fact that turns the above into a useable algorithm – the fact that matrix R(K)^ can be computed from only its predecessor matrix R(K-1). Consider the computation of element R(K)IJ, which is 1 if and only if there is a directed path from vertex I to vertex J, containing no vertex numbered higher than K. Now consider the matrix R(K-1), with its element R(K-1)IJ defined similarly. Suppose that R(K-1)IJ = 1. Then there is a path from vertex I to vertex J, containing no intermediate vertex numbered higher than (K – 1), certainly fulfilling the criteria that there be no intermediate vertex numbered higher than K. Thus we arrive at the first conclusion. If R(K-1)IJ == 1 then R(K)IJ = 1. If R(K-1)IJ = 0, there is another way to create a directed path from vertex I to vertex J, containing no vertex numbered higher than K.
  5. there is a path from vertex I to vertex K involving no vertex numbered higher than (K – 1), and
  6. there is a path from vertex K to vertex J involving no vertex numbered higher than (K – 1). Thus we have the second conclusion If R(K-1)IK == 1 and R(K-1)KJ = 1 then R(K)IJ = 1. Based on these two conclusions, we have the fundamental Boolean equation of the method. R(K)IJ = R(K-1)IJ OR (R(K-1)IK AND R(K-1)KJ) Note that by definition of a path in a graph, neither the start vertex nor the end vertex is a part of the path. Thus consider the paths from vertex I to vertex K containing no intermediate vertex numbered greater than K. Since vertex K is the end point of the path, it cannot appear as an intermediate vertex and the path, if present, contains no intermediate vertex numbered greater than (K – 1).

The situation discussed above may be illustrated by the figure below The pseudocode for the simple version of Warshall’s algorithm is found in the book and repeated here. Note that it constructs a sequence of (N + 1) matrices – R(0)^ through R(N). Algorithm Warshall (A[1..N, 1..N]) // Implements Warshall’s algorithm for computing the transitive // closure of a graph on N nodes with adjacency matrix A. // // This uses a sequence of (N + 1) matrices, each N-by-N, and // labeled R(0), R(1), …, R(N^ – 1), R(N). // // R(0)^ = A // Set matrix R(0)^ equal to matrix A. // For K = 1 to N Do For I = 1 to N Do For J = 1 to N Do R(K)[I,J] = R(K-1)[I,J] Or (R(K-1)[I,K] And R(K-1)[K,J]) End Do // J End Do // I End Do // K // Return R(N) The above algorithm has been written with the sequence of matrices in order to emphasize the dynamic-programming nature of the algorithm. In face, it is quite possible to avoid generating more than one matrix in this algorithm, as we shall see shortly. In order to facilitate this discussion, I shall rewrite the assignment statement of the algorithm above in an equivalent, but apparently less efficient, form. R(K)[I,J] = R(K-1)[I,J] // If R(K)[I,J] == 0, then do the computation. If !R(K)[I,J] Then R(K)[I,J] = R(K-1)[I,K] And R(K-1)[K,J]

The start matrix is R(0)^ =                 0 0 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 . K = 1 The loop for K = 1 generates R(1). The operative code is the following line. R(1)[I,J] = R(0)[I,J] If (0 == R(1)[I,J]) Then R(1)[I,J] = R(0)[I,K] And R(0)[K,J] But note that R(0)[1, J] = 0 for all values of J. The end result is that none of the zero values in the matrix R(0)^ are changed to 1, hence we have R(1)^ = R(0). We now have R(1)^ =                 0 0 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 K = 2 The loop for K = 2 generates R(2). The operative code is the following line. R(2)[I,J] = R(1)[I,J] If (0 == R(2)[I,J]) Then R(2)[I,J] = R(1)[I,2] And R(1)[2,J] I = 1, J = 1 R(1)[1, 1] = 0, so R(2)[1, 1] = R(1)[1, 2 ] And R(1)[ 2 , 1] = 1  0 = 0 I = 1, J = 2 R(1)[1, 2] = 1, so R(2)[1, 2] = 1 I = 1, J = 3 R(1)[1, 3] = 0, so R(2)[1, 3] = R(1)[1, 2 ] And R(1)[ 2 , 3] = 1  1 = 1 I = 1, J = 4 R(1)[1, 4] = 0, so R(2)[1, 3] = R(1)[1, 2 ] And R(1)[ 2 , 4] = 1  0 = 0 I = 1, J = 5 R(1)[1, 5] = 1, so R(2)[1, 5] = 1 I = 2, J = 1 R(1)[2, 1] = 0, so R(2)[2, 1] = R(1)[2, 2 ] And R(1)[ 2 , 1] = 0  0 = 0 I = 2, J = 2 R(1)[2, 2] = 0, so R(2)[2, 2] = R(1)[2, 2 ] And R(1)[ 2 , 2] = 0  0 = 0 I = 2, J = 3 R(1)[2, 3] = 1, so R(2)[2, 3] = 1 I = 2, J = 4 R(1)[2, 4] = 0, so R(2)[2, 4] = R(1)[2, 2 ] And R(1)[ 2 , 4] = 0  0 = 0 I = 2, J = 5 R(1)[2, 5] = 1, so R(2)[2, 5] = 1

I = 3, J = 1 R(1)[3, 1] = 0, so R(2)[3, 1] = R(1)[3, 2 ] And R(1)[ 2 , 1] = 1  0 = 0 I = 3, J = 2 R(1)[3, 2] = 1, so R(2)[3, 2] = 1 I = 3, J = 3 R(1)[3, 3] = 0, so R(2)[3, 3] = R(1)[3, 2 ] And R(1)[ 2 , 3] = 1  1 = 1 I = 3, J = 4 R(1)[3, 4] = 1, so R(2)[3, 4] = 1 I = 3, J = 5 R(1)[3, 5] = 0, so R(2)[3, 5] = R(1)[3, 2 ] And R(1)[ 2 , 5] = 1  1 = 1 I = 4, J = 1 R(1)[4, 1] = 0, so R(2)[4, 1] = R(1)[4, 2 ] And R(1)[ 2 , 1] = 0  0 = 0 I = 4, J = 2 R(1)[4, 2] = 0, so R(2)[4, 2] = R(1)[4, 2 ] And R(1)[ 2 , 2] = 0  0 = 0 I = 4, J = 3 R(1)[4, 3] = 0, so R(2)[4, 3] = R(1)[4, 2 ] And R(1)[ 2 , 3] = 0  1 = 0 I = 4, J = 4 R(1)[4, 4] = 0, so R(2)[4, 4] = R(1)[4, 2 ] And R(1)[ 2 , 4] = 0  0 = 0 I = 4, J = 5 R(1)[4, 5] = 1, so R(2)[4, 5] = 1 I = 5, J = 1 R(1)[5, 1] = 0, so R(2)[5, 1] = R(1)[5, 2 ] And R(1)[ 2 , 1] = 0  0 = 0 I = 5, J = 2 R(1)[5, 2] = 0, so R(2)[5, 2] = R(1)[5, 2 ] And R(1)[ 2 , 2] = 0  0 = 0 I = 5, J = 3 R(1)[5, 3] = 0, so R(2)[5, 3] = R(1)[5, 2 ] And R(1)[ 2 , 3] = 0  1 = 0 I = 5, J = 4 R(1)[5, 4] = 1, so R(2)[5, 4] = 1 I = 5, J = 5 R(1)[5, 5] = 0, so R(2)[5, 5] = R(1)[5, 2 ] And R(1)[ 2 , 5] = 0  1 = 0 We now have R(2)^ =                 0 0 0 1 0 0 0 0 0 1 0 1 1 1 1 0 0 1 0 1 0 1 1 0 1 K = 3 The loop for K = 3 generates R(3). The operative code is the following line. R(3)[I,J] = R(2)[I,J] If (0 == R(3)[I,J]) Then R(3)[I,J] = R(2)[I,3] And R(2)[3,J] I = 1, J = 1 R(2)[1, 1] = 0, so R(3)[1, 1] = R(2)[1, 3 ] And R(2)[ 3 , 1] = 1  0 = 0 I = 1, J = 2 R(2)[1, 2] = 1, so R(3)[1, 2] = 1 I = 1, J = 3 R(2)[1, 3] = 1, so R(3)[1, 3] = 1 I = 1, J = 4 R(2)[1, 4] = 0, so R(3)[1, 4] = R(2)[1, 3 ] And R(2)[ 3 , 4] = 1  1 = 1 I = 1, J = 5 R(2)[1, 5] = 1, so R(3)[1, 5] = 1

I = 2, J = 2 R(3)[2, 2] = 1, so R(4)[2, 2] = 1 I = 2, J = 3 R(3)[2, 3] = 1, so R(4)[2, 3] = 1 I = 2, J = 3 R(3)[2, 4] = 1, so R(4)[2, 4] = 1 I = 2, J = 5 R(3)[2, 5] = 1, so R(4)[2, 5] = 1 I = 3, J = 1 R(3)[3, 1] = 0, so R(4)[3, 1] = R(3)[3, 4 ] And R(3)[ 4 , 1] = 1  0 = 0 I = 3, J = 2 R(3)[3, 2] = 1, so R(4)[3, 2] = 1 I = 3, J = 3 R(3)[3, 3] = 1, so R(4)[3, 3] = 1 I = 3, J = 3 R(3)[3, 4] = 1, so R(4)[3, 4] = 1 I = 3, J = 5 R(3)[3, 5] = 1, so R(4)[3, 5] = 1 I = 4, J = 1 R(3)[4, 1] = 0, so R(4)[4, 1] = R(3)[4, 4 ] And R(3)[ 4 , 1] = 0  0 = 0 I = 4, J = 2 R(3)[4, 2] = 0, so R(4)[4, 2] = R(3)[4, 4 ] And R(3)[ 4 , 2] = 0  0 = 0 I = 4, J = 3 R(3)[4, 3] = 0, so R(4)[4, 3] = R(3)[4, 4 ] And R(3)[ 4 , 3] = 0  0 = 0 I = 4, J = 4 R(3)[4, 4] = 0, so R(4)[4, 4] = R(3)[4, 4 ] And R(3)[ 4 , 4] = 0  0 = 0 I = 4, J = 5 R(3)[4, 5] = 1, so R(4)[4, 5] = 1 I = 5, J = 1 R(3)[5, 1] = 0, so R(4)[5, 1] = R(3)[5, 4 ] And R(3)[ 4 , 1] = 1  0 = 0 I = 5, J = 2 R(3)[5, 2] = 0, so R(4)[5, 2] = R(3)[5, 4 ] And R(3)[ 4 , 2] = 1  0 = 0 I = 5, J = 3 R(3)[5, 3] = 0, so R(4)[5, 3] = R(3)[5, 4 ] And R(3)[ 4 , 3] = 1  0 = 0 I = 5, J = 4 R(3)[5, 4] = 1, so R(4)[5, 4] = 1 I = 5, J = 5 R(3)[5, 3] = 0, so R(4)[5, 3] = R(3)[5, 4 ] And R(3)[ 4 , 5] = 1  1 = 1 We now have R(4)^ =                 0 0 0 1 1 0 0 0 0 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 K = 5 The loop for K = 5 generates R(5). The operative code is the following line. R(5)[I,J] = R(4)[I,J] If (0 == R(5)[I,J]) Then R(5)[I,J] = R(4)[I,5] And R(4)[5,J]

I = 1, J = 1 R(4)[1, 1] = 0, so R(5)[1, 1] = R(4)[1, 5 ] And R(4)[ 5 , 1] = 1  0 = 0 I = 1, J = 2 R(4)[1, 2] = 1, so R(5)[1, 2] = 1 I = 1, J = 3 R(4)[1, 3] = 1, so R(5)[1, 3] = 1 I = 1, J = 3 R(4)[1, 4] = 1, so R(5)[1, 4] = 1 I = 1, J = 5 R(4)[1, 5] = 1, so R(5)[1, 5] = 1 I = 2, J = 1 R(4)[2, 1] = 0, so R(5)[2, 1] = R(4)[2, 5 ] And R(4)[ 5 , 1] = 1  0 = 0 I = 2, J = 2 R(4)[2, 2] = 1, so R(5)[2, 2] = 1 I = 2, J = 3 R(4)[2, 3] = 1, so R(5)[2, 3] = 1 I = 2, J = 3 R(4)[2, 4] = 1, so R(5)[2, 4] = 1 I = 2, J = 5 R(4)[2, 5] = 1, so R(5)[2, 5] = 1 I = 3, J = 1 R(4)[3, 1] = 0, so R(5)[3, 1] = R(4)[3, 5 ] And R(4)[ 5 , 1] = 1  0 = 0 I = 3, J = 2 R(4)[3, 2] = 1, so R(5)[3, 2] = 1 I = 3, J = 3 R(4)[3, 3] = 1, so R(5)[3, 3] = 1 I = 3, J = 3 R(4)[3, 4] = 1, so R(5)[3, 4] = 1 I = 3, J = 5 R(4)[3, 5] = 1, so R(5)[3, 5] = 1 I = 4, J = 1 R(4)[4, 1] = 0, so R(5)[4, 1] = R(4)[4, 5 ] And R(4)[ 5 , 1] = 1  0 = 0 I = 4, J = 2 R(4)[4, 2] = 0, so R(5)[4, 2] = R(4)[4, 5 ] And R(4)[ 5 , 2] = 1  0 = 0 I = 4, J = 3 R(4)[4, 3] = 0, so R(5)[4, 3] = R(4)[4, 5 ] And R(4)[ 5 , 3] = 1  0 = 0 I = 4, J = 4 R(4)[4, 4] = 0, so R(5)[4, 4] = R(4)[4, 5 ] And R(4)[ 5 , 4] = 1  1 = 1 I = 4, J = 5 R(4)[4, 5] = 1, so R(5)[4, 5] = 1 I = 5, J = 1 R(4)[5, 1] = 0, so R(5)[5, 1] = R(4)[5, 5 ] And R(4)[ 5 , 1] = 1  0 = 0 I = 5, J = 2 R(4)[5, 2] = 0, so R(5)[5, 2] = R(4)[5, 5 ] And R(4)[ 5 , 2] = 1  0 = 0 I = 5, J = 3 R(4)[5, 3] = 0, so R(5)[5, 3] = R(4)[5, 5 ] And R(4)[ 5 , 3] = 1  0 = 0 I = 5, J = 4 R(4)[5, 4] = 1, so R(5)[5, 4] = 1 I = 5, J = 5 R(4)[5, 5] = 1, so R(5)[5, 5] = 1 We now have R(5)^ =                 0 0 0 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 The above matrix, R(5), represents the transitive closure of the directed graph. What can we say about the graph? Vertex 1 there is no path to vertex 1 from any vertex. Vertices 2 and 3 there is a path to these vertices from vertices 1, 2, and 3 Vertices 4 and 5 there is a path to these vertices from every vertex.

The textbook takes the infinite edge approach and represents the matrix of edge weights (the generalized adjacency matrix) as. W =                    6 0 7 0 1 2 0 0 3 The problem here is how to represent infinity in the computer program. If we go with the IEEE standard floating point, we can represent infinity as follows. Single Precision: 0x7F80 0000 Double Precision: 0x7FF0 0000 0000 0000 One should exercise some caution in using this approach because there is no guarantee that the CPU hardware will support this part of the standard. Also, can one say X = ? Another approach is to select a real number at the upper limit of the range of the data type being used to represent the weights. For the two more common data types, we might say For 16-bit integers Infinity = 32767 For single-precision floating point Infinity = 3.5 1038. Again, this approach also might present some difficulties. Algorithms that make use of infinity as a working part can lead to unexpected difficulties when actually implemented. These notes will follow the standard presentation of the algorithm and then suggest a more reasonable way to handle the infinity problem. Before we investigate the algorithm, we should take note of a situation that the algorithm was not designed to handle. This is a graph, such as one at right, that has a cycle of negative total weight. Consider the cycle A  C  B  A, with total weight of 2 + 3 – 6 = – 1. Of course, one should be suspicious of any graph having edges with negative weight. The approach used in Floyd’s algorithm is so similar to that used in Warshall’s algorithm that some textbooks teach the two algorithms under the name Floyd-Warshall. The algorithm computes the distance matrix of a weighted graph with N vertices by using a sequence of (N + 1) matrices D(0), D(1), …. D(N), with D(K)[I, J] representing the shortest path from vertex I to vertex J with no vertex numbered higher than K being included in the path.

As in Warshall’s algorithm, we consider two possible paths between vertices I and J

  1. a path from I to J including no vertex numbered higher than (K – 1), and
  2. a path from I to K including no vertex numbered higher than (K – 1), followed by vertex K, followed by a similar path from vertex K to vertex J. Inspection of the above figure leads immediately to the algorithm for computing the Kth partial distance. D(K)[I, J] = min( D(K-1)[I, J], D(K-1)[I, K] + D(K-1)[K, J] ), for 1  K  N with D(0)[I, J] = W[I, J]. Note also that vertex K cannot be a part of the path ending on itself, so that D(K)[I, K] = D(K-1)[I, K] and D(K)[K, J] = D(K-1)[K, J]. As in Warshall’s algorithm, we use the last fact to avoid generating a sequence of matrices, and to write the next matrix in the sequence over its predecessor. The algorithm is below. Algorithm Floyd ( W[1..N, 1..N] ) // Implements Floyd’s algorithm for shortest paths // Input: The weight matrix of the graph // Output: The distance matrix of the graph. // D = W // For K = 1 to N Do For I = 1 to N Do For J = 1 to N Do X = D[I, K] + D[K, J] If X < D[I, J] Then D[I, J] = X End Do // J End Do // I End Do // K

I = 2, J = 1 D(0)[2, 1] = 2.

X = D(0)[2, 1 ] + D(0)[ 1 , 1] = 2 + 0 = 2, so no change. I = 2, J = 2 D(0)[2, 2] = 0, so no change. I = 2, J = 3 D(0)[2, 3] = 40. X = D(0)[2, 1 ] + D(0)[ 1 , 3] = 2 + 3 = 5, a new value. I = 2, J = 4 D(0)[2, 4] = 40. X = D(0)[2, 1 ] + D(0)[ 1 , 4] = 2 + 40 = 42, no change. Here we note one advantage of using very large finite numbers in this algorithm. The sum 2 + 40 has meaning and is equal to 42, while the sum 2 +  has no meaning. I = 3, J = 1 D(0)[3, 1] = 40. X = D(0)[3, 1 ] + D(0)[ 1 , 1] = 40 + 0 = 40, so no change. I = 3, J = 2 D(0)[3, 2] = 7. X = D(0)[3, 1 ] + D(0)[ 1 , 2] = 40 + 7 = 47, so no change. I = 3, J = 3 D(0)[3, 3] = 0, so no change. I = 3, J = 4 D(0)[3, 4] = 1. X = D(0)[3, 1 ] + D(0)[ 1 , 4] = 40 + 1 = 41, so no change. We could have examined the third row of the matrix and shown that there would be no change in any element by noting that every computation of D(1)[3, J] involved adding a distance to D(0)[3, 1], which we have set to our representation of infinity. I = 4, J = 1 D(0)[4, 1] = 6. X = D(0)[4, 1 ] + D(0)[ 1 , 1] = 6 + 0 = 6, so no change. I = 4, J = 2 D(0)[4, 2] = 40. X = D(0)[4, 1 ] + D(0)[ 1 , 2] = 6 + 40 = 46, so no change. I = 4, J = 3 D(0)[4, 2] = 40. X = D(0)[4, 1 ] + D(0)[ 1 , 3] = 6 + 3 = 9, a new value. I = 4, J = 4 D(0)[4, 4] = 0, so no change. At this point, we have the following intermediate result. D(1)^ =             6 40 9 0 40 7 0 1 2 0 5 40 0 40 3 40

Before continuing the example, we present a revision of the algorithm that we shall adopt during the solution of this example. Algorithm Floyd ( W[1..N, 1..N] ) // Implements Floyd’s algorithm for shortest paths // Input: The weight matrix of the graph // Output: The distance matrix of the graph. // D = W // Copy the W matrix, so we don’t change it. // For K = 1 to N Do For I = 1 to N Do If (I  K) And !(D[I, K] == ) Then For J = 1 to N Do X = D[I, K] + D[K, J] If X < D[I, J] Then D[I, J] = X End Do // J End If End Do // I End Do // K K = 2 This loop generates the matrix D(2). The operative code is the following. X = D(1)[I, 2 ] + D(1)[ 2 , J] If X < D(1)[I, J] Then D(2)[I, J] = X, Else D(2)[I, J] = D(1)[I, J] Row I = 1. D(1)[I, K] = D(1)[1, 2] = 40, so no change on this row. Row I = 2. I = K = 2, so no change on this row. I = 3, J = 1 D(1)[3, 1] = 40. X = D(1)[3, 2 ] + D(1)[ 2 , 1] = 7 + 2 = 9, a new value. I = 3, J = 2 D(1)[3, 2] = 7. X = D(1)[3, 2 ] + D(1)[ 2 , 2] = 7 + 0 = 7, so no change.. I = 3, J = 3 D(1)[3, 3] = 0, so no change. I = 3, J = 4 D(1)[3, 4] = 1. X = D(1)[3, 2 ] + D(1)[ 2 , 4] = 7 + 1 = 8, so no change. Row I = 4. D(1)[4, K] = D(1)[4, 2] = 40, so no change on this row.

At this point, we have the following intermediate result. D(3)^ =             6 16 9 0 9 7 0 1 2 0 5 6 0 10 3 4 K = 4 This loop generates the matrix D(3). The operative code is the following. X = D(3)[I, 4 ] + D(3)[ 4 , J] If X < D(3)[I, J] Then D(4)[I, J] = X, Else D(4)[I, J] = D(3)[I, J] I = 1, J = 1 D(3)[1, 1] = 0, so no change. I = 1, J = 2 D(3)[1, 2] = 10. X = D(3)[1, 4 ] + D(2)[ 4 , 2] = 4 + 16 = 20, so no change. I = 1, J = 3 D(3)[1, 3] = 3. X = D(3)[1, 4 ] + D(2)[ 4 , 3] = 4 + 9 = 13, so no change. I = 1, J = 4 D(3)[1, 4] = 4. X = D(3)[1, 4 ] + D(2)[ 4 , 4] = 4 + 0 = 4, so no change. I = 2, J = 1 D(3)[2, 1] = 2. X = D(3)[2, 4 ] + D(2)[ 4 , 1] = 6 + 6 = 12, so no change. I = 2, J = 2 D(3)[2, 2] = 0, so no change. I = 2, J = 3 D(3)[2, 3] = 5. X = D(3)[2, 4 ] + D(2)[ 4 , 3] = 6 + 9 = 15, so no change. I = 2, J = 4 D(3)[2, 4] = 6. X = D(3)[2, 4 ] + D(2)[ 4 , 4] = 6 + 0 = 6, so no change. I = 3, J = 1 D(3)[3, 1] = 9. X = D(3)[3, 4 ] + D(2)[ 4 , 1] = 1 + 6 = 7, a new value. I = 3, J = 2 D(3)[3, 2] = 7. X = D(3)[3, 4 ] + D(2)[ 4 , 2] = 1 + 16 = 17, so no change. I = 3, J = 3 D(3)[3, 3] = 0, so no change. I = 3, J = 4 D(3)[3, 4] = 1. X = D(3)[3, 4 ] + D(2)[ 4 , 4] = 1 + 0 = 1, so no change. Row 4: I = K = 4, so no change.

The final result is thus D(4)^ =             6 16 9 0 7 7 0 1 2 0 5 6 0 10 3 4 Unweighted Graphs If we consider a simple graph as a weighted graph in which all edges have weight 1, we can easily apply Floyd’s algorithm. The graph at left is a simple undirected graph in which each edge has been given a weight of 1. We can apply Floyd’s algorithm. The weight matrix for this graph is D(0)^ = W =             1 40 40 0 40 1 0 1 1 0 40 40 0 40 1 40 The distance matrix finally computed is D(4)^ =             1 2 2 0 2 1 0 1 1 0 2 3 0 2 1 2 Each weight in the distance matrix is the number of edges in the path from the source vertex to the destination vertex.