Belmanfordt
Belmanfordt
the single edges in the graph. At k = 1, paths that go through the vertex 1 are found: in particular, the
path [2,1,3] is found, replacing the path [2,3] which has fewer edges but is longer (in terms of weight). At
k = 2, paths going through the vertices {1,2} are found. The red and blue boxes show how the path
[4,2,1,3] is assembled from the two known paths [4,2] and [2,1,3] encountered in previous iterations,
with 2 in the intersection. The path [4,2,3] is not considered, because [2,1,3] is the shortest path
encountered so far from 2 to 3. At k = 3, paths going through the vertices {1,2,3} are found. Finally, at k =
4, all shortest paths are found.
The distance matrix at each iteration of k, with the updated distances in bold, will be:
k=0 j
1 2 3 4
i 1 0 ∞ −2 ∞
2 4 0 3 ∞
3 ∞ ∞ 0 2
4 ∞ −1 ∞ 0
k=1 j
1 2 3 4
i 1 0 ∞ −2 ∞
2 4 0 2 ∞
3 ∞ ∞ 0 2
4 ∞ −1 ∞ 0
k=2 j
1 2 3 4
i 1 0 ∞ −2 ∞
2 4 0 2 ∞
3 ∞ ∞ 0 2
4 3 −1 1 0
k=3 j
1 2 3 4
i 1 0 ∞ −2 0
2 4 0 2 4
3 ∞ ∞ 0 2
4 3 −1 1 0
k=4 j
1 2 3 4
i 1 0 −1 −2 0
2 4 0 2 4
3 5 1 0 2
4 3 −1 1 0
A negative cycle is a cycle whose edges sum to a negative value. There is no shortest path between any
pair of vertices
{\displaystyle i},
{\displaystyle i} to
{\displaystyle j} can be arbitrarily small (negative). For numerically meaningful output, the Floyd–
Warshall algorithm assumes that there are no negative cycles. Nevertheless, if there are negative cycles,
the Floyd–Warshall algorithm can be used to detect them. The intuition is as follows:
The Floyd–Warshall algorithm iteratively revises path lengths between all pairs of vertices
{\displaystyle i=j};
A path
,
𝑖
{\displaystyle [i,k,\ldots ,i]} can only improve upon this if it has length less than zero, i.e. denotes a
negative cycle;
{\displaystyle i} back to
{\displaystyle i}.
Hence, to detect negative cycles using the Floyd–Warshall algorithm, one can inspect the diagonal of the
path matrix, and the presence of a negative number indicates that the graph contains at least one
negative cycle.[9] During the execution of the algorithm, if there is a negative cycle, exponentially large
numbers can appear, as large as
𝑤
𝑚
{\displaystyle w_{max}} is the largest absolute value of a negative edge in the graph. To avoid
overflow/underflow problems one should check for negative numbers on the diagonal of the path matrix
within the inner for loop of the algorithm.[10] Obviously, in an undirected graph a negative edge creates
a negative cycle (i.e., a closed walk) involving its incident vertices. Considering all edges of the above
example graph as undirected, e.g. the vertex sequence 4 – 2 – 4 is a cycle with weight sum −2.
Path reconstruction
The Floyd–Warshall algorithm typically only provides the lengths of the paths between all pairs of
vertices. With simple modifications, it is possible to create a method to reconstruct the actual path
between any two endpoint vertices. While one may be inclined to store the actual path from each vertex
to each other vertex, this is not necessary, and in fact, is very costly in terms of memory. Instead, the
shortest-path tree can be calculated for each node in
Θ
(
{\displaystyle \Theta (|V|)} memory to store each tree which allows us to efficiently reconstruct a path
from any two connected vertices.
Pseudocode
Source:[11]
let dist be a
let prev be a
×
|
procedure FloydWarshallWithPathReconstruction() is
prev[u][v] ← u
dist[v][v] ← 0
prev[v][v] ← v
prev[i][j] ← prev[k][j]