Negative Weight Cycle and the Bellman-Ford Algorithm for Single-Source Shortest Distance
A negative weight cycle in a directed graph is a cycle such that the sum of the weights of all edges on that cycle is negative.2 If such a cycle is reachable from the source vertex, then the shortest path problem becomes ill-defined, because traversing the cycle repeatedly keeps decreasing the total path cost without bound.2 In effect, the path cost can be pushed toward , so no finite shortest distance exists for vertices reachable through that cycle.2
The Bellman-Ford algorithm solves the single-source shortest-distance problem in weighted directed graphs even when some edge weights are negative, provided there is no reachable negative weight cycle.2 Its core idea is relaxation: for every edge with weight , if
then the estimate is improved.2
A key theoretical fact is that any simple path in a graph with vertices contains at most edges. Therefore, after relaxing all edges exactly times, Bellman-Ford computes correct shortest distances from the source to all reachable vertices if no reachable negative cycle exists.2
In the conceptual graph above, if the cycle has total weight , then every route from to through that cycle can be made cheaper by looping one more time before proceeding to .2
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩ ↩2 ↩3
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7
-
Bellman-Ford and Floyd-Warshall 1 Single Source Shortest Path - Notes explaining why negative cycles make shortest paths ill-defined and can drive distances to negative infinity. ↩
-
DP: Single Source Shortest Paths, The Bellman-Ford Algorithm - University notes discussing directed graphs, negative edges, and why negative cycles invalidate shortest-path computation. ↩
-
Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. ↩ ↩2
Bellman-Ford in 5 minutes — Step by step example
Core Definition
A negative weight cycle does not merely contain a negative edge; the total sum of the entire cycle must be negative.2
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩
-
DP: Single Source Shortest Paths, The Bellman-Ford Algorithm - University notes discussing directed graphs, negative edges, and why negative cycles invalidate shortest-path computation. ↩
Why negative weight cycles matter
In graphs with only positive weights, every additional cycle increases or preserves path cost. With negative cycles, repeated traversal keeps lowering the path total.2 This is why shortest-distance computation in such a graph is not just difficult; it is mathematically undefined for vertices reachable from the cycle.2
Suppose a cycle has total weight . If a path from source reaches that cycle, then after going around it times, the path cost decreases by . Since can be arbitrarily large,
Hence, no minimum finite distance exists.2
This distinction is important:
| Case | Meaning | Bellman-Ford outcome |
|---|---|---|
| Negative edges but no negative cycle | Shortest paths still well-defined | Computes distances correctly2 |
| Negative cycle unreachable from source | Does not affect source-based answers | Distances for reachable vertices still valid |
| Negative cycle reachable from source | Shortest distances not well-defined | Algorithm reports failure / negative cycle2 |
Important distance estimate, predecessor, and reachability concepts determine exactly which vertices are affected.2
Footnotes
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7
-
Bellman-Ford and Floyd-Warshall 1 Single Source Shortest Path - Notes explaining why negative cycles make shortest paths ill-defined and can drive distances to negative infinity. ↩ ↩2
-
DP: Single Source Shortest Paths, The Bellman-Ford Algorithm - University notes discussing directed graphs, negative edges, and why negative cycles invalidate shortest-path computation. ↩
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩
-
Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. ↩ ↩2
Bellman-Ford Algorithm for Single-Source Shortest Distance
- 1Step 1
Set for the source vertex , and set for every other vertex . Also initialize predecessor for path reconstruction if needed.2
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩
-
- 2Step 2
Perform passes over all edges. For each directed edge with weight , update if . This propagates best-known distances outward from the source.3
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩
-
Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. ↩
-
- 3Step 3
Whenever a relaxation improves , set . The predecessor array forms a shortest-path tree if no reachable negative cycle exists.
Footnotes
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩
-
- 4Step 4
Scan all edges one more time. If any edge can still be relaxed, then a negative weight cycle is reachable from the source, and shortest distances are not well-defined.3
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩
-
Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. ↩
-
- 5Step 5
If no further relaxation is possible in the extra pass, output the distance array as the single-source shortest distances. Unreachable vertices remain at .2
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩
-
Standard Bellman-Ford pseudocode
Let be a weighted directed graph and be the source.
1BELLMAN-FORD(G, w, s) 21. for each vertex v in V: 32. d[v] = ∞ 43. π[v] = NIL 54. d[s] = 0 6 75. for i = 1 to |V| - 1: 86. for each edge (u, v) in E: 97. if d[u] ≠ ∞ and d[u] + w(u, v) < d[v]: 108. d[v] = d[u] + w(u, v) 119. π[v] = u 12 1310. for each edge (u, v) in E: 1411. if d[u] ≠ ∞ and d[u] + w(u, v) < d[v]: 1512. return "Negative weight cycle reachable from source" 16 1713. return d, π
This algorithm runs in
time, because it scans all edges times plus one final pass, and uses
extra space for distance and predecessor arrays.2
A useful correctness intuition is: after the -th full pass, Bellman-Ford has correctly computed shortest distances for all vertices whose shortest path uses at most edges.2
Footnotes
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩
-
Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. ↩ ↩2
-
Shortest Paths II: Bellman-Ford - MIT 6.006 Lecture Notes - Formal lecture notes on Bellman-Ford correctness and path-length interpretation by iteration. ↩
Common Misconception
A graph may contain a negative edge without containing a negative cycle. Bellman-Ford handles negative edges correctly; it fails only when a reachable negative cycle exists.2
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩
Worked example
Consider the weighted directed graph with source :
| Edge | Weight |
|---|---|
| 4 | |
| 5 | |
| -2 | |
| 6 | |
| 3 |
There is no negative cycle here, although one edge has negative weight.2
Initialization
Pass 1
- Relax :
- Relax :
- Relax :
- Relax :
- Relax :
After pass 1:
- , , ,
Pass 2 No value improves further.
Pass 3 No value improves further.
Since , we need at most passes.2 Final shortest distances from are:
| Vertex | Shortest distance from |
|---|---|
| 0 | |
| 4 | |
| 2 | |
| 5 |
Now compare that with a graph containing a cycle of total weight . Then after passes, the distances around that cycle can still decrease, so the extra scan detects the negative cycle.2
Terms such as simple path, edge relaxation, and cycle weight are central to understanding why the algorithm works.2
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩ ↩2
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩ ↩2 ↩3 ↩4
-
Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. ↩ ↩2
Bellman-Ford Complexity Overview
Asymptotic cost for single-source shortest path in weighted directed graphs
1for each vertex v: 2 d[v] = ∞ 3 parent[v] = NIL 4 5d[source] = 0 6 7repeat V-1 times: 8 for each edge (u, v, w): 9 if d[u] != ∞ and d[u] + w < d[v]: 10 d[v] = d[u] + w 11 parent[v] = u 12 13for each edge (u, v, w): 14 if d[u] != ∞ and d[u] + w < d[v]: 15 report negative cycle
Important Clarifications and Edge Cases
Exam-Friendly Writing Pattern
When asked to write Bellman-Ford, include: initialization, |V|-1 relaxations, one extra pass for negative-cycle detection, and the time complexity .3
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩
-
Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. ↩
Concise academic answer suitable for theory exams
A negative weight cycle is a cycle in a weighted directed graph whose total edge weight is negative.2 If such a cycle is reachable from the source, then the single-source shortest-distance problem has no finite solution, because repeatedly traversing the cycle decreases path cost indefinitely.2
The Bellman-Ford algorithm finds shortest distances from one source to all vertices in a directed weighted graph, even when some edges have negative weights.2 It initializes the source distance to and all others to , then relaxes every edge repeatedly for rounds.2 Finally, it checks all edges once more; if any distance can still be reduced, a reachable negative weight cycle exists.3
Algorithm:
- Set and for all .
- Repeat times:
- For every edge , if , set .2
- For every edge , if , report negative weight cycle.2
- Otherwise, output all distances.
Its time complexity is and space complexity is .2
Footnotes
-
Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8
-
CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7
-
Bellman-Ford and Floyd-Warshall 1 Single Source Shortest Path - Notes explaining why negative cycles make shortest paths ill-defined and can drive distances to negative infinity. ↩
-
Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. ↩ ↩2 ↩3
Knowledge Check
What is a negative weight cycle?
Explore Related Topics
Minimum Spanning Tree (MST) and Prim's Algorithm
Prim's algorithm builds a minimum spanning tree (MST) of a connected weighted undirected graph by repeatedly adding the cheapest edge that links the current tree to a new vertex.
- An MST includes all vertices, has exactly edges, contains no cycles, and minimizes total edge weight.
- The cut property guarantees that the lightest edge crossing any cut (the current tree vs. the rest) is safe to include in an MST.
- Algorithm steps: start at any vertex, repeatedly select the minimum‑weight crossing edge, add its outside vertex, and update candidate edges until all vertices are reached.
- With an adjacency list and a priority queue, the runtime is ; using an adjacency matrix it is .
- In the example graph, Prim selects edges , producing an MST of total weight .
Justifying Why a Cycle in a Resource Allocation Graph Does Not Always Imply Deadlock
A cycle in a resource allocation graph (RAG) does not always imply deadlock; the conclusion hinges on whether each resource type involved has a single instance or multiple instances.
- No cycle → the system cannot be deadlocked.
- Cycle + every resource in the cycle has one instance ⇒ deadlock is guaranteed.
- Cycle + any resource has multiple instances ⇒ deadlock is possible but not certain ().
- Thus a cycle is a necessary but not sufficient condition for deadlock in multi‑instance systems.
- Example: with two instances of and , a cycle can be broken when another process releases an instance.
Dijkstra's Algorithm
Dijkstra's algorithm finds the shortest‑path distances from a single source to all reachable vertices in a weighted graph whose edge weights are non‑negative, using a greedy selection of the minimum‑distance unsettled vertex and edge relaxation.
- Keeps a tentative distance array and a min‑priority queue; extracting the smallest distance finalizes that vertex’s shortest path.
- Correct only for non‑negative edges; negative weights invalidate the greedy invariant.
- With an adjacency‑list and binary heap the time is O((V+E) log V); matrix scans give O(V²) and Fibonacci heaps can achieve O(E + V log V).
- Storing a predecessor array allows reconstruction of actual shortest paths and early termination when a specific target is settled.
- Fundamental in routing, navigation, and as a subroutine in many larger graph algorithms.
