The Floyd-Warshall Algorithm
Explore the Floyd-Warshall algorithm to compute shortest paths between all pairs of vertices in a weighted graph. Understand its step-by-step approach using dynamic programming, how it updates distance and parent arrays, and its cubic runtime complexity. This lesson equips you to apply this algorithm to graph problems requiring all-pairs shortest path solutions and path recovery.
We'll cover the following...
The Floyd-Warshall algorithm is the algorithm of choice for the all-pairs shortest-paths problem.
Every vertex either appears in a shortest path from a vertex to a vertex , or it doesn’t.
- If there is such a path through , we can express its length as the sum of the lengths of shortest and paths.
- If there is no shortest path through , then we have a smaller subproblem on our hands, in which the set of candidate vertices that can appear as intermediate vertices of the shortest path is smaller (as it does not include ).
The central idea behind the Floyd-Warshall algorithm is essentially a refinement of this notion in which the set of vertices being considered as intermediate vertices is gradually expanded.
The central idea
Let’s think about the length of a shortest path from a vertex to whose intermediate vertices are confined to a set .
Let’s say is the length of a shortest possible path with intermediate vertices, if any, belonging to the set .
For example, in the following digraph with the vertex set containing five vertices and the set of vertices shown in the cloud,
On the other hand, if the entire vertex set is allowed as the set of intermediate vertices,
...