CoursifyCoursify

Negative Weight Cycle and the Bellman-Ford Algorithm for Single-Source Shortest Distance

Negative Weight Cycle and the Bellman-Ford Algorithm for Single-Source Shortest Distance

Verified Sources
May 23, 2026

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 -\infty, 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 (u,v)(u,v) with weight w(u,v)w(u,v), if

d[u]+w(u,v)<d[v],d[u] + w(u,v) < d[v],

then the estimate d[v]d[v] is improved.2

A key theoretical fact is that any simple path in a graph with V|V| vertices contains at most V1|V|-1 edges. Therefore, after relaxing all edges exactly V1|V|-1 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 ABCAA \to B \to C \to A has total weight <0< 0, then every route from SS to TT through that cycle can be made cheaper by looping one more time before proceeding to TT.2

Footnotes

  1. Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. 2 3

  2. 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

  3. 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.

  4. DP: Single Source Shortest Paths, The Bellman-Ford Algorithm - University notes discussing directed graphs, negative edges, and why negative cycles invalidate shortest-path computation.

  5. 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

  1. Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass.

  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.

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 3-3. If a path from source ss reaches that cycle, then after going around it kk times, the path cost decreases by 3k3k. Since kk can be arbitrarily large,

limkcost(P)3k=.\lim_{k \to \infty} \text{cost}(P) - 3k = -\infty.

Hence, no minimum finite distance exists.2

This distinction is important:

CaseMeaningBellman-Ford outcome
Negative edges but no negative cycleShortest paths still well-definedComputes distances correctly2
Negative cycle unreachable from sourceDoes not affect source-based answersDistances for reachable vertices still valid
Negative cycle reachable from sourceShortest distances not well-definedAlgorithm reports failure / negative cycle2

Important distance estimate, predecessor, and reachability concepts determine exactly which vertices are affected.2

Footnotes

  1. 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

  2. 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

  3. DP: Single Source Shortest Paths, The Bellman-Ford Algorithm - University notes discussing directed graphs, negative edges, and why negative cycles invalidate shortest-path computation.

  4. Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass.

  5. 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

  1. 1
    Step 1

    Set d[s]=0d[s]=0 for the source vertex ss, and set d[v]=d[v]=\infty for every other vertex vv. Also initialize predecessor π[v]=NIL\pi[v]=\text{NIL} for path reconstruction if needed.2

    Footnotes

    1. 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. 2
    Step 2

    Perform V1|V|-1 passes over all edges. For each directed edge (u,v)(u,v) with weight w(u,v)w(u,v), update d[v]d[v] if d[u]+w(u,v)<d[v]d[u] + w(u,v) < d[v]. This propagates best-known distances outward from the source.3

    Footnotes

    1. 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.

    3. Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection.

  3. 3
    Step 3

    Whenever a relaxation improves d[v]d[v], set π[v]=u\pi[v]=u. The predecessor array forms a shortest-path tree if no reachable negative cycle exists.

    Footnotes

    1. CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles.

  4. 4
    Step 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

    1. 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.

    3. Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection.

  5. 5
    Step 5

    If no further relaxation is possible in the extra pass, output the distance array d[]d[] as the single-source shortest distances. Unreachable vertices remain at \infty.2

    Footnotes

    1. 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.

Standard Bellman-Ford pseudocode

Let G=(V,E)G=(V,E) be a weighted directed graph and ss 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

O(VE)O(|V||E|)

time, because it scans all edges V1|V|-1 times plus one final pass, and uses

O(V)O(|V|)

extra space for distance and predecessor arrays.2

A useful correctness intuition is: after the ii-th full pass, Bellman-Ford has correctly computed shortest distances for all vertices whose shortest path uses at most ii edges.2

Footnotes

  1. CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles.

  2. Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. 2

  3. 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

  1. 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.

Worked example

Consider the weighted directed graph with source SS:

EdgeWeight
SAS \to A4
SBS \to B5
ABA \to B-2
ACA \to C6
BCB \to C3

There is no negative cycle here, although one edge has negative weight.2

Initialization

  • d[S]=0d[S]=0
  • d[A]=d[B]=d[C]=d[A]=d[B]=d[C]=\infty

Pass 1

  • Relax SAS \to A: d[A]=4d[A]=4
  • Relax SBS \to B: d[B]=5d[B]=5
  • Relax ABA \to B: d[B]=min(5,42)=2d[B]=\min(5,4-2)=2
  • Relax ACA \to C: d[C]=10d[C]=10
  • Relax BCB \to C: d[C]=min(10,2+3)=5d[C]=\min(10,2+3)=5

After pass 1:

  • d[S]=0d[S]=0, d[A]=4d[A]=4, d[B]=2d[B]=2, d[C]=5d[C]=5

Pass 2 No value improves further.

Pass 3 No value improves further.

Since V=4|V|=4, we need at most V1=3|V|-1=3 passes.2 Final shortest distances from SS are:

VertexShortest distance from SS
SS0
AA4
BB2
CC5

Now compare that with a graph containing a cycle ABCAA \to B \to C \to A of total weight 1-1. Then after V1|V|-1 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

  1. Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. 2

  2. CHAPTER 25: SINGLE-SOURCE SHORTEST PATHS - CLRS-based treatment of Bellman-Ford correctness, shortest paths, and reachable negative cycles. 2 3 4

  3. 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 O(VE)O(VE).3

Footnotes

  1. 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.

  3. 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 00 and all others to infty\\infty, then relaxes every edge repeatedly for V1|V|-1 rounds.2 Finally, it checks all edges once more; if any distance can still be reduced, a reachable negative weight cycle exists.3

Algorithm:

  1. Set d[s]=0d[s]=0 and d[v]=inftyd[v]=\\infty for all vnesv \\ne s.
  2. Repeat V1|V|-1 times:
    • For every edge (u,v)(u,v), if d[u]+w(u,v)<d[v]d[u] + w(u,v) < d[v], set d[v]=d[u]+w(u,v)d[v] = d[u] + w(u,v).2
  3. For every edge (u,v)(u,v), if d[u]+w(u,v)<d[v]d[u] + w(u,v) < d[v], report negative weight cycle.2
  4. Otherwise, output all distances.

Its time complexity is O(VE)O(|V||E|) and space complexity is O(V)O(|V|).2

Footnotes

  1. Bellman–Ford Algorithm - GeeksforGeeks - Explains Bellman-Ford, relaxation, and negative cycle detection with the extra pass. 2 3 4 5 6 7 8

  2. 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

  3. 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.

  4. Bellman-Ford - finding shortest paths with negative weights - Provides algorithm intuition, complexity, and the criterion for negative-cycle detection. 2 3

Knowledge Check

Question 1 of 4
Q1Single choice

What is a negative weight cycle?

Explore Related Topics

1

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 VV vertices, has exactly V1V-1 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 O(ElogV)O(E \log V); using an adjacency matrix it is O(V2)O(V^2).
  • In the example graph, Prim selects edges AB(2),BC(1),BD(4),CE(6)AB(2), BC(1), BD(4), CE(6), producing an MST of total weight 1313.
2

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 (Cycle\centernot    Deadlock\text{Cycle} \centernot\implies \text{Deadlock}).
  • Thus a cycle is a necessary but not sufficient condition for deadlock in multi‑instance systems.
  • Example: with two instances of R1R1 and R2R2, a cycle P1R2P2R1P1P1 \rightarrow R2 \rightarrow P2 \rightarrow R1 \rightarrow P1 can be broken when another process releases an instance.
3

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.
Chat with Kiro