Home » DAA | Floyd-Warshall Algorithm

DAA | Floyd-Warshall Algorithm

by Online Tutorials Library

Floyd-Warshall Algorithm

Let the vertices of G be V = {1, 2……..n} and consider a subset {1, 2……..k} of vertices for some k. For any pair of vertices i, j ∈ V, considered all paths from i to j whose intermediate vertices are all drawn from {1, 2…….k}, and let p be a minimum weight path from amongst them. The Floyd-Warshall algorithm exploits a link between path p and shortest paths from i to j with all intermediate vertices in the set {1, 2…….k-1}. The link depends on whether or not k is an intermediate vertex of path p.

If k is not an intermediate vertex of path p, then all intermediate vertices of path p are in the set {1, 2……..k-1}. Thus, the shortest path from vertex i to vertex j with all intermediate vertices in the set {1, 2…….k-1} is also the shortest path i to j with all intermediate vertices in the set {1, 2…….k}.

If k is an intermediate vertex of path p, then we break p down into i → k → j.

Let dij(k) be the weight of the shortest path from vertex i to vertex j with all intermediate vertices in the set {1, 2…….k}.

A recursive definition is given by

Floyd-Warshall Algorithm

FLOYD - WARSHALL (W)   1. n ← rows [W].   2. D0 ← W   3. for k ← 1 to n   4. do for i ← 1 to n        5. do for j ← 1 to n        6. do dij(k) ← min (dij(k-1),dik(k-1)+dkj(k-1) )   7. return D(n)        

The strategy adopted by the Floyd-Warshall algorithm is Dynamic Programming. The running time of the Floyd-Warshall algorithm is determined by the triply nested for loops of lines 3-6. Each execution of line 6 takes O (1) time. The algorithm thus runs in time θ(n3 ).

Example: Apply Floyd-Warshall algorithm for constructing the shortest path. Show that matrices D(k) and π(k) computed by the Floyd-Warshall algorithm for the graph.

Floyd-Warshall Algorithm

Solution:

Floyd-Warshall Algorithm

Step (i) When k = 0

Floyd-Warshall Algorithm

Step (ii) When k =1

Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm

Step (iii) When k = 2

Floyd-Warshall Algorithm
Floyd-Warshall Algorithm

Step (iv) When k = 3

Floyd-Warshall Algorithm
Floyd-Warshall Algorithm

Step (v) When k = 4

Floyd-Warshall Algorithm
Floyd-Warshall Algorithm
Floyd-Warshall Algorithm

Step (vi) When k = 5

Floyd-Warshall Algorithm
Floyd-Warshall Algorithm

TRANSITIVE- CLOSURE (G)   1. n ← |V[G]|   2. for i ← 1 to n   3. do for j ← 1 to n   4. do if i = j or (i, j) ∈ E [G]   5. the Floyd-Warshall Algorithm← 1   6. else Floyd-Warshall Algorithm← 0   7. for k ← 1 to n   8. do for i ← 1 to n   9. do for j ← 1 to n   10. dod ij(k)Floyd-Warshall Algorithm   11. Return T(n).  

You may also like