Beruflich Dokumente
Kultur Dokumente
Chandler Bureld
Chandler Bureld
Floyd-Warshall
1 / 15
Problem
To nd the shortest path between all vertices v V for a weighted graph G = (V , E ).
Chandler Bureld
Floyd-Warshall
2 / 15
Dynamic-Programming
Chandler Bureld
Floyd-Warshall
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 }.
"#$%&'%()*$%!+%&$),%-!./!0*$1!
!" #
!
!l #
Chandler Bureld
Floyd-Warshall
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 = j and (i , j ) E wij = if i = j and (i , j ) /E
W
An n x n matrix representing the edge weights of an n-vertex graph, where W = (wij ).
Chandler Bureld
Floyd-Warshall
5 / 15
Path Matrix
dij
(k )
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 Bureld
Floyd-Warshall
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, k 1 so the shortest such path has length dij 2. k is a vertex on the path, k 1 k 1 so the shortest such path is dik +dkj
(k )
as
(k )
Predecessor Matrix
ij
(k )
The predecessor of vertex j on a shortest path from vertex i with all intermediate vertices in the set {1, 2, ..., k }. Where ij = And ij
(k ) (0)
ij
if dij if dij
(k 1)
(k 1)
+ dkj + dkj
(k 1)
kj
(k 1)
(k 1)
(k 1)
(k 1)
( k )
The predecessor matrix, where (k ) = (ij ).
Chandler Bureld Floyd-Warshall February 20, 2013 8 / 15
(k )
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 Bureld
Floyd-Warshall
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
(n3 )
Space
(n3 ) here, but if we reuse space this can be done in (n2 ).
Chandler Bureld
Floyd-Warshall
10 / 15
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
Analysis
The shortest path can be constructed, not just the lengths of the paths. Runtime: (n3 ). Space: (n2 ).
Chandler Bureld
Floyd-Warshall
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 Bureld
Floyd-Warshall
12 / 15
Proof of Correctness
Inductive Hypothesis
Suppose that prior to the k th 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 Bureld
Floyd-Warshall
13 / 15
Applications
Chandler Bureld
Floyd-Warshall
14 / 15
Chandler Bureld
Floyd-Warshall
15 / 15