Presentation is loading. Please wait.

Presentation is loading. Please wait.

Floyd-Warshall Algorithm

Similar presentations


Presentation on theme: "Floyd-Warshall Algorithm"— Presentation transcript:

1 Floyd-Warshall Algorithm
Shortest Path Floyd-Warshall Algorithm

2 Shortest Paths Problems
1 v t 5 4 9 8 13 u 4 y 3 x 2 10 1 1 2 w z 6 Given a weighted directed graph, <u,v,t,x,z> is a path of weight 29 from u to z. <u,v,w,x,y,z> is another path from u to z; it has weight 16 and is the shortest path from u to z.

3 Shortest Path Problem: given a weighted directed graph G, find the minimum-weight path from a given source vertex s to another vertex v “Shortest-path” = minimum weight Weight of path is sum of edges E.g., a road map: what is the shortest path from Chapel Hill to Charlottesville?

4 More Shortest Paths Variants
All weights are non-negative. Weights may be negative, but no negative cycles (A cycle is a path from a vertex to itself.) Negative cycles allowed. Algorithm reports “∞” if there is a negative cycle on path from source to destination (2) and (3) seem to be harder than (1). v v 5 u v w 5 u v w 5 8 -3 3 -6 -1 u -3 2 u 3 2 6 w -6 w

5 Floyd-Warshall Algorithm
Dynamic programming solution that solves the problem in O(|V| 3) time. Negative weight edges may be present Assumption:- No negative weight cycles

6 Intermediate Vertices
Without loss of generality, we will assume that V={1,2,…,n}, i.e., that the vertices of the graph are numbered from 1 to n. Given a path p=(1,2,…,m) in the graph, we will call the vertices k with k in {2,…,m-1} the intermediate vertices of p. 6

7 Structure of the Shortest Path
Let dij(k) denote the length of the shortest path from i to j such that all intermediate vertices are contained in the set {1,…,k}. Thus, (i, j) = dij(n). Also, dij(0) = aij . 7

8 Remark 1 A shortest path does not contain any vertex twice, as this would imply that the path contains a cycle. By assumption, cycles in the graph have a positive weight, so removing the cycle would result in a shorter path, which is impossible.

9 Remark 2 Consider a shortest path p from i to j such that the intermediate vertices are from the set {1,…,k}. If the vertex k is not an intermediate vertex on p, then dij(k) = dij(k-1) If the vertex k is an intermediate vertex on p, then dij(k) = dik(k-1) + dkj(k-1) Interestingly, in either case, the subpaths contain merely nodes from {1,…,k-1}.

10 Remark 2 Therefore, we can conclude that dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1)} intermediate vertices in {1, 2, …, k}

11 Recursive Formulation
If we do not use intermediate nodes, i.e., when k=0, then dij(0) = wij If k>0, then dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1)} 11

12 The Floyd-Warshall Algorithm
Floyd-Warshall(W) n = # of rows of W; D(0) = W; for k = 1 to n do for i = 1 to n do for j = 1 to n do dij(k) = min{dij(k-1) , dik(k-1) + dkj(k-1)}; return D(n); 12

13 Time and Space Requirements
The running time is obviously O(n3). However, in this version, the space requirements are high. One can reduce the space from O(n3) to O(n2) by using a single array d. 13

14 Example 8 5 3 ∞ 2 At each step k dij satisfies the criteria
1 2 3 5 D(0) 1 3 8 5 3 2 1 8 2 3 2 2 3 At each step k dij satisfies the criteria Path begins at i & ends at j Intermediate vertices on the path come from the set {1, 2, …, k}

15 Example 1 2 3 5 D(0) 1 3 8 5 3 2 1 8 2 3 2 2 3 Step 1:- Consider all paths which contain 1 as a intermediate vertex 2,1,3 is the only path dij(1) = min{dij(0) , dik(0) + dkj(0)} d23(1) = min{d23(0) , d21(0) + d13(0)};

16 Example 1 2 3 5 D(1) 1 3 8 5 3 2 1 8 2 3 2 2 3 Step 1:- Consider all paths which contain 1 as a intermediate vertex 2,1,3 is the only path dij(1) = min{dij(0) , dik(0) + dkj(0)} d23(1) = min{d23(0) , d21(0) + d13(0)} = 8

17 Example 1 2 3 5 D(1) 1 3 8 5 3 2 1 8 2 3 2 2 3 Step 2:- Consider all paths which contain 2 as a intermediate vertex 3,2,1 is the only path dij(2) = min{dij(1) , dik(1) + dkj(1)} d31(2) = min{d31(1) , d32(1) + d21(1)};

18 Example 1 2 3 5 D(2) 1 3 8 5 3 2 1 8 2 3 2 2 3 Step 2:- Consider all paths which contain 2 as a intermediate vertex 3,2,1 is the only path dij(2) = min{dij(1) , dik(1) + dkj(1)} d31(2) = min{d31(1) , d32(1) + d21(1)} = 5

19 Example 1 2 3 5 D(2) 1 3 8 5 3 2 1 8 2 3 2 2 3 Step 3:- Consider all paths which contain 3 as a intermediate vertex 1,3,2 is the only path dij(3) = min{dij(2) , dik(2) + dkj(2)} d12(3) = min{d12(2) , d13(2) + d32(2)};

20 Example 1 2 3 5 D(3) 1 3 7 5 3 8 2 1 8 2 3 2 2 3 Step 3:- Consider all paths which contain 3 as a intermediate vertex 1,3,2 is the only path dij(3) = min{dij(2) , dik(2) + dkj(2)} d12(3) = min{d12(2) , d13(2) + d32(2)} = 7

21 Example 7 5 3 8 2 We have obtained the value of an optimal solution.
1 2 3 5 D(3) 1 3 7 5 3 8 2 1 8 2 3 2 2 3 Final Result We have obtained the value of an optimal solution. To obtain the actual solution i.e. the shortest path between each pair of vertices, use the predecessor matrix

22 Predecessor Matrix pij(0) = NIL if i = j or wij = ∞
i if i ≠ j & wij < ∞ pij(k) = pij(k-1) if dij(k-1) ≤ dik(k-1) + dkj(k-1) pkj(k-1) if dij(k-1) > dik(k-1) + dkj(k-1)

23 Example 1 2 3 5 D(1) 1 3 8 5 3 2 1 8 2 3 2 2 3 1 2 3 1 2 3 P(0) P(1) N 1 2 3 1 N 1 2 3 1 2 2 3 3

24 Example 1 2 3 5 D(2) 1 3 8 5 3 2 1 8 2 3 2 2 3 1 2 3 P(2) N 1 2 3 1 2 3

25 Example 1 2 3 5 D(3) 1 3 7 5 3 8 2 1 8 2 3 2 2 3 1 2 3 P(3) N 3 1 2 Use this final predecessor matrix to obtain the optimal result 1 2 3

26 Printing the Shortest Path between vertices i & j
print( P, i, j) { if i = j then print i else if pij = NIL then print “No Path” else { print(P, i, pij) print j }

27 Floyd-Warshall Algorithm & Transitive Closure of a Graph

28 Transitive closure of a directed graph
The transitive closure of G is defined as the graph G* = (V, E*), where E* = { (i, j) : there is a path from vertex i to j in G}

29 Transitive closure of a directed graph
1 if there exists a path from i to j, 0 otherwise. Compute tij = IDEA: Use Floyd-Warshall, but with (Λ, V) instead of (min, +): 0 if i  j and (i,j) not in E, 1 if i = j or (i,j) in E. tij(0) = tij(k) = tij(k–1) V (tik(k–1) Λ tkj(k–1)). Time = (n3).

30 Example 1 2 3 4 1 2 3 4 T(0) T(1) 1 2 1 1 1 1 2 2 3 3 4 3 4 4 T(2) 1 2 3 4 T(3) 1 2 3 T(4) 4 1 2 3 4 1 1 1 1 1 1 2 2 2 3 3 3 4 4 4


Download ppt "Floyd-Warshall Algorithm"

Similar presentations


Ads by Google