Floyd-Warshall Algorithm

Slides:



Advertisements
Similar presentations
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 19 Prof. Erik Demaine.
Advertisements

* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
Advanced Algorithm Design and Analysis (Lecture 7) SW5 fall 2004 Simonas Šaltenis E1-215b
1 Chapter 26 All-Pairs Shortest Paths Problem definition Shortest paths and matrix multiplication The Floyd-Warshall algorithm.
Shortest Paths Algorithm Design and Analysis Week 7 Bibliography: [CLRS] – chap 24 [CLRS] – chap 25.
Chapter 25: All-Pairs Shortest-Paths
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Dynamic Programming Reading Material: Chapter 7..
1 8a-ShortestPathsMore Shortest Paths in a Graph (cont’d) Fundamental Algorithms.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 16 All shortest paths algorithms Properties of all shortest paths Simple algorithm:
Midterm 3 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Algorithms All pairs shortest path
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
More Dynamic Programming Floyd-Warshall Algorithm.
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
Dynamic Programming Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by or formulated.
Week 4 Single Source Shortest Paths All Pairs Shortest Path Problem.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
All-pairs Shortest Paths. p2. The structure of a shortest path: All subpaths of a shortest path are shortest paths. p : a shortest path from vertex i.
The all-pairs shortest path problem (APSP) input: a directed graph G = (V, E) with edge weights goal: find a minimum weight (shortest) path between every.
All-Pairs Shortest Paths
Graph Theory. undirected graph node: a, b, c, d, e, f edge: (a, b), (a, c), (b, c), (b, e), (c, d), (c, f), (d, e), (d, f), (e, f) subgraph.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
All Pairs Shortest Path Algorithms Aditya Sehgal Amlan Bhattacharya.
All-pairs Shortest paths Transitive Closure
Shortest Paths.
CSC317 Shortest path algorithms
Graph Algorithms Minimum Spanning Tree (Chap 23)
Algorithm Analysis Fall 2017 CS 4306/03
CS4234 Optimiz(s)ation Algorithms
All-Pairs SPs on DG Run Dijkstra;s algorithm for each vertex or
Algorithms and Data Structures Lecture XIII
CS Introduction to Data Structures
CS330 Discussion 6.
All-Pairs Shortest Paths (26.0/25)
Chapter 25: All-Pairs Shortest Paths
Data Structures and Algorithms
Warshall’s and Floyd’sAlgorithm
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Shortest Paths.
Lecture 7 Shortest Path Shortest-path problems
All-pairs Shortest Path
All-pairs shortest path
CS200: Algorithm Analysis
Analysis and design of algorithm
Unit 4: Dynamic Programming
Chapter 8 Dynamic Programming
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Dynamic Programming Characterize the structure (problem state) of optimal solution Recursively define the value of optimal solution Compute the value of.
Shortest Path Algorithms
Lecture 13 Algorithm Analysis
Dynamic Programming.
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
All pairs shortest path problem
Near-neighbor or Mesh Based Paradigm
Presented by-Kapil Kumar Cse-iii rd year
Advanced Algorithms Analysis and Design
Dynamic Programming.
Shortest Paths.
Negative-Weight edges:
All Pairs Shortest Path Examples While the illustrations which follow only show solutions from vertex A (or 1) for simplicity, students should note that.
Directed Graphs (Part II)
All-Pairs Shortest Paths
COSC 3101A - Design and Analysis of Algorithms 12
Presentation transcript:

Floyd-Warshall Algorithm Shortest Path Floyd-Warshall Algorithm

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.

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?

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

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

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

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

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.

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}.

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}

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

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

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

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}

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)};

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

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)};

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

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)};

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

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

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)

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

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

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

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 }

Floyd-Warshall Algorithm & Transitive Closure of a Graph

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}

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).

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