Home/Graphs/Bellman-Ford
Search algorithmsCTRL K

Bellman-Ford

· shortest paths · negative edges allowed
/graphs/bellman-ford
ROUND
0/ 9
passes over the edge list
RELAXED
0
distances improved
CHECKS
0
edges looked at
NEGATIVE
6
edges with weight below zero
REACHED
1
nodes with a finite distance
edge examinedparent edge · improvedskipped (dashed)final
10 nodes · 15 edges · seed 7 · 52 steps
step 0 / 51
Nodes10
bellman_ford.ts
1
function bellmanFord(graph: Graph, source: Node) {
2
  const distance = new Map(graph.nodes.map((node) => [node, Infinity])); distance.set(source, 0);
3
  for (let round = 1; round < graph.nodes.length; round++) {
4
    let changed = false;
5
    for (const edge of graph.edges) {
6
      const candidate = distance.get(edge.from)! + edge.weight;
7
      if (candidate >= distance.get(edge.to)!) continue;
8
      distance.set(edge.to, candidate); changed = true;
9
    }
10
    if (!changed) break;
11
  }
12
  for (const edge of graph.edges) if (distance.get(edge.from)! + edge.weight < distance.get(edge.to)!) throw new Error("negative cycle");
13
  return distance;
14
}
CURRENT STEP
line 2

Source A gets distance 0, every other node ∞. 6 of the 15 edges are negative: Dijkstra could not be trusted here.

// how it works

Understanding Bellman-Ford

Bellman-Ford · relax every edge, V − 1 times, and negative weights stop being a problem
01

The idea

Bellman-Ford makes no assumption about the order in which nodes are settled. It simply relaxes every edge, in any order, and repeats: after one full pass every shortest path of one edge is correct, after two passes every path of two edges, and so on. V − 1 passes cover every simple path, so the distances are final.

Because it never commits to a node early, negative edge weights are fine. One extra pass tells whether anything still improves; if it does, a negative cycle is reachable and no shortest path exists. The price is V · E work instead of Dijkstra's E log V.

02

The four stages

1
initsource 0, everything else ∞
2
relaxevery edge, in list order
3
repeatV − 1 rounds, or until a round changes nothing
4
checkone more round: any change means a negative cycle
03

Complexity

best
O(E)edges in a lucky order: one round settles everything
average
O(V · E)early exit helps on most graphs
worst
O(V · E)a chain relaxed back to front
space
O(V)distances and parents
04

Versus its siblings

EDGE RELAXATIONS · 1 000 NODES, 3 000 EDGES
dijkstra
6,000
bellman-ford
3M
floyd-warshall
1B
johnson
9M
only Bellman-Ford and Floyd-Warshall accept negative edges
05

Pseudocode

BELLMANFORD(graph, source)
  dist[source] ← 0; every other dist ← ∞
  repeat V − 1 times, or until a round changes nothing
    for each edge from → to with weight: if dist[from] + weight < dist[to]: dist[to] ← dist[from] + weight
  one more round: if anything still improves, a negative cycle exists
06

When to use

Graphs with negative edges: currency arbitrage, difference constraints, potentials for Johnson's algorithm.
Distributed routing, where each node only relaxes its own edges: RIP is Bellman-Ford over the network.
07

Pitfalls

V · E on a big graph is slow; with non-negative weights use Dijkstra.
Skipping the final check silently returns garbage on a negative cycle.
Edge order matters for the early exit: forward-sorted edges settle a DAG in one round.
08

History

Alfonso Shimbel described the relaxation scheme in 1955; Lester Ford Jr. published it in 1956 and Richard Bellman in 1958, and Edward Moore independently in 1957, which is why it is sometimes Bellman-Ford-Moore. Its distributed form ran the ARPANET's routing in the 1970s.

Back to Graphs