Bellman-Ford Algorithm Explained: Relaxation, Negative Cycles and a Worked Example

Follow every relaxation in a complete Bellman-Ford run, reconstruct the shortest paths, then detect a reachable negative cycle after one edge changes.

KnowledgeGate Team

Exam prep & CS education

Updated 21 Nov 20266 min read
Bellman-Ford Algorithm Explained: Relaxation, Negative Cycles and a Worked Example

Bellman-Ford can look like repeated edge scanning without much logic. The difficult parts are what each scan guarantees, how edge order affects intermediate values, and why a final scan reveals a reachable negative cycle. On the worked graph, edge order delays improvements across four passes; changing B -> A from +2 to +1 creates the reachable negative cycle exposed by the extra scan. In the GATE syllabus, Bellman-Ford connects graph traversal, shortest paths, greedy preconditions and dynamic programming-style relaxation.

What Bellman-Ford solves, and when Dijkstra is not enough

Bellman-Ford takes a directed weighted graph G = (V, E) and source s. It finds d[v], the minimum total edge weight from s to each reachable vertex v. An unreachable vertex keeps distance infinity.

Unlike Dijkstra's greedy method, Bellman-Ford permits negative edges and reports a negative-weight cycle reachable from the source. Dijkstra finalises the smallest tentative distance, so a later negative edge can invalidate that choice; Bellman-Ford keeps labels revisable through repeated full-edge scans. For the cross-algorithm decision, Shortest Path Algorithms: Dijkstra, Bellman-Ford and Floyd-Warshall compares the methods on separate examples. The five-vertex trace below isolates Bellman-Ford's edge order, predecessor reconstruction and extra-scan certificate.

If a reachable negative cycle can lead to a vertex, no finite shortest-path value exists there. Another trip around the cycle reduces the walk cost again.

Relaxation is the one operation to understand

For edge (u, v, w), require d[u] != infinity, then compare d[u] + w with d[v]. If it is smaller, set d[v] = d[u] + w and pred[v] = u. A tie is not an improvement.

If d[A] = 4, edge A -> C weighs -3, and d[C] = 7, the candidate is 4 + (-3) = 1, so d[C] becomes 1. If d[A] were infinity, the edge would do nothing. Never treat infinity plus a negative number as a path value.

After pass i, shortest paths using at most i edges are guaranteed to be represented. Helpful edge order can carry in-place updates farther within one pass, but correctness after at most |V| - 1 passes does not depend on order. The repeated improvement resembles how dynamic programming builds answers from smaller states, though Bellman-Ford need not use a DP table.

Set up the five-vertex worked graph

Let V = {S, A, B, C, D} and source S. Every pass scans this exact directed edge order:

  1. D -> C (-1)

  2. C -> D (4)

  3. C -> B (1)

  4. B -> A (2)

  5. B -> D (3)

  6. A -> C (-3)

  7. S -> B (5)

  8. S -> A (4)

Initially, d[S] = 0; other distances are infinity; predecessors are NIL. Cycle A -> C -> B -> A weighs -3 + 1 + 2 = 0, so negative edges exist without a negative cycle.

Five-vertex directed graph with source S, weighted edges, scan order, and the zero-weight A-C-B-A cycle highlighted in orange.

Run all four passes and account for every change

The columns stay in the order S, A, B, C, D.

Stage

S

A

B

C

D

Initial

0

infinity

infinity

infinity

infinity

After pass 1

0

4

5

infinity

infinity

After pass 2

0

4

5

1

8

After pass 3

0

4

2

1

5

After pass 4

0

4

2

1

5

In pass 1, the first six edges start from infinity. S -> B sets d[B] = 5, then S -> A sets d[A] = 4.

In pass 2, B -> A proposes 5 + 2 = 7, which cannot beat 4. B -> D sets d[D] = 5 + 3 = 8, then A -> C sets d[C] = 4 - 3 = 1.

In pass 3, D -> C offers 8 - 1 = 7, no improvement. C -> D gives 1 + 4 = 5 < 8, and C -> B gives 1 + 1 = 2 < 5. Now B -> A offers 2 + 2 = 4, a tie, while B -> D offers 2 + 3 = 5, another tie. Other candidates fail.

Pass 4 changes nothing, so a changed flag can stop the algorithm. Four is the maximum because |V| = 5, hence |V| - 1 = 4, not because four updates occurred.

Final predecessors are pred[A] = S, pred[C] = A, pred[B] = C, and pred[D] = C. Path S -> A -> C -> B costs 4 - 3 + 1 = 2; S -> A -> C -> D costs 4 - 3 + 4 = 5.

Turn the pass logic into an implementation

Code
dist[source] = 0; all other dist values = INF
all pred values = NIL

repeat at most |V| - 1 times:
    changed = false
    for each edge (u, v, w):
        if dist[u] != INF and dist[u] + w < dist[v]:
            dist[v] = dist[u] + w
            pred[v] = u
            changed = true
    if changed == false: break

scan every edge once more for a reachable relaxation

At most |V| - 1 scans of |E| edges give O(VE) time. Distance and predecessor arrays use O(V) extra space. Early stopping does not change the worst-case bound. Use a numeric type wide enough for path sums and guard against INF + w. Edge order changes intermediate rows, not correct final distances when no reachable negative cycle exists.

Detect a reachable negative cycle with one extra scan

Change only B -> A from +2 to +1. Cycle A -> C -> B -> A now weighs -3 + 1 + 1 = -1, and S -> A makes it reachable.

Using the same in-place scan order gives:

Stage

S

A

B

C

D

After pass 1

0

4

5

infinity

infinity

After pass 2

0

4

5

1

8

After pass 3

0

3

2

0

5

After pass 4

0

2

1

-1

4

On the separate extra scan, C -> B (1) still offers -1 + 1 = 0 < 1. Continued improvement after |V| - 1 passes certifies a reachable negative cycle.

The reachability guard remains essential. Single-source Bellman-Ford reports only negative cycles reachable from S. A disconnected negative cycle does not invalidate source distances elsewhere.

Modified five-vertex graph highlighting the negative cycle A-C-B-A and the extra-scan inequality 0 < 1 that detects it.

How exam-oriented questions test Bellman-Ford

Practice questions ask you to fill a distance row for a stated edge order, decide whether a negative edge is allowed, distinguish a negative edge from a negative cycle, or identify an extra-pass witness. Each form tests whether you can apply the invariant rather than recite the algorithm's name.

Watch for these traps:

  • Running |V| main passes instead of at most |V| - 1.

  • Updating from infinity or counting a tie as relaxation.

  • Looking for any negative edge instead of a negative cycle.

  • Forgetting that edge order affects intermediate rows.

  • Reporting a negative cycle that is unreachable from the source.

  • Choosing Dijkstra merely because currently known distances look non-negative.

Use the GATE Test Series to practise under question-solving conditions, always noting the stated edge order.

Bellman-Ford in the shortest possible revision note

Initialise the source to zero and other distances to infinity. Relax every edge for at most |V| - 1 passes. Stop when a complete pass changes nothing. Scan once more, with the reachability guard, for continued improvement. Use Bellman-Ford when negative weights and reachable negative-cycle reporting matter, accepting O(VE) time.

Reproduce the table without looking. Then change B -> A to +1 and explain the extra-scan inequality aloud. For a structured subject route, continue with GATE Guidance by Sanchit Sir.