The Bellman–Ford algorithm computes the shortest paths from one source vertex to every other vertex in a weighted directed graph, even when some edges have negative weights. It also detects negative-weight cycles reachable from the source, situations in which no finite shortest path exists.
Among graph algorithms, Bellman–Ford is the standard choice when Dijkstra’s algorithm cannot be used safely. Named after Richard Bellman and Lester Ford Jr., who published versions in the 1950s, it is a classic example of dynamic programming and runs in O(V·E) time, where V is the number of vertices and E the number of edges.
How the Bellman–Ford algorithm relaxes edges
Bellman–Ford works by repeated edge relaxation. Each vertex keeps a distance estimate, initially zero for the source and infinity for all others. Relaxing an edge from u to v with weight w means checking whether dist[u] + w < dist[v]; if so, the algorithm lowers dist[v] and records u as the predecessor of v.
- Set the source distance to 0 and every other distance to infinity.
- Repeat V − 1 times: relax every edge in the graph once.
- Run one more pass over all edges; if any distance still decreases, a negative cycle is reachable.
- Otherwise, follow predecessor links backward to reconstruct each shortest path.
The bound of V − 1 iterations follows from a simple fact: a shortest path without cycles contains at most V − 1 edges. After pass k, every vertex whose shortest path uses at most k edges already has its correct final distance, so V − 1 passes cover all simple paths.
Complexity and key properties
The cost of Bellman–Ford is dominated by V − 1 full passes over E edges. This makes it markedly slower than Dijkstra’s algorithm on large graphs, but its simple structure brings advantages in correctness and distribution.
| Property | Value | Remark |
|---|---|---|
| Time complexity | O(V·E) | O(V³) on dense graphs |
| Space complexity | O(V) | Distance and predecessor arrays |
| Negative edge weights | Supported | Main advantage over Dijkstra |
| Negative cycle detection | Yes | Extra pass after V − 1 iterations |
| Best case with early exit | O(E) | When no distance changes in a pass |
| Graph type | Directed or undirected | Undirected negative edges form cycles |
A practical early termination check stops the loop when a full pass changes no distance, since further passes cannot change anything either. On many real graphs, this reduces running time considerably, although the worst case remains O(V·E).
Bellman–Ford compared with other shortest-path algorithms
Bellman–Ford is one member of a family of shortest-path methods, and the right choice depends on edge weights, graph density and the number of sources. The comparison below summarizes the trade-offs.
| Algorithm | Scope | Negative weights | Time complexity |
|---|---|---|---|
| Bellman–Ford | Single source | Yes, detects cycles | O(V·E) |
| Dijkstra’s algorithm | Single source | No | O((V + E) log V) with a binary heap |
| Breadth-first search | Single source | Unweighted only | O(V + E) |
| DAG shortest path | Single source, acyclic graphs | Yes | O(V + E) after topological sorting |
| Floyd–Warshall | All pairs | Yes, detects cycles | O(V³) |
| Johnson’s algorithm | All pairs, sparse graphs | Yes | O(V·E + V² log V) |
Dijkstra’s greedy strategy assumes that a settled vertex never improves later, which negative edges can violate. Bellman–Ford makes no such assumption and therefore stays correct. Johnson’s algorithm combines both: it runs Bellman–Ford once to reweight edges to non-negative values, then runs Dijkstra from every vertex.
Detecting negative cycles
A negative cycle is a cycle whose edge weights sum to less than zero. Traversing it repeatedly lowers path cost without limit, so shortest paths to any vertex reachable through it are undefined, not merely very small.
Bellman–Ford detects such cycles with the extra V-th pass. If any edge can still be relaxed, some vertex lies on or behind a negative cycle. To extract the cycle itself, the implementation follows predecessor links from that vertex V times, which guarantees landing inside the cycle, and then walks the loop until it repeats.
| Observation | Interpretation | Action |
|---|---|---|
| No edge relaxes | All distances are final | Report shortest paths |
| An edge relaxes | Reachable negative cycle exists | Report the cycle or mark affected vertices |
| Distance remains infinity | Vertex is unreachable | Report no path |
Practical applications and implementation pitfalls
The best-known application is distance-vector routing. Protocols such as the Routing Information Protocol (RIP) use a distributed form of Bellman–Ford in which each router shares its distance table with neighbors, so no router needs a map of the whole network. Finance offers another use: currency arbitrage detection converts exchange rates to negative logarithms, so a profitable trading loop appears as a negative cycle.
Implementation errors tend to recur in a few places, and most of them produce silently wrong distances rather than crashes.
| Mistake | Consequence | Fix |
|---|---|---|
| Adding to infinity | Integer overflow, false relaxations | Skip edges whose source distance is infinite |
| Only V − 2 passes | Long paths remain wrong | Run exactly V − 1 relaxation passes |
| Missing verification pass | Negative cycles go unnoticed | Always run the extra pass |
| Bidirectional negative edge | Spurious negative cycle | Model undirected negative edges with care |
Distributed versions add their own failure mode, the count-to-infinity problem: after a link failure, routers may keep increasing distances slowly in a loop. Techniques such as split horizon and a maximum hop count of 15 in RIP limit the damage.
In plain words
The Bellman–Ford algorithm finds the cheapest route from one starting point to every other point in a network, even when some connections pay money back instead of costing it.
It works patiently. It checks every connection and updates its best known prices. Then it repeats this until the answers settle. If prices still fall after that, a loop exists that lets someone gain endlessly. In that case, it reports the problem instead of a wrong answer.
A real-world example
A treasury analyst at a mid-sized trading firm monitors exchange rates among twelve currencies, giving 132 directed conversion edges. Each rate r becomes an edge weight of −log(r), so a sequence of trades that multiplies money above 1.0 turns into a path with a negative total weight.
Every minute, a scheduled job runs Bellman–Ford from an arbitrary currency through 11 relaxation passes plus one verification pass. One afternoon, the final pass still relaxes an edge, and following predecessor links reveals a loop from euro to yen to Swiss franc and back. After fees, the loop yields about 0.04 percent per cycle, so the system flags it for review, and the desk confirms that the opportunity disappears within seconds.