...

/

Floyd-Warshall Algorithm for the ASSP

Floyd-Warshall Algorithm for the ASSP

Use Floyd-Warshall to compute shortest paths between all pairs of nodes.

In the next lessons, we’ll focus on the All Sources Shortest Path (ASSP) problem. Of course, we could solve the ASSP problem by just running Dijkstra’s algorithm once from each starting vertex. But the Floyd-Warshall algorithm, which was designed to specifically solve the ASSP problem, is a viable alternative. Its benefits are that it is very simple to implement and it runs faster than repeated Dijkstra executions on dense graphs.

The Floyd-Warshall algorithm

To explain how the Floyd-Warshall algorithm works, let’s look at a small example graph.

g 0 0 1 1 0->1 6 2 2 0->2 3 3 3 0->3 12 1->2 9 1->3 5 2->3 2 3->1 4 3->2 1
An example graph with 4 nodes

The Floyd-Warshall algorithm is remarkable in the sense that it is one of the few algorithms that uses the weighted adjacency matrix graph representation. Let’s assume that all edge weights are positive, so we can use a single matrix to represent the graph. The weighted adjacency matrix of our example graph is

A=(06312009500020410)A = \begin{pmatrix} 0 & 6 & 3 & 12 \\ 0 & 0 & 9 & 5 \\ 0 & 0 & 0 & 2 \\ 0 & 4 & 1 & 0 \end{pmatrix}

The basic idea of Floyd-Warshall is to successively transform the adjacency matrix to end up with a resulting matrix that contains the lengths of the shortest paths between each pair of nodes.

In the following, let’s write nn for the number of nodes V|V|. In particular, the adjacency matrix is an n×nn \times n matrix and the nodes of the graph are numbered 0,1,,n10, 1, \ldots, n-1.

The Floyd-Warshall algorithm successively computes a sequence of n×nn \times n distance matrices

A0,A1,,AnA_0, A_1, \ldots, A_n

where A0A_0 can be obtained directly from the adjacency matrix and AnA_n contains the end result.

Let’s look at the definition of these matrices AkA_k.

Distance matrices: Ak[i,j]A_k[i, j] is the length of the shortest path from node ii to node jj that visits only intermediate nodes with an index less than kk.

So, the matrix AkA_k contains the lengths of the shortest paths between each pair of nodes, assuming that the nodes with index k\geq k are forbidden to visit in between. We’ll call such a path a k\mathbf{k}-restricted shortest path.

Let’s take a deeper look at the first and last path matrix, A0A_0 and AnA_n.

The matrix AnA_n contains the lengths of shortest paths between any pair of nodes that visit only nodes with index <n< n in between. Since all nodes have an index <n< n, the matrix AnA_n is our desired end result containing the lengths of the shortest paths between all pairs of nodes.

On the other hand, the matrix A0A_0 contains lengths of shortest paths between all pairs of nodes that may only visit nodes with index <0< 0 in between. Since there is no node with a negative index, the shortest paths can only be direct edges between nodes. Thus, we have

A0[i,j]={0i=jw(i,j)(i,j)EotherwiseA_0[i, j] = \begin{cases} 0 &i = j \\ w(i, j) &(i, j) \in E \\ \infty &\text{otherwise} \end{cases}

Here, we follow the convention that if no shortest path between two nodes exists, then their distance is infinite. Further, the shortest path from a node to itself is the empty path of length 00.

In particular, A0A_0 ...