Sub topic 4
Negative Cycles: When Shortest Paths Stop Existing
This chapter assumes you know what a weighted directed graph is and what a path's total weight means. It also assumes you have seen the shortest path problem stated for a single source. You do not need to know any algorithm yet.
What a negative cycle is
A cycle is a path that starts and ends at the same vertex. A negative cycle is a cycle whose edge weights add up to less than zero. Some of its individual edges may be positive. Only the total matters.
In the graph below, the cycle A → B → C → A has weight 2 + (−3) + 0 = −1.
Why shortest paths stop existing
Each trip around a negative cycle lowers the total cost of a walk by the same amount. If you can reach the cycle from the source and then leave it toward a target, no walk is ever the cheapest, because one more lap always gives a cheaper one. The distance goes down without limit, so it is written as −∞.
You can see this by measuring walks from S to T that make more and more laps:
w = {("S", "A"): 1, ("S", "D"): 5, ("A", "B"): 2,
("B", "C"): -3, ("C", "A"): 0, ("C", "T"): 4}
def cost(path):
return sum(w[(u, v)] for u, v in zip(path, path[1:]))
for laps in range(4):
path = ["S"] + ["A", "B", "C"] * laps + ["A", "B", "C", "T"]
print(laps, cost(path))
# 0 4
# 1 3
# 2 2
# 3 1The cost drops by 1 on every lap, and nothing stops it from going on forever.
Which distances are affected
A negative cycle does not ruin the whole graph. A vertex v has distance −∞ only when both of these are true:
- The cycle can be reached from the source.
- v can be reached from the cycle.
Every other vertex keeps a normal, finite distance.
| Vertex | Reachable from S | Reachable from the cycle | Distance from S |
|---|---|---|---|
| S | yes | no | 0 |
| D | yes | no | 5 |
| A, B, C | yes | yes | −∞ |
| T | yes | yes | −∞ |
A negative cycle that the source cannot reach has no effect on single-source distances. In the all-pairs problem, the same cycle still makes some pairs −∞, namely the pairs whose start vertex can reach the cycle.
Common misconceptions
- "Just forbid repeated vertices." Asking for the shortest simple path (one that never repeats a vertex) does make the answer finite. The trouble is that this version of the problem is NP-hard in general. Bellman-Ford and Floyd-Warshall do not solve it.
- "Negative edges mean negative cycles." This is false. A graph can have many negative edges and still have no negative cycle. In that case every shortest path is well defined.
- "Return the most negative number found." If an algorithm stops after a fixed number of steps on a graph with a negative cycle, the number it returns means nothing. A correct algorithm has to detect the cycle and report it. The later chapters on Bellman-Ford and Floyd-Warshall show how.
Exercise
In the graph above, what is the smallest weight for the edge C → A that removes the negative cycle? After that change, what is the shortest distance from S to T?
Show answer
The cycle weight is 2 − 3 + w(C → A) = w(C → A) − 1, so w(C → A) must be at least 1. With w(C → A) = 1, the cycle weighs 0, which is not negative. The shortest path S → A → B → C → T then costs 1 + 2 − 3 + 4 = 4.
Exercise
Add a new vertex E and an edge E → A of weight 10, with no edges into E. Does the negative cycle change dist(S, E) or dist(E, T)?
Show answer
dist(S, E) is +∞ because E cannot be reached from S, and the cycle has no effect on that. dist(E, T) is −∞ in the all-pairs sense, because E can reach the cycle and the cycle can reach T.