Tutorials › Graph Theory (Undergraduate) › Floyd-Warshall Correctness And Negative Cycles

Shortest Paths · Tutorial 252 of 799

Floyd-Warshall Correctness And Negative Cycles

Use an induction on allowed intermediate vertices to justify Floyd–Warshall, and understand exactly what its diagonal entries reveal about negative cycles.

Intermediate 11 min read

What You'll Learn

  • Define the distance represented by each intermediate-vertex stage.
  • Prove the recurrence by induction when no negative cycle exists.
  • Explain why shortest distances are attained when negative cycles are absent.
  • Show that a negative cycle forces a negative final diagonal entry.
  • Prove that a negative final diagonal entry certifies a negative cycle.
  • Trace small directed weighted examples, including unreachable pairs.

What the Floyd–Warshall Tables Mean

Floyd–Warshall updates a distance table in stages. At stage \(k\), it allows vertex \(k\) to be used as an intermediate vertex, in addition to the vertices already allowed. The key correctness question is whether these table entries really are shortest-path distances. The answer depends on the absence of negative cycles: with no negative cycle, the final table gives all shortest distances. If a negative cycle is present, a negative diagonal entry records that fact.

Throughout, consider a finite, loopless directed graph with a real weight \(w(u,v)\) on each directed edge. There is at most one edge for each ordered pair of vertices. A directed walk is a sequence of vertices in which each consecutive pair is joined by a directed edge; its weight is the sum of the weights of its edges. A directed path has no repeated vertices. A directed cycle is a positive-length closed directed walk with no repeated vertices except its first and last vertex. A negative cycle is a directed cycle whose total weight is negative. The empty walk from a vertex to itself has weight zero. We use \(+\infty\) for a pair with no available walk.

Definition: Number the vertices \(1,\dots,n\). Let \(D^{(k)}_{ij}\) be the entry after vertices \(1,\dots,k\) have been allowed as intermediate vertices. The endpoints \(i,j\) are unrestricted; only internal vertices of a walk are restricted. Set \(D^{(0)}_{ii}=0\), set \(D^{(0)}_{ij}=w(i,j)\) when the edge \(i\to j\) exists, and set \(D^{(0)}_{ij}=+\infty\) otherwise. Floyd–Warshall uses \(D^{(k)}_{ij}=\min\!\left(D^{(k-1)}_{ij},D^{(k-1)}_{ik}+D^{(k-1)}_{kj}\right)\).

In this formula, a sum involving \(+\infty\) is \(+\infty\). The first candidate keeps a route that does not use \(k\) internally; the second joins a route from \(i\) to \(k\) to one from \(k\) to \(j\). The earlier material on the intermediate-vertex recurrence develops this update. Here we justify its distance interpretation and establish what happens when negative cycles exist.

Why the Final Table Gives Shortest Distances

First, a basic consequence of having no negative cycles is that shortest distances exist whenever a destination is reachable. Any closed directed walk can be split into directed cycles: if it repeats a vertex other than the repeated endpoints, split at two occurrences of that vertex into shorter closed walks, and continue. The weights of the resulting cycles add to the weight of the original walk. Thus, if there is no negative cycle, every closed directed walk has nonnegative weight.

Now take any walk from \(i\) to \(j\) that repeats a vertex. The part between two occurrences of the same vertex is a closed walk, so it has nonnegative weight. Removing that part leaves a walk with the same endpoints and no greater weight. Repeating this removal gives a path, or the empty walk if \(i=j\). There are only finitely many paths in a finite graph, so if \(j\) is reachable from \(i\), a path of minimum weight exists. If \(i=j\), the empty walk has minimum weight zero, since every closed walk has nonnegative weight.

Theorem: If a directed weighted graph has no negative cycle, then \(D^{(k)}_{ij}\) equals the minimum weight of a walk from \(i\) to \(j\) whose internal vertices lie in \(\{1,\dots,k\}\). Consequently, \(D^{(n)}_{ij}\) is the shortest distance from \(i\) to \(j\), or \(+\infty\) if no such walk exists.

Proof. We use induction on \(k\). For \(k=0\), a walk with no internal vertices is either the empty walk from a vertex to itself, a single edge, or nonexistent. These possibilities give exactly the entries of \(D^{(0)}\).

Assume the assertion holds at stage \(k-1\). Consider first distinct endpoints \(i\ne j\). By the preceding argument, a minimum-weight walk subject to the allowed intermediate vertices can be chosen to be a path. If this path does not contain \(k\) internally, its weight is at least \(D^{(k-1)}_{ij}\), and the induction hypothesis says that this candidate is attainable when finite. If it does contain \(k\), it contains \(k\) only once, because it is a path. Splitting at \(k\) gives an \(i\)-to-\(k\) path and a \(k\)-to-\(j\) path whose internal vertices are all among \(1,\dots,k-1\). By induction, their minimum weights are \(D^{(k-1)}_{ik}\) and \(D^{(k-1)}_{kj}\). Their concatenation is a permitted walk through \(k\), with weight equal to the sum of those entries. Taking the smaller of the two cases gives the recurrence.

For \(i=j\), the empty walk has weight zero and is permitted at every stage. Every other permitted walk from \(i\) back to \(i\) is closed and has nonnegative weight, because there is no negative cycle. Therefore the minimum is zero. The recurrence also returns zero: its first candidate is \(D^{(k-1)}_{ii}=0\) by induction, and its second candidate, if finite, is the weight of a closed walk formed by joining an \(i\)-to-\(k\) walk to a \(k\)-to-\(i\) walk. That weight is nonnegative. This proves the assertion for equal endpoints as well. At \(k=n\), every vertex is allowed as an intermediate vertex, proving the final claim. \(\square\)

Worked Tables Without Negative Cycles

Worked Example: Negative Edges but No Negative Cycle

Let the directed graph have \(V=\{1,2,3\}\) and edge set \(\{1\to2\text{ of weight }4,\ 1\to3\text{ of weight }10,\ 2\to3\text{ of weight }-2,\ 3\to1\text{ of weight }3\}\). The directed cycle \(1\to2\to3\to1\) has weight \(4+(-2)+3=5\); the other directed cycles have weights \(10+3=13\) and \((-2)+3+4=5\). Hence there is no negative cycle.

The initial table is

$$ D^{(0)}= \begin{pmatrix} 0&4&10\\ +\infty&0&-2\\ 3&+\infty&0 \end{pmatrix}. $$

At \(k=1\), the new route from \(3\) to \(2\) has weight \(3+4=7\). The route from \(3\) to \(3\) through \(1\) has weight \(3+10=13\), which does not improve its zero entry. The other entries do not improve, so

$$ D^{(1)}= \begin{pmatrix} 0&4&10\\ +\infty&0&-2\\ 3&7&0 \end{pmatrix}. $$

At \(k=2\), the route from \(1\) to \(3\) through \(2\) has weight \(4+(-2)=2\), improving \(10\). The route from \(3\) to \(3\) through \(2\) has weight \(7+(-2)=5\), which does not improve zero. Thus

$$ D^{(2)}= \begin{pmatrix} 0&4&2\\ +\infty&0&-2\\ 3&7&0 \end{pmatrix}. $$

At \(k=3\), the route from \(2\) to \(1\) through \(3\) has weight \((-2)+3=1\). The possible improvements from \(1\) to \(1\), \(1\) to \(2\), and \(2\) to \(2\) have weights \(2+3=5\), \(2+7=9\), and \((-2)+7=5\), respectively, so none improves the existing entries. The final table is

$$ D^{(3)}= \begin{pmatrix} 0&4&2\\ 1&0&-2\\ 3&7&0 \end{pmatrix}. $$

Check: The improved routes have weights \(1\to2\to3: 4+(-2)=2\) and \(2\to3\to1: (-2)+3=1\). The other reachable pairs have direct routes of weights \(4,-2,3,7\), or \(0\) on the diagonal as shown. In particular, all diagonal entries are zero, consistent with the absence of negative cycles.

Worked Example: An Unreachable Pair

Let \(V=\{1,2,3\}\) and let the edge set be \(\{1\to2\text{ of weight }2,\ 2\to3\text{ of weight }3,\ 1\to3\text{ of weight }8\}\). This directed graph has no cycle, so it has no negative cycle. Its initial table is

$$ D^{(0)}= \begin{pmatrix} 0&2&8\\ +\infty&0&3\\ +\infty&+\infty&0 \end{pmatrix}. $$

Allowing vertex \(1\) does not improve any entry: there is no edge into \(1\), and the entries out of \(1\) already appear in the initial table. Thus \(D^{(1)}=D^{(0)}\). At \(k=2\), the new route from \(1\) to \(3\) has weight \(2+3=5\), so it improves the direct weight \(8\). The other entries do not improve, giving

$$ D^{(2)}= \begin{pmatrix} 0&2&5\\ +\infty&0&3\\ +\infty&+\infty&0 \end{pmatrix}. $$

Allowing vertex \(3\) makes no improvement: there is no edge leaving \(3\), so no new finite route can use it as an intermediate vertex. Hence \(D^{(3)}=D^{(2)}\). The pair \(3,1\) remains unreachable, represented by \(+\infty\).

Check: The only route from \(1\) to \(3\) using two edges is \(1\to2\to3\), with weight \(2+3=5\); the direct edge has weight \(8\). There is no directed route from \(3\) to \(1\), and each diagonal entry remains zero.

Negative Cycles and the Diagonal

When a negative cycle is reachable from a vertex and can return to it, closed walks through that vertex can be made arbitrarily negative by repeating the cycle. There is then no finite shortest distance for that pair. Floyd–Warshall’s final diagonal detects the presence of such a cycle anywhere in the graph, whether or not every vertex can reach it.

Theorem: A finite entry \(D^{(n)}_{ii}<0\) exists for some vertex \(i\) if and only if the directed graph contains a negative cycle.

Proof. First suppose there is a negative cycle \(C\). We show that the recurrence can represent its weight in a diagonal entry. More precisely, for every \(k\), it gives an entry no greater than the weight of any simple directed path whose internal vertices have labels at most \(k\), and no greater than the weight of any simple directed cycle rooted at \(i\) whose internal vertices have labels at most \(k\). At \(k=0\), the path claim follows from the direct-edge entries, and there is no loopless cycle with no internal vertices.

For the induction step, take either such a path or such a cycle. If \(k\) is not an internal vertex, the induction hypothesis applies at \(k-1\), and the recurrence never increases an entry because it takes a minimum including the previous entry. If \(k\) is internal, it occurs exactly once. For a path, split it at \(k\) into two simple paths; for a cycle rooted at \(i\), split it at \(k\) into a path from \(i\) to \(k\) and a path from \(k\) back to \(i\). Each piece has internal vertices labelled at most \(k-1\). By induction, the corresponding \(D^{(k-1)}\) entries are no greater than the weights of the pieces. The recurrence’s through-\(k\) candidate is therefore no greater than their sum, which is the original path or cycle weight. This proves the claim.

Choose \(k\) to be the largest vertex label on \(C\), and root \(C\) at a vertex \(i\) on it. The claim gives \(D^{(k)}_{ii}\le w(C)<0\). Since later stages never increase entries, \(D^{(n)}_{ii}<0\).

Conversely, every finite entry produced by the recurrence is the weight of an actual directed walk: this holds initially, and a finite through-\(k\) candidate concatenates two walks represented at stage \(k-1\). Thus \(D^{(n)}_{ii}<0\) gives a negative closed walk. If a closed walk is not a simple cycle, splitting it at a repeated vertex expresses its weight as the sum of the weights of two shorter closed walks. A negative sum has at least one negative summand. Repeating this splitting eventually yields a negative simple directed cycle. This proves the converse. \(\square\)

Worked Example: A Negative Cycle Makes the Diagonal Negative

Let \(V=\{1,2,3\}\) and let the directed edge set be \(\{1\to2\text{ of weight }-1,\ 2\to3\text{ of weight }-1,\ 3\to1\text{ of weight }1\}\). The cycle \(1\to2\to3\to1\) has weight \((-1)+(-1)+1=-1\). The initial table is

$$ D^{(0)}= \begin{pmatrix} 0&-1&+\infty\\ +\infty&0&-1\\ 1&+\infty&0 \end{pmatrix}. $$

At \(k=1\), the route \(3\to1\to2\) has weight \(1+(-1)=0\), so \(D^{(1)}_{32}=0\); the other entries do not improve. At \(k=2\), \(1\to2\to3\) has weight \((-1)+(-1)=-2\), and \(3\to2\to3\) has weight \(0+(-1)=-1\). Therefore

$$ D^{(2)}= \begin{pmatrix} 0&-1&-2\\ +\infty&0&-1\\ 1&0&-1 \end{pmatrix}. $$

At \(k=3\), compute each entry as the minimum of its old value and the sum through \(3\). Row \(1\) becomes \((-1,-2,-3)\), since its through-\(3\) candidates are \(-2+1=-1\), \(-2+0=-2\), and \(-2+(-1)=-3\). Row \(2\) becomes \((0,-1,-2)\), from candidates \((-1)+1=0\), \((-1)+0=-1\), and \((-1)+(-1)=-2\). Row \(3\) becomes \((0,-1,-2)\), from candidates \((-1)+1=0\), \((-1)+0=-1\), and \((-1)+(-1)=-2\). Thus

$$ D^{(3)}= \begin{pmatrix} -1&-2&-3\\ 0&-1&-2\\ 0&-1&-2 \end{pmatrix}. $$

Check: The walk \(3\to1\to2\to3\to1\to2\to3\) starts and ends at \(3\), traverses the cycle twice, and has weight \((1-1-1)+(1-1-1)=-2\). This agrees with \(D^{(3)}_{33}=-2\). The negative diagonal is therefore consistent with the negative cycle of weight \(-1\).

Reading the Result Correctly

With no negative cycle, the diagonal of the final table is zero, and every finite off-diagonal entry is a shortest distance. A value \(+\infty\) means there is no directed route between that ordered pair. With a negative cycle, a negative diagonal entry is a certificate that shortest distances are not well-defined for all pairs in the ordinary finite sense. The table’s other entries should not then be interpreted as a complete all-pairs shortest-distance solution.

Common mistake. A negative edge is not by itself evidence of a negative cycle. In the first example, the edge \(2\to3\) has weight \(-2\), but every directed cycle has positive weight, and the final diagonal remains zero. Conversely, checking only whether an input edge has negative weight cannot establish that the graph is safe. The relevant test is the diagonal after all vertices have been allowed: a negative entry means a negative cycle exists. Do not confuse this with a negative off-diagonal distance, which can occur even when there is no negative cycle.

Check Your Understanding

Use the recurrence and the proofs above to answer the following questions.

  1. In a graph with no negative cycle, why can a minimum-weight walk between distinct reachable vertices be chosen to be a path?
  2. Suppose a shortest path from \(i\) to \(j\) uses vertex \(k\) internally. Why can it be split into two paths covered by stage \(k-1\)?
  3. What does \(D^{(n)}_{ij}=+\infty\) mean, and what does \(D^{(n)}_{ii}<0\) mean?
  4. Can an off-diagonal entry be negative when there is no negative cycle? Explain using the role of edge weights.
  5. For the graph in the negative-cycle example, calculate the weight of one traversal of \(1\to2\to3\to1\), then explain why repeating it makes closed-walk weights arbitrarily small.