- Introduced by
- Richard Bellman and Lester R. Ford
- Year
- 1956
- Type
- algorithm
- Family
- Graph Algorithms
What does it do?
The Bellman-Ford algorithm computes single-source shortest paths in a weighted directed graph whose edge weights may be negative.
It is associated with Richard Bellman, whose 1958 paper 'On a routing problem' treats minimum-travel-time routing between cities, and with Lester R. Ford's earlier work on network flow. Unlike Dijkstra's algorithm, it does not require non-negative weights, and it can report when a negative-weight cycle is reachable from the source.
The intuition behind the method
The algorithm keeps a distance estimate for every vertex, starting at zero for the source and infinity elsewhere. It then repeatedly relaxes every edge (u, v): if travelling through u gives a shorter distance to v, the estimate for v is lowered. After V-1 full passes over the edge list, where V is the number of vertices, the estimates are correct, provided no negative cycle is reachable from the source. A shortest path that visits no vertex twice has at most V-1 edges, and each pass extends the correct prefix of every such path by at least one edge. If a further pass still lowers some estimate, a negative cycle reachable from the source must exist, because otherwise the estimates could not keep decreasing.
History
The algorithm is associated with Richard Bellman, whose 1958 paper 'On a routing problem' addresses finding the path between two cities that minimises travel time, where road times are not simply proportional to distance. Lester R. Ford's 1956 RAND work on network flow theory is traditionally credited with a closely related relaxation approach, which is why the method carries both names.
When to use it
Choose Bellman-Ford when you need shortest paths from a single source and some edge weights may be negative, for example when a cost is negative by construction, or when the question is whether a profitable cycle exists, as in currency arbitrage. For arbitrage, convert each exchange rate r to an edge weight of -ln(r); a cycle whose product of rates exceeds 1 then corresponds to a negative-weight cycle. It also suits a graph given as an explicit edge list when a simple algorithm is preferred. Do not use it when all weights are non-negative, because Dijkstra's algorithm solves the same problem faster. Do not use it on an undirected graph with any negative edge, since such an edge forms a negative cycle when traversed in both directions. If a heuristic estimate of the distance to a single target is available, a heuristic search such as A* may be more efficient.
Limitations and considerations
- Its O(VE) bound is higher than the O((V+E)logV) bound of Dijkstra's algorithm, so it is worth its cost only when negative weights require it
- Every pass scans the full edge list, so the running time depends on E even when few distances change, unless an early-termination check is added
- It offers no heuristic guidance toward a target vertex and cannot focus the search as A* does
- On an undirected graph, any negative edge creates a negative cycle, so shortest paths are undefined and the method does not apply
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Allows negative edge weights, which Dijkstra's algorithm cannot handle correctly in general
- Detects a negative-weight cycle reachable from the source with one extra pass, so it can flag cases where shortest paths are undefined
- Its O(VE) bound applies regardless of the signs of the edge weights
- Consists of a fixed number of sweeps over the edge list with no priority queue, which keeps the implementation short
- Computes distances from one source to all vertices in a single run
Applications
- Currency arbitrage detection, where a negative cycle in a graph with edge weights -ln(rate) indicates a profitable sequence of trades
- Routing with link costs that can be negative or adjusted by signed penalties, where the shortest path must still be well defined
- Route planning on road networks with travel times, the setting addressed in Bellman's 1958 paper
How it works
How it works
Set the distance to the source to zero and the distance to every other vertex to infinity; optionally set every predecessor to null. Vertices that cannot be reached from the source keep distance infinity, and the results are meaningful only for vertices reachable from the source.
Perform V-1 passes. In each pass, for every edge (u, v) with weight w, if distance[u] + w is less than distance[v], lower distance[v] and set the predecessor of v to u. Each pass scans the full edge list rather than selecting the next vertex by priority. Edges whose tail has infinite distance are skipped.
Perform one more pass over all edges. If any distance would still decrease, a negative-weight cycle is reachable from the source, and the reported distances are not valid shortest paths. To recover such a cycle, take a vertex whose distance still decreases, follow predecessor pointers back V times (this lands on a vertex of the cycle), and then keep following predecessors until a vertex repeats.
Common pitfalls
- Stopping after V-1 passes without running the detection pass, so a negative cycle goes unreported and the returned distances are not valid
- Applying the algorithm to an undirected graph with a negative edge, where traversing that edge back and forth forms a negative closed walk
- Reading distances to vertices reachable from a negative cycle as shortest paths, when those values have no finite minimum
- Using Bellman-Ford when all weights are non-negative, which gives the same answer at a higher cost than Dijkstra's algorithm
Frequently asked questions
Why does the algorithm need exactly V-1 iterations?
A shortest path that visits no vertex twice has at most V-1 edges, provided no negative cycle is reachable from the source. Each pass extends the correct prefix of every such path by at least one edge, so V-1 passes are enough. A further pass that still improves a distance indicates a negative cycle reachable from the source.
How can the algorithm detect negative cycles?
After V-1 passes, run one more pass over all edges. If any distance decreases, a negative cycle is reachable from the source. To find the cycle, start at a vertex whose distance still decreases, step back along predecessors V times to be sure of standing on the cycle, then follow predecessors until a vertex repeats.
What is the time complexity and how does it compare to Dijkstra?
Bellman-Ford runs in O(VE) time, where V is the number of vertices and E is the number of edges. Dijkstra runs in O((V+E)logV) with a binary heap. Bellman-Ford is slower but handles negative weights; Dijkstra is faster but requires non-negative weights.
Can Bellman-Ford work on undirected graphs with negative weights?
Not in a meaningful way. An undirected edge with a negative weight can be traversed back and forth, forming a closed walk of negative total weight, so shortest paths are undefined. Undirected graphs with only non-negative weights are better handled by Dijkstra's algorithm.
Related methods
Sources
Bellman, R. (1958). On a routing problem. Quarterly of Applied Mathematics, 16(1), 87-90.
Open DOI referenceFord, L. R. (1956). Network Flow Theory. RAND Corporation Paper P-923.
Open publication reference