Algorithms And Data Structures Codexery

Bellman–Ford algorithm

Computes shortest paths in graphs with negative edge weights.

Bellman–Ford algorithm

The Bellman–Ford algorithm computes the shortest paths from a single source vertex to every other vertex in a weighted directed graph. While it is slower than Dijkstra’s algorithm for the same problem, it is more versatile because it can handle graphs where some edge weights are negative. Negative edge weights appear in various real-world applications, making this capability useful. The algorithm is named after Richard Bellman and Lester Ford Jr., who published it in 1958 and 1956 respectively, though Ford’s publication was earlier. Edward F. Moore also published a variation in 1959, leading to the occasional name Bellman–Ford–Moore algorithm.

The algorithm works through a process called relaxation, where approximate distances are repeatedly improved until they reach the correct values. Unlike Dijkstra’s algorithm, which uses a priority queue to greedily select the closest unprocessed vertex, Bellman–Ford simply relaxes all edges in the graph a total of |V|−1 times, where |V| is the number of vertices. This repeated relaxation allows the correct distances to propagate gradually, enabling the algorithm to handle negative weights. However, if a graph contains a negative cycle—a cycle whose edges sum to a negative value—that is reachable from the source, no cheapest path exists, as any path can be made cheaper by traversing the cycle again. The Bellman–Ford algorithm can detect and report such negative cycles.

The algorithm runs in O(|V|·|E|) time, where |E| is the number of edges. Its intermediate results and choices among equally short paths depend on the order in which edges are relaxed, but the final distances are invariant. Several improvements exist. Yen’s first improvement reduces the worst-case number of iterations by partitioning edges into two subsets and processing vertices in a specific order. Yen’s second improvement uses a random permutation of vertices to make the worst-case scenario unlikely, reducing the expected number of iterations. Another improvement, developed at Georgetown University, runs in O(|E|·log|V|) time with high probability.

Lore & Background

The Bellman–Ford algorithm computes shortest paths from a single source vertex to all other vertices in a weighted directed graph. It is slower than Dijkstra’s algorithm but more versatile because it can handle graphs with negative edge weights, which appear in various applications. The algorithm is named after Richard Bellman and Lester Ford Jr., who published it in 1958 and 1956, respectively; Edward F. Moore also published a variation in 1959, leading to the alternative name Bellman–Ford–Moore algorithm. It proceeds by relaxation, where overestimates of the true distance are repeatedly replaced by smaller values until the correct distances are found. Unlike Dijkstra’s algorithm, which uses a priority queue to greedily select the closest unprocessed vertex, Bellman–Ford simply relaxes all edges |V|−1 times, where |V| is the number of vertices. Each repetition increases the number of vertices with correctly calculated distances, eventually converging for all vertices. The final distances are independent of the order in which edges are relaxed, though intermediate results may vary. The algorithm runs in O(|V|·|E|) time, where |E| is the number of edges. If a graph contains a negative cycle—a cycle whose edge weights sum to a negative value—reachable from the source, no cheapest path exists because any path can be made cheaper by traversing the cycle again. In such cases, the algorithm can detect and report the negative cycle.

Reader's Guide

The Bellman–Ford algorithm is significant because it extends shortest-path computation to graphs with negative edge weights, which Dijkstra's algorithm cannot handle. Its ability to detect negative cycles is critical in applications such as currency arbitrage and network routing protocols. The algorithm runs in O(|V|·|E|) time, where |V| and |E| are the number of vertices and edges. A common improvement is to return early when an iteration fails to relax any edges, reducing complexity to O(l·|E|) where l is the maximum shortest path length. The algorithm's correctness is proven by induction, showing that after i iterations, distances are at most the length of the shortest path using at most i edges. Its legacy lies in its versatility and foundational role in graph theory and algorithm design.

Did You Know?

Origins and the Curious Question of Attribution

The story behind the Bellman–Ford algorithm's name is a curious footnote in the history of graph theory. Edward F. Despite Shimbel's clear priority in proposing the idea, the convention stuck: two names became the standard label for a technique that, in essence, all three (and Moore) were describing. The episode is a small reminder that in mathematics and computer science, the attribution of an idea to its discoverers is not always as clean as one might expect, and that publication timing, institutional visibility, and the inertia of naming conventions can all shape which names endure in the literature.

The Relaxation Loop and Its Invariant

At its heart, the Bellman–Ford algorithm is a repeated-relaxation procedure. It begins by assigning the source vertex a distance of zero and every other vertex an infinite placeholder, then enters a loop that sweeps across every edge in the graph. During each sweep, if routing through a particular edge yields a shorter tentative distance to the destination, that distance is updated and the predecessor recorded. This sweep is repeated |V| − 1 times, where |V| is the total number of vertices. The reasoning is straightforward: the longest simple path can contain at most |V| − 1 edges, so after that many full passes every vertex's distance must have settled to its true shortest value. A key invariant holds at each stage: after the i-th pass, the predecessor chain from any vertex traces a path whose weight is no greater than the stored distance, and that value is a valid lower bound on any source-to-vertex path using at most i edges. Unlike Dijkstra's method, which uses a priority queue to greedily select the nearest unprocessed vertex, Bellman–Ford simply relaxes all edges in whatever order they appear. Remarkably, while intermediate values and tie-breaking choices can shift with edge ordering, the final distances are always the same.

Taming Negative Weights and Detecting Negative Cycles

The single most important reason the Bellman–Ford algorithm exists alongside Dijkstra's is its ability to cope with negative edge weights. In many real-world graph applications—network cost models, certain scheduling problems, or any setting where a transition can reduce rather than increase accumulated cost—edges carry negative values, and Dijkstra's greedy strategy breaks down. Bellman–Ford, by contrast, handles such weights without special treatment. Beyond merely tolerating negative weights, the algorithm can actively detect a far more dangerous structure: a negative cycle reachable from the source. A negative cycle is a closed loop whose edge weights sum to a negative total. If such a cycle exists, no finite shortest path is meaningful, because a traveler can loop around the cycle indefinitely, each lap shaving more cost off the total. The algorithm exposes this by performing one additional full scan of all edges after the |V| − 1 relaxation passes; if any distance still improves, a negative cycle must be present, and the predecessor pointers can be traced back to identify the offending cycle and report it to the caller.

Complexity and the Practical Trade-off

The price of this added versatility is speed. Bellman–Ford runs in O(|V| · |E|) time, where |V| and |E| denote the numbers of vertices and edges in the graph. For dense networks this can be substantially slower than Dijkstra's algorithm, which exploits a priority queue to achieve better performance on non-negative-weight inputs. The trade-off is deliberate: Bellman–Ford applies to a strictly wider class of graphs, including those with negative weights and even those containing negative cycles, whereas Dijkstra's is restricted to non-negative edge weights. One practical consequence of the algorithm's design is that the final distances are independent of the order in which edges are relaxed within a sweep; only intermediate values and tie-breaking among equally short paths shift with ordering. In practice, a practitioner choosing between the two methods must weigh the expected weight distribution of the input graph against the size of the network. If all weights are non-negative and the graph is large, Dijkstra's is typically the faster choice; if negative weights are present or the graph is small, Bellman–Ford's simpler, more general approach becomes the natural default.

Frequently Asked Questions

What are Bellman–Ford algorithm's powers/role?

Its defining strength is the ability to process negative edge weights, a scenario that stumps Dijkstra's algorithm. It accomplishes this by performing V−1 full rounds of edge relaxation, gradually tightening distance estimates until every shortest path is locked in.

How does Bellman–Ford algorithm's story end?

After the V−1 relaxation passes, it runs one extra sweep to check whether any edge can still be improved, which signals a reachable negative-weight cycle. If such a cycle is found it reports that no finite shortest path exists; otherwise it returns the finalized distance table.

Why is Bellman–Ford algorithm important?

It covers the exact case that faster greedy methods like Dijkstra's cannot handle: graphs where some edges carry negative costs. That makes it a go-to choice in network routing protocols, financial arbitrage detection, and any domain where edge weights are not guaranteed to be non-negative.

More in Algorithms And Data Structures 1-24

Spotted an error? Know more?

This is a living reference — every entry is fact-audited, and reader corrections feed straight into our audit queue. Suggest an edit · See this site's audit record

Comments

Loading…
Open in the interactive codex →