Bellman–Ford algorithm
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers. The algorithm was first proposed by , but is instead named after Richard Bellman and Lester Ford Jr., who published it in 1958 and 1956, respectively. Edward F. Moore also published a variation of the algorithm in 1959, and for this reason it is also sometimes called the Bellman–Ford–Moore algorithm. Negative edge weights are found in various applications of graphs. This is why this algorithm is useful. If a graph contains a "negative cycle" (i.e. a cycle whose edges sum to a negative value) that is reachable from the source, then there is no cheapest path: any path that has a point on the negative cycle can be made cheaper by one more walk around the negative cycle. In such a case, the Bellman–Ford algorithm can detect and report the negative cycle.
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is slower than Dijkstra's algorithm for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers. The algorithm was first proposed by , but is instead named after Richard Bellman and Lester Ford Jr., who published it in 1958 and 1956, respectively. Edward F. Moore also published a variation of the algorithm in 1959, and for this reason it is also sometimes called the Bellman–Ford–Moore algorithm. Negative edge weights are found in various applications of graphs. This is why this algorithm is useful. If a graph contains a "negative cycle" (i.e. a cycle whose edges sum to a negative value) that is reachable from the source, then there is no cheapest path: any path that has a point on the negative cycle can be made cheaper by one more walk around the negative cycle. In such a case, the Bellman–Ford algorithm can detect and report the negative cycle. distance := list of size n predecessor := list of size n // Step 1: initialize graph for each vertex v in vertices do // Initialize the distance to all vertices to infinity distance[v] := inf // And having a null predecessor predecessor[v] := null // The distance from the source to itself is zero distance[source] := 0 // Step 2: relax edges repeatedly repeat |V|−1 times: for each edge (u, v) with weight w in edges do if distance[u] + w i \leq |V|-1 , at the end of the i -th iteration, from any vertex , following the predecessor trail recorded in yields a path that has a total weight that is at most , and further, is a lower bound to the length of any path from source to that uses at most edges. Since the longest possible path without a cycle can be |V|-1 edges, the edges must be scanned |V|-1 times to ensure the shortest path has been found for all nodes. A final scan of all the edges is performed and if any distance is updated, then a path of length |V| edges has been found which can only occur if at least one negative cycle exists in the graph. The edge (u, v) that is found in step 3 must be reachable from a negative cycle, but it isn't necessarily part of the cycle itself, which is why it's necessary to follow the path of predecessors backwards until a cycle is detected. The above pseudo-code uses a Boolean array ( visited ) to find a vertex on the cycle, but any cycle finding algorithm can be used to find a vertex on the cycle. A common improvement when implementing the algorithm is to return early when an iteration of step 2 fails to relax any edges, which implies all shortest paths have been found, and therefore there are no negative cycles. In that case, the complexity of the algorithm is reduced from O(|V|\cdot |E|) to O(l \cdot |E|) where l is the maximum length of a shortest path in the graph. Proof. For the base case of induction, consider i=0 and the moment before for loop is executed for the first time. Then, for the source vertex, source.distance = 0 , which is correct. For other vertices u, u.distance = infinity , which is also correct because there is no path from source to u with 0 edges. For the inductive case, we first prove the first part. Consider a moment when a vertex's distance is updated by v.distance := u.distance + uv.weight . By inductive assumption, u.distance is the length of some path from source to u. Then u.distance + uv.weight is the length of the path from source to v that follows the path from source to u and then goes to v. For the second part, consider a shortest path P (there may be more than one) from source to v with at most i edges. Let u be the last vertex before v on this path. Then, the part of the path from source to u is a shortest path from source to u with at most i-1 edges, since if it were not, then there must be some strictly shorter path from source to u with at most i-1 edges, and we could then append the edge uv to this path to obtain a path with at most i edges that is strictly shorter than P—a contradiction. By inductive assumption, u.distance after i−1 iterations is at most the length of this path from source to u. Therefore, uv.weight + u.distance is at most the length of P. In the i th iteration, v.distance gets compared with uv.weight + u.distance , and is set equal to it if uv.weight + u.distance is smaller. Therefore, after i iterations, v.distance is at most the length of P, i.e., the length of the shortest path from source to v that uses at most i edges. If there are no negative-weight cycles, then every shortest path visits each vertex at most once, so at step 3 no further improvements can be made. Conversely, suppose no improvement can be made. Then for any cycle with vertices v[0], ..., v[k−1], v[i].distance Summing around the cycle, the v[i].distance and v[i−1 (mod k)].distance terms cancel, leaving 0 I.e., every cycle has nonnegative weight.
References
- Cormen et al., 4th ed., Problem 22-1, p. 640. Wikipedia revision 1359368078, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "Cormen et al., 4th ed., Problem 22-1, p. 640. Wikipedia revision 1359368078, retrieved 2026-11-18." Cormen et al. 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078. Accessed 24 November 2026.
Chicago "Cormen et al., 4th ed., Problem 22-1, p. 640. Wikipedia revision 1359368078, retrieved 2026-11-18." Cormen et al. 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078.
APA Cormen et al. (2026). Cormen et al., 4th ed., Problem 22-1, p. 640. Wikipedia revision 1359368078, retrieved 2026-11-18. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1359368078
- Hybrid Bellman–Ford–Dijkstra algorithm · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "Hybrid Bellman–Ford–Dijkstra algorithm." Dinitz, Yefim; Itzhak, Rotem (2017-01-01). 2026-11-17. https://www.sciencedirect.com/science/article/pii/S1570866717300011. Accessed 24 November 2026.
Chicago "Hybrid Bellman–Ford–Dijkstra algorithm." Dinitz, Yefim; Itzhak, Rotem (2017-01-01). 2026-11-17. https://www.sciencedirect.com/science/article/pii/S1570866717300011.
APA Dinitz, Yefim; Itzhak, Rotem (2017-01-01). (2026). Hybrid Bellman–Ford–Dijkstra algorithm. Retrieved November 24, 2026, from https://www.sciencedirect.com/science/article/pii/S1570866717300011
- Malkin, Gary S. (November 1998). RIP Version 2 (Report). Internet Engineering Task Force. Wikipedia revision 1359368078, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "Malkin, Gary S. (November 1998). RIP Version 2 (Report). Internet Engineering Task Force. Wikipedia revision 1359368078, retrieved 2026-11-18." Malkin, Gary S. 2026-11-17. https://www.rfc-editor.org/rfc/rfc2453. Accessed 24 November 2026.
Chicago "Malkin, Gary S. (November 1998). RIP Version 2 (Report). Internet Engineering Task Force. Wikipedia revision 1359368078, retrieved 2026-11-18." Malkin, Gary S. 2026-11-17. https://www.rfc-editor.org/rfc/rfc2453.
APA Malkin, Gary S. (2026). Malkin, Gary S. (November 1998). RIP Version 2 (Report). Internet Engineering Task Force. Wikipedia revision 1359368078, retrieved 2026-11-18. Retrieved November 24, 2026, from https://www.rfc-editor.org/rfc/rfc2453
- 关于最短路径的SPFA快速算法 [About the SPFA algorithm] · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "关于最短路径的SPFA快速算法 [About the SPFA algorithm]." Duan, Fanding (1994). 2026-11-17. http://wenku.baidu.com/view/3b8c5d778e9951e79a892705.html. Accessed 24 November 2026.
Chicago "关于最短路径的SPFA快速算法 [About the SPFA algorithm]." Duan, Fanding (1994). 2026-11-17. http://wenku.baidu.com/view/3b8c5d778e9951e79a892705.html.
APA Duan, Fanding (1994). (2026). 关于最短路径的SPFA快速算法 [About the SPFA algorithm]. Retrieved November 24, 2026, from http://wenku.baidu.com/view/3b8c5d778e9951e79a892705.html
- a b c d Bang-Jensen & Gutin (2000) Wikipedia revision 1359368078, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "a b c d Bang-Jensen & Gutin (2000) Wikipedia revision 1359368078, retrieved 2026-11-18." 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078. Accessed 24 November 2026.
Chicago "a b c d Bang-Jensen & Gutin (2000) Wikipedia revision 1359368078, retrieved 2026-11-18." 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078.
APA encyclopegia.org. (2026). a b c d Bang-Jensen & Gutin (2000) Wikipedia revision 1359368078, retrieved 2026-11-18. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1359368078
- a b c Cormen et al. (2022) , Section 22.1. Wikipedia revision 1359368078, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "a b c Cormen et al. (2022) , Section 22.1. Wikipedia revision 1359368078, retrieved 2026-11-18." a b c Cormen et al. 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078. Accessed 24 November 2026.
Chicago "a b c Cormen et al. (2022) , Section 22.1. Wikipedia revision 1359368078, retrieved 2026-11-18." a b c Cormen et al. 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078.
APA a b c Cormen et al. (2026). a b c Cormen et al. (2022) , Section 22.1. Wikipedia revision 1359368078, retrieved 2026-11-18. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1359368078
- Kleinberg & Tardos (2006) . Wikipedia revision 1359368078, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "Kleinberg & Tardos (2006) . Wikipedia revision 1359368078, retrieved 2026-11-18." Kleinberg & Tardos (2006). 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078. Accessed 24 November 2026.
Chicago "Kleinberg & Tardos (2006) . Wikipedia revision 1359368078, retrieved 2026-11-18." Kleinberg & Tardos (2006). 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078.
APA Kleinberg & Tardos (2006). (2026). Kleinberg & Tardos (2006) . Wikipedia revision 1359368078, retrieved 2026-11-18. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1359368078
- Sedgewick (2002) . Wikipedia revision 1359368078, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "Sedgewick (2002) . Wikipedia revision 1359368078, retrieved 2026-11-18." Sedgewick (2002). 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078. Accessed 24 November 2026.
Chicago "Sedgewick (2002) . Wikipedia revision 1359368078, retrieved 2026-11-18." Sedgewick (2002). 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078.
APA Sedgewick (2002). (2026). Sedgewick (2002) . Wikipedia revision 1359368078, retrieved 2026-11-18. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1359368078
- Schrijver (2005) Wikipedia revision 1359368078, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "Schrijver (2005) Wikipedia revision 1359368078, retrieved 2026-11-18." 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078. Accessed 24 November 2026.
Chicago "Schrijver (2005) Wikipedia revision 1359368078, retrieved 2026-11-18." 2026-11-17. https://en.wikipedia.org/w/index.php?oldid=1359368078.
APA encyclopegia.org. (2026). Schrijver (2005) Wikipedia revision 1359368078, retrieved 2026-11-18. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1359368078
- a b See Sedgewick's web exercises for Algorithms , 4th ed., exercises 5 and 12 (retrieved 2013-01-30). Wikipedia revision 1359368078, retrieved 2026-11-18 · retrieved 2013-01-29 · rev 1359368078
Cite this reference
MLA "a b See Sedgewick's web exercises for Algorithms , 4th ed., exercises 5 and 12 (retrieved 2013-01-30). Wikipedia revision 1359368078, retrieved 2026-11-18." a b See Sedgewick's web exercises for Algorithms , 4th ed. 2013-01-29. http://algs4.cs.princeton.edu/44sp/. Accessed 24 November 2026.
Chicago "a b See Sedgewick's web exercises for Algorithms , 4th ed., exercises 5 and 12 (retrieved 2013-01-30). Wikipedia revision 1359368078, retrieved 2026-11-18." a b See Sedgewick's web exercises for Algorithms , 4th ed. 2013-01-29. http://algs4.cs.princeton.edu/44sp/.
APA a b See Sedgewick's web exercises for Algorithms , 4th ed. (2013). a b See Sedgewick's web exercises for Algorithms , 4th ed., exercises 5 and 12 (retrieved 2013-01-30). Wikipedia revision 1359368078, retrieved 2026-11-18. Retrieved November 24, 2026, from http://algs4.cs.princeton.edu/44sp/
- a b Lecture 14 stanford.edu Wikipedia revision 1359368078, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1359368078
Cite this reference
MLA "a b Lecture 14 stanford.edu Wikipedia revision 1359368078, retrieved 2026-11-18." a b Lecture 14 stanford. 2026-11-17. https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/lecture14.pdf. Accessed 24 November 2026.
Chicago "a b Lecture 14 stanford.edu Wikipedia revision 1359368078, retrieved 2026-11-18." a b Lecture 14 stanford. 2026-11-17. https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/lecture14.pdf.
APA a b Lecture 14 stanford. (2026). a b Lecture 14 stanford.edu Wikipedia revision 1359368078, retrieved 2026-11-18. Retrieved November 24, 2026, from https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/lecture14.pdf
- Bellman–Ford algorithm · retrieved 2026-10-19 · rev 1359368078
Cite this reference
MLA "Bellman–Ford algorithm." Wikipedia. 2026-10-19. https://en.wikipedia.org/w/index.php?oldid=1359368078. Accessed 24 November 2026.
Chicago "Bellman–Ford algorithm." Wikipedia. 2026-10-19. https://en.wikipedia.org/w/index.php?oldid=1359368078.
APA Wikipedia. (2026). Bellman–Ford algorithm. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1359368078
Cite this article
MLA
"Bellman–Ford algorithm." encyclopegia.org, 2026, https://encyclopegia.org/wiki/bellman-ford-algorithm. Accessed 24 November 2026.
Chicago
"Bellman–Ford algorithm." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/bellman-ford-algorithm.
APA
encyclopegia.org. (2026). Bellman–Ford algorithm. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/bellman-ford-algorithm
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON
Claims
| Predicate | Value | Source |
|---|---|---|
| publication date | 1958 | Wikipedia |
| publication date | 1959 | Wikipedia |