1. Gyanastra
  2. Algorithms
  3. Bellman-Ford Algorithm
  4. Bellman-Ford Algorithm: Negative Weight Cycle Detection
Free lesson, no account needed 5 min read 10 practice questions after it
Asked in past papers7 times
  • GATE CSE2026202020132009
  • UGC NET202020192012

Algorithms · Bellman-Ford Algorithm

Bellman-Ford Algorithm: Negative Weight Cycle Detection

Overview

Bellman-Ford is a single-source shortest path algorithm whose special capability is detecting whether a graph contains a negative weight cycle.
This matters because it lets you hand the algorithm a graph with negative weight edges without first checking whether a negative weight cycle exists, unlike Dijkstra.

Key concepts

  • Bellman-Ford algorithm is a single-source shortest path algorithm that has the capability to detect whether a graph contains a negative weight cycle.
  • Given a graph as input, Bellman-Ford outputs one of two verdicts:
    1. "No, I cannot find the shortest path because there is a negative weight cycle" — it reports that a negative weight cycle is present.
    2. "Yes, I can find the shortest path because there are no negative weight cycles" — and in that case it also gives the shortest path weights.
  • Negative weight cycle detection is Bellman-Ford's special feature: it can say whether a negative weight cycle is present in the graph.
  • No pre-checking needed: because of this detection feature, you can give Bellman-Ford a graph containing negative weight edges without worrying beforehand whether a negative weight cycle is present.
  • Contrast with Dijkstra: with Dijkstra you must think about whether a negative weight cycle exists before giving the graph to the algorithm.
  • Disadvantage: Bellman-Ford is slower than Dijkstra — the time complexity of Dijkstra is better.
Graph diagram: Bellman-Ford Algorithm: Negative Weight Cycle Detection
Figure. Bellman-Ford Algorithm: Negative Weight Cycle Detection

How it works

  1. Bellman-Ford accepts any graph as input, including graphs with negative weight edges.
  2. It reports whether a negative weight cycle is reachable from the source (after ∣V∣−1|V|-1 rounds of relaxation, one extra pass that still relaxes an edge signals it). If one exists, it declares that shortest paths cannot be found.
  3. If no negative weight cycle exists, it reports that shortest paths can be found and returns the shortest path weights.
  4. Because of this detection feature, you can hand Bellman-Ford a graph containing negative weight edges without worrying beforehand whether a negative weight cycle is present.
  5. This is unlike Dijkstra: with Dijkstra you must think about whether a negative weight cycle exists before giving the graph to the algorithm.
  6. Dijkstra may fail whenever there are negative weight edges, whereas Bellman-Ford handles negative weight edges and also detects a negative weight cycle reachable from the source (reporting that shortest paths are undefined).

Comparison

AspectDijkstraBellman-Ford
Negative weight edgesMay fail in some casesWorks even with negative weight edges
Negative weight cycle detectionNot capable; must be checked beforehandHas the special capability to detect it
Speed / time complexityBetter (faster)Slower; time complexity is more
Practical useWidely popular and widely used, because most real scenarios have positive weightsUsed when negative weight edges or negative weight cycles are involved

Revision summary

  • Bellman-Ford's special feature: it can say whether a negative weight cycle is present in the graph.
  • If a negative weight cycle is present, Bellman-Ford reports that the shortest path cannot be found; otherwise it gives the shortest path weights.
  • You can give Bellman-Ford a graph with negative weight edges without pre-checking for a negative weight cycle.
  • Dijkstra might fail with negative weight edges (in some cases with negative weight cycles); Bellman-Ford works correctly with negative weight edges, and it detects (rather than solves) a negative weight cycle reachable from the source.
  • Disadvantage of Bellman-Ford compared to Dijkstra: it is slower — the time complexity of Dijkstra is better.
  • In many real-time scenarios the weights are positive: for example, the length/distance of a road when laying down roads, or the delay between two routers when constructing a computer network.
    These values are positive, which is why Dijkstra is widely popular and widely used.
  • Rule of thumb: for positive weights, Dijkstra is preferred; if you have negative weight edges or a negative weight cycle, always go for Bellman-Ford, but its time complexity is more.

Previous-year exam insights

  • Bellman-Ford runs |V|-1 relaxation passes over all |E| edges, then one final pass to test for a negative cycle, giving time complexity Θ(VE)\Theta(VE) (O(VE)O(VE));
    on a complete graph with E = n(n-1)/2 this is Θ(n3)\Theta(n^{3}).
    Asked inGATE 2013UGC NET 2020UGC NET 2012
  • Bellman-Ford detects a negative weight cycle only if it is reachable from the source; it does not find or report negative cycles in components unreachable from the source, and it does not output the cycle itself.
    Asked inGATE 2009
  • Reweighting w'(u,v) = w(u,v) + f(u) - f(v) for any function f preserves shortest paths (the f-terms telescope along a path), and if f is chosen as shortest distances from a source, all reweighted edges become non-negative, allowing Dijkstra to be used.
    Asked inGATE 2020
  • Bellman-Ford's relaxation is order-independent and works on directed graphs; for undirected graphs any negative-weight edge immediately forms a negative cycle (traverse it back and forth), so Bellman-Ford on undirected graphs requires all weights non-negative.
    Asked inGATE 2026
  • After k full relaxation passes, d(u) is at most the weight of a shortest path from s to u using at most k edges (equal to it in the synchronous, previous-round formulation); hence |V|-1 passes suffice for shortest paths when no negative cycle is reachable.
    Asked inGATE 2026
  • Bellman-Ford is a dynamic-programming/greedy-relaxation algorithm (unlike Dijkstra's priority-queue greedy), runs in O(VE)O(VE) even with adjacency lists, and is preferred over Floyd-Warshall (Θ(V3)\Theta(V^{3})) for single-source shortest paths with negative edges.
    Asked inUGC NET 2012UGC NET 2020

Bellman-Ford verdict flow

Shows how Bellman-Ford takes any graph and branches into the two verdicts depending on whether a negative weight cycle exists.

Pages 1–2 of 2