Sie sind auf Seite 1von 15

Floyd-Warshall Algorithm

Chandler Bureld

February 20, 2013

Chandler Bureld

Floyd-Warshall

February 20, 2013

1 / 15

All-Pairs Shortest Paths

Problem
To nd the shortest path between all vertices v V for a weighted graph G = (V , E ).

Chandler Bureld

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 dene the value of an optimal solution. 3. Compute the value of an optimal solution in a bottom-up manner.

Chandler Bureld

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

"#$%&'%()*$%!+%&$),%-!./!0*$1!

!" #
!

!l #

Chandler Bureld

Floyd-Warshall

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 = 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

February 20, 2013

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

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, 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

So we see that we can recursively dene dij dij


Chandler Bureld

(k )

as

(k )

wij if k = 0 (k 1) (k 1) (k 1) min(dij , dik + dkj ) if k 1


Floyd-Warshall February 20, 2013 7 / 15

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)

NIL if i = j or wij = i if i = j and wij <


(k 1)

ij

if dij if dij

(k 1)

dik > dik

(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

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

(n3 )

Space
(n3 ) here, but if we reuse space this can be done in (n2 ).

Chandler Bureld

Floyd-Warshall

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

Analysis
The shortest path can be constructed, not just the lengths of the paths. Runtime: (n3 ). Space: (n2 ).

Chandler Bureld

Floyd-Warshall

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 Bureld

Floyd-Warshall

February 20, 2013

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

February 20, 2013

13 / 15

Applications

Detecting the Presence of a Negative Cycle Transitive Closure of a Directed Graph

Chandler Bureld

Floyd-Warshall

February 20, 2013

14 / 15

Other All-Pairs Shortest Paths Algorithms


Dynamic Programming Approach Based on Matrix Multiplication Johnsons Algorithm for Sparse Graphs

Chandler Bureld

Floyd-Warshall

February 20, 2013

15 / 15

Das könnte Ihnen auch gefallen