Floyd-Warshall Algorithm Chandler Burfield February 20, 2013 Chandler Burfield Floyd-Warshall February 20, 2013 1 / 15 All-Pairs Shortest Paths Problem To find the shortest path between all vertices v ∈ V for a weighted graph G = (V , E ). Chandler Burfield Floyd-Warshall February 20, 2013 2 / 15 Dynamic-Programming How to Develop a Dynamic-Programming Algorithm 1. Characterize the structure of an optimal solution. 2. Recursively define the value of an optimal solution. 3. Compute the value of an optimal solution in a bottom-up manner. Chandler Burfield Floyd-Warshall February 20, 2013 3 / 15 ! Intermediate Vertices An intermediate vertex of a simple path p = {v1 , v2 , ..., vl } is any vertex other than v1 or vl i.e. a vertex from the set {v2 , ..., vl−1 }. Intermediate!Vertices!of!Path! V1 # ! Chandler Burfield ! ! Floyd-Warshall Vl # February 20, 2013 4 / 15 Weight Matrix wij The weight of an edge between vertex i and vertex j in graph G = (V , E ), where if i = j 0 the weight of a directed edge (i, j) if i 6= j and (i, j) ∈ E wij = ∞ if i 6= j and (i, j) ∈ /E W An n x n matrix representing the edge weights of an n-vertex graph, where W = (wij ). Chandler Burfield Floyd-Warshall February 20, 2013 5 / 15 Path Matrix (k) dij The weight of the shortest path from vertex i to vertex j for which all intermediate vertices are in the set {1, 2, ..., k}. D (k) An n x n matrix representing the path distances between vertices in a (k) directed n-vertex graph, where D (k) = (dij ). Chandler Burfield Floyd-Warshall February 20, 2013 6 / 15 Observations A shortest path does not contain the same vertex more than once. For a shortest path from i to j such that any intermediate vertices on the path are chosen from the set {1, 2, ..., k}, there are two possibilities: 1. k is not a vertex on the path, so the shortest such path has length dijk−1 2. k is a vertex on the path, so the shortest such path is dikk−1 +dkjk−1 (k) So we see that we can recursively define dij as ( wij if k = 0 (k) dij = (k−1) (k−1) (k−1) min(dij , dik + dkj ) if k ≥ 1 Chandler Burfield Floyd-Warshall February 20, 2013 7 / 15 Predecessor Matrix (k) πij The predecessor of vertex j on a shortest path from vertex i with all intermediate vertices in the set {1, 2, ..., k}. Where NIL if i = j or wij = ∞ (0) πij = i if i 6= j and wij < ∞ And ( (k) πij = (k−1) if dij (k−1) if dij πij πkj (k−1) ≤ dik (k−1) + dkj (k−1) > dik (k−1) (k−1) + dkj (k−1) Π(k) (k) The predecessor matrix, where Π(k) = (πij ). Chandler Burfield Floyd-Warshall February 20, 2013 8 / 15 Algorithm Floyd-Warshall(W) n = W .rows D (0) = W for k = 1 to n (k) let D (k) = (dij ) be a new matrix for i = 1 to n for j = 1 to n (k) (k−1) (k−1) (k−1) dij = min(dij , dik + dkj ) (n) return D Chandler Burfield Floyd-Warshall February 20, 2013 9 / 15 Analysis of Algorithm Runtime Floyd-Warshall(W) n = W .rows D (0) = W for k = 1 to n (k) let D (k) = (dij ) be a new matrix for i = 1 to n for j = 1 to n (k) (k−1) (k−1) (k−1) dij = min(dij , dik + dkj ) (n) return D Chandler Burfield Floyd-Warshall Θ(n3 ) Space Θ(n3 ) here, but if we reuse space this can be done in Θ(n2 ). February 20, 2013 10 / 15 Analysis of Improved Algorithm Floyd-Warshall(W) n = W .rows D=W Π initialization for k = 1 to n for i = 1 to n for j = 1 to n if dij > dik + dkj then dij = dik + dkj πij = πkj return D Chandler Burfield Floyd-Warshall Analysis The shortest path can be constructed, not just the lengths of the paths. Runtime: Θ(n3 ). Space: Θ(n2 ). February 20, 2013 11 / 15 Example Floyd-Warshall(W) n = W .rows D=W Π initialization for k = 1 to n for i = 1 to n for j = 1 to n if dij > dik + dkj then dij = dik + dkj πij = πkj return D Chandler Burfield Floyd-Warshall February 20, 2013 12 / 15 Proof of Correctness Inductive Hypothesis Suppose that prior to the kth iteration it holds that for i, j ∈ V , dij contains the length of the shortest path Q from i to j in G containing only vertices in the set {1, 2, ..., k − 1}, and πij contains the immediate predecessor of j on path Q. Chandler Burfield Floyd-Warshall February 20, 2013 13 / 15 Applications Detecting the Presence of a Negative Cycle Transitive Closure of a Directed Graph Chandler Burfield Floyd-Warshall February 20, 2013 14 / 15 Other All-Pairs Shortest Paths Algorithms Dynamic Programming Approach Based on Matrix Multiplication Johnson’s Algorithm for Sparse Graphs Chandler Burfield Floyd-Warshall February 20, 2013 15 / 15