The Floyd-Warshall algorithm finds the shortest paths between all pairs of vertices in a weighted graph. It works by computing the shortest path between all pairs where intermediate vertices are restricted to being from a subset at each step, until the subset is the full vertex set. It has O(V^3) time complexity and O(V^2) space complexity. The algorithm stores predecessor pointers to extract the actual shortest paths between pairs of vertices once the distances are computed.
1. Lecture 15: The Floyd-Warshall
Algorithm
CLRS section 25.2
Outline of this Lecture
Recalling the all-pairs shortest path problem.
Recalling the previous two solutions.
The Floyd-Warshall Algorithm.
1
6. Step 2: Structure of shortest paths
Observation 1:
A shortest path does not contain the same vertex twice.
Proof: A path containing the same vertex twice contains a cycle. Removing cycle gives a shorter path.
Observation 2: For a shortest path from to such
that any intermediate vertices on the path are chosen
, there are two possibilities:
from the set
'
(% ¥
.
£
¨¥ 0
§
¡
¤£¡
¡
¨¥
§
¨
¨
¥
#
$¥
!
%
%
2. is a vertex on the path.
The shortest such path has length
¨¥
§
¨
1. is not a vertex on the path,
The shortest such path has length
6
.
7. Step 2: Structure of shortest paths
Consider a shortest path from to containing the
vertex . It consists of a subpath from to and a
subpath from to .
Each subpath can only contain intermediate vertices
, and must be as short as possible,
in
namely they have lengths
and
.
%
%
¨¥
§
%
'
¡
¨¥ 0
§
£
¨¥
§
£
¡
¡
¨¥
§
%
$¥ ¨ ¨ ¨ ¥
!
!
Hence the path has length
.
Combining the two cases we get
¨
¦
7
§ ¥ 0
£
¡
¡
¨¥
§
¥
¨¥
§
¤£¡
¥
£
¤¢
¡
¡
¤£¡