VLVisual Learner

Bellman-Ford Algorithm

Time: O(V · E)Space: O(V)Negative weights OK, detects negative cycles
–
Pseudocode
1dist[s] ← 0, every other dist ← ∞
2repeat V − 1 times:
3 for each edge u → v with weight w:
4 if dist[u] + w < dist[v]: dist[v] ← dist[u] + w; prev[v] ← u
5 if nothing changed in this pass: stop early
6for each edge u → v with weight w: // one extra pass
7 if dist[u] + w < dist[v]: there is a negative cycle
Pass
0
Updates
0
edge being relaxedshortest-path treecyclenegative cycle (red)

What is the Bellman-Ford Algorithm?

Bellman-Ford finds shortest paths from one start vertex, like Dijkstra, but it also works when some edges have negative weights, for example a refund, an energy gain or an exchange rate.

Instead of choosing vertices cleverly, it simply relaxes every edge again and again. After pass k, every shortest path that uses at most k edges is correct, so after V − 1 passes all of them are. One extra pass then checks for a negative cycle: a loop whose weights add up to less than zero, which makes "shortest" meaningless.

Relax every edge, V − 1 times. Still improving after that? Negative cycle.

Using the Visualizer

  • • The example graph has negative edges but no negative cycle. Open Edit graph and make an edge more negative (for example 1 → 4 = −12) to create a negative cycle.
  • • The chips on the right are the edges in the fixed order every pass uses. The yellow one is being tried now.
  • • Passes stop early when nothing changes. A detected cycle is drawn in red.
  • • Compare with Dijkstra on a graph without negative edges.

How It Works

1. Start

dist[s] = 0, every other distance ∞.

2. Relax every edge

For each edge u → v: if dist[u] + w < dist[v], update it.

3. Repeat V − 1 times

Each pass lets shortest paths grow by one more edge. Stop early if a pass changes nothing.

4. Check once more

If any edge can still be relaxed, a negative cycle is reachable from the start.

Implementation

// Bellman-Ford: shortest paths from s, negative weights allowed.
// edges = [[u, v, w], ...]; returns null if a negative cycle is reachable.
function bellmanFord(n, edges, s) {
  const dist = Array(n).fill(Infinity);
  const prev = Array(n).fill(-1);
  dist[s] = 0;

  for (let pass = 1; pass <= n - 1; pass++) {
    let changed = false;
    for (const [u, v, w] of edges) {
      if (dist[u] !== Infinity && dist[u] + w < dist[v]) {
        dist[v] = dist[u] + w;
        prev[v] = u;
        changed = true;
      }
    }
    if (!changed) break;               // nothing moved: already final
  }

  // one more pass: any improvement now means a negative cycle
  for (const [u, v, w] of edges) {
    if (dist[u] !== Infinity && dist[u] + w < dist[v]) return null;
  }
  return { dist, prev };
}

Properties & Complexity

Time Complexity

  • • Worst case: O(V · E)
  • • With early stop: often much faster in practice
  • • Compared to Dijkstra: slower, but handles negative weights

Space & Properties

  • • Space: O(V)
  • • Negative edges: yes
  • • Negative cycles: detected, not "solved"

Where It Is Used

  • • Distance-vector routing (RIP) between routers
  • • Currency arbitrage: a negative cycle of −log(rates) is a money-making loop
  • • Johnson's algorithm uses it to reweight edges
  • • Constraint systems of the form x − y ≤ c

Limitations

  • • Slow: O(V·E) is much more than Dijkstra on big graphs
  • • Negative cycles: no shortest path exists through them
  • • Undirected negative edge: is already a negative cycle (go back and forth)