...

/

The Floyd-Warshall Algorithm

The Floyd-Warshall Algorithm

Learn how the Floyd-Warshall algorithm finds shortest paths between all pairs of vertices.

The Floyd-Warshall algorithm is the algorithm of choice for the all-pairs shortest-paths problem.

Every vertex xx either appears in a shortest path from a vertex vv to a vertex ww, or it doesn’t.

  • If there is such a path through xx, we can express its length as the sum of the lengths of shortest vxv-x and xwx - w paths.
  • If there is no vwv - w shortest path through xx, 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 vwv - w path is smaller (as it does not include xx).
Press + to interact
A shortest path from v to w may or may not pass through a vertex x
A shortest path from v to w may or may not pass through a vertex x

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 vv to ww whose intermediate vertices are confined to a set IV\cal{I} \subseteq \textup{V}.

Let’s say dI[v][w]d_{\cal{I}}\bigr[v\bigr]\bigr[w\bigr] is the length of a shortest possible vwv-w path with intermediate vertices, if any, belonging to the set I\cal{I}.

For example, in the following digraph with the vertex set VV containing five vertices and the set I\cal{I} of vertices shown in the cloud,

dI[v][w]=4d_{\cal{I}}[v][w]=4

On the other hand, if the entire vertex set VV ...