šŸ•·ļø Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 152 (from laksa016)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ā„¹ļø Skipped - page is already crawled

šŸ“„
INDEXABLE
āœ…
CRAWLED
28 days ago
šŸ¤–
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH1 months ago (distributed domain, exempt)
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
Last Crawled2026-03-11 09:15:20 (28 days ago)
First Indexed2013-08-23 23:13:08 (12 years ago)
HTTP Status Code200
Meta TitleBellman–Ford algorithm - Wikipedia
Meta Descriptionnull
Meta Canonicalnull
Boilerpipe Text
From Wikipedia, the free encyclopedia Bellman–Ford algorithm Class Single-source shortest path problem (for weighted directed graphs) Data structure Graph Worst-case performance Best-case performance Worst-case space complexity 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 . [ 1 ] 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. [ 2 ] The algorithm was first proposed by Alfonso ShimbelĀ ( 1955 ), but is instead named after Richard Bellman and Lester Ford Jr. , who published it in 1958 and 1956 , respectively. [ 3 ] 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 . [ 1 ] Negative edge weights are found in various applications of graphs. This is why this algorithm is useful. [ 4 ] 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. [ 1 ] [ 5 ] In this example graph, assuming that A is the source and edges are processed in the worst order, from right to left, it requires the full | V |āˆ’1 or 4 iterations for the distance estimates to converge. Conversely, if the edges are processed in the best order, from left to right, the algorithm converges in a single iteration. Like Dijkstra's algorithm , Bellman–Ford proceeds by relaxation , in which approximations to the correct distance are replaced by better ones until they eventually reach the solution. In both algorithms, the approximate distance to each vertex is always an overestimate of the true distance, and is replaced by the minimum of its old value and the length of a newly found path. [ 6 ] However, Dijkstra's algorithm uses a priority queue to greedily select the closest vertex that has not yet been processed, and performs this relaxation process on all of its outgoing edges; by contrast, the Bellman–Ford algorithm simply relaxes all the edges, and does this times, where is the number of vertices in the graph. [ 6 ] In each of these repetitions, the number of vertices with correctly calculated distances grows, from which it follows that eventually all vertices will have their correct distances. This method allows the Bellman–Ford algorithm to be applied to a wider class of inputs than Dijkstra's algorithm. The intermediate answers and the choices among equally short paths depend on the order of edges relaxed, but the final distances remain the same. [ 6 ] Bellman–Ford runs in time , where and are the number of vertices and edges respectively. function BellmanFord( list vertices, list edges, vertex source) is // This implementation takes in a graph, represented as // lists of vertices (represented as integers [0..n-1]) and // edges, and fills two arrays (distance and predecessor) // holding the shortest path from the source to each vertex 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 < distance[v] then distance[v]Ā := distance[u] + w predecessor[v]Ā := u // Step 3: check for negative-weight cycles for each edge (u, v) with weight w in edges do if distance[u] + w < distance[v] then predecessor[v]Ā := u // A negative cycle exists; // find a vertex on the cycle visitedĀ := list of size n initialized with false visited[v]Ā := true while not visited[u] do visited[u]Ā := true uĀ := predecessor[u] // u is a vertex in a negative cycle, // find the cycle itself ncycleĀ := [u] vĀ := predecessor[u] while vĀ != u do ncycleĀ := concatenate([v], ncycle) vĀ := predecessor[v] error "Graph contains a negative-weight cycle", ncycle return distance, predecessor Simply put, the algorithm initializes the distance to the source to 0 and all other nodes to infinity. Then for all edges, if the distance to the destination can be shortened by taking the edge, the distance is updated to the new lower value. The core of the algorithm is a loop that scans across all edges at every loop. For every , at the end of the -th iteration, from any vertex v , following the predecessor trail recorded in predecessor yields a path that has a total weight that is at most distance[v] , and further, distance[v] is a lower bound to the length of any path from source to v that uses at most i edges. Since the longest possible path without a cycle can be edges, the edges must be scanned 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 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 to where is the maximum length of a shortest path in the graph. Proof of correctness [ edit ] The correctness of the algorithm can be shown by induction : [ 2 ] [ 7 ] Lemma . After i repetitions of for loop, if Distance( u ) is not infinity, it is equal to the length of some path from s to u ; and if there is a path from s to u with at most i edges, then Distance(u) is at most the length of the shortest path from s to u with at most i edges. 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 <= v[i-1 (mod k)].distance + v[i-1 (mod k)]v[i].weight Summing around the cycle, the v [ i ].distance and v [ i āˆ’1 (mod k )].distance terms cancel, leaving 0 <= sum from 1 to k of v[i-1 (mod k)]v[i].weight I.e., every cycle has nonnegative weight. Finding negative cycles [ edit ] When the algorithm is used to find shortest paths, the existence of negative cycles is a problem, preventing the algorithm from finding a correct answer. However, since it terminates upon finding a negative cycle, the Bellman–Ford algorithm can be used for applications in which this is the target to be sought – for example in cycle-cancelling techniques in network flow analysis. [ 1 ] Applications in routing [ edit ] A distributed variant of the Bellman–Ford algorithm is used in distance-vector routing protocols , for example the Routing Information Protocol (RIP). [ 8 ] The algorithm is distributed because it involves a number of nodes (routers) within an Autonomous system (AS) , a collection of IP networks typically owned by an ISP. It consists of the following steps: Each node calculates the distances between itself and all other nodes within the AS and stores this information as a table. Each node sends its table to all neighboring nodes. When a node receives distance tables from its neighbors, it calculates the shortest routes to all other nodes and updates its own table to reflect any changes. The main disadvantages of the Bellman–Ford algorithm in this setting are as follows: It does not scale well. Changes in network topology are not reflected quickly since updates are spread node-by-node. Count to infinity if link or node failures render a node unreachable from some set of other nodes, those nodes may spend forever gradually increasing their estimates of the distance to it, and in the meantime there may be routing loops. The Bellman–Ford algorithm may be improved in practice (although not in the worst case) by the observation that, if an iteration of the main loop of the algorithm terminates without making any changes, the algorithm can be immediately terminated, as subsequent iterations will not make any more changes. With this early termination condition, the main loop may in some cases use many fewer than | V |Ā āˆ’Ā 1 iterations, even though the worst case of the algorithm remains unchanged. The following improvements all maintain the worst-case time complexity. A variation of the Bellman–Ford algorithm described by Moore (1959) , reduces the number of relaxation steps that need to be performed within each iteration of the algorithm. If a vertex v has a distance value that has not changed since the last time the edges out of v were relaxed, then there is no need to relax the edges out of v a second time. In this way, as the number of vertices with correct distance values grows, the number whose outgoing edges that need to be relaxed in each iteration shrinks, leading to a constant-factor savings in time for dense graphs . This variation can be implemented by keeping a collection of vertices whose outgoing edges need to be relaxed, removing a vertex from this collection when its edges are relaxed, and adding to the collection any vertex whose distance value is changed by a relaxation step. In China, this algorithm was popularized by Fanding Duan, who rediscovered it in 1994, as the "shortest path faster algorithm". [ 9 ] Yen (1970) described another improvement to the Bellman–Ford algorithm. His improvement first assigns some arbitrary linear order on all vertices and then partitions the set of all edges into two subsets. The first subset, E f , contains all edges ( v i , v j ) such that i < j ; the second, E b , contains edges ( v i , v j ) such that i > j . Each vertex is visited in the order v 1 , v 2 , ..., v | V | , relaxing each outgoing edge from that vertex in E f . Each vertex is then visited in the order v | V | , v | V |āˆ’1 , ..., v 1 , relaxing each outgoing edge from that vertex in E b . Each iteration of the main loop of the algorithm, after the first one, adds at least two edges to the set of edges whose relaxed distances match the correct shortest path distances: one from E f and one from E b . This modification reduces the worst-case number of iterations of the main loop of the algorithm from | V |Ā āˆ’Ā 1 to . [ 10 ] [ 11 ] Another improvement, by Bannister & Eppstein (2012) , replaces the arbitrary linear order of the vertices used in Yen's second improvement by a random permutation . This change makes the worst case for Yen's improvement (in which the edges of a shortest path strictly alternate between the two subsets E f and E b ) very unlikely to happen. With a randomly permuted vertex ordering, the expected number of iterations needed in the main loop is at most . [ 11 ] Fineman (2024) , at Georgetown University , created an improved algorithm that with high probability runs in time . Here, the is a variant of big O notation that hides logarithmic factors. ^ a b c d Bang-Jensen & Gutin (2000) ^ a b Lecture 14 stanford.edu ^ Schrijver (2005) ^ Sedgewick (2002) . ^ Kleinberg & Tardos (2006) . ^ a b c Cormen et al. (2022) , Section 22.1. ^ Dinitz, Yefim; Itzhak, Rotem (2017-01-01). "Hybrid Bellman–Ford–Dijkstra algorithm" . Journal of Discrete Algorithms . 42 : 35– 44. doi : 10.1016/j.jda.2017.01.001 . ISSN Ā  1570-8667 . ^ Malkin, Gary S. (November 1998). RIP Version 2 (Report). Internet Engineering Task Force. ^ Duan, Fanding (1994). "å…³äŗŽęœ€ēŸ­č·Æå¾„ēš„SPFAåæ«é€Ÿē®—ę³• [About the SPFA algorithm]" . Journal of Southwest Jiaotong University . 29 (2): 207– 212. ^ Cormen et al., 4th ed., Problem 22-1, p. 640. ^ a b See Sedgewick's web exercises for Algorithms , 4th ed., exercises 5 and 12 (retrieved 2013-01-30). Shimbel, A. (1955). Structure in communication nets . Proceedings of the Symposium on Information Networks. New York, New York: Polytechnic Press of the Polytechnic Institute of Brooklyn. pp.Ā  199– 203. Bellman, Richard (1958). "On a routing problem" . Quarterly of Applied Mathematics . 16 : 87– 90. doi : 10.1090/qam/102435 . MR Ā  0102435 . Ford, Lester R. Jr. (August 14, 1956). Network Flow Theory . Paper P-923. Santa Monica, California: RAND Corporation. Moore, Edward F. (1959). The shortest path through a maze . Proc. Internat. Sympos. Switching Theory 1957, Part II. Cambridge, Massachusetts: Harvard Univ. Press. pp.Ā  285– 292. MR Ā  0114710 . Yen, Jin Y. (1970). "An algorithm for finding shortest routes from all source nodes to a given destination in general networks" . Quarterly of Applied Mathematics . 27 (4): 526– 530. doi : 10.1090/qam/253822 . MR Ā  0253822 . Bannister, M. J.; Eppstein, D. (2012). "Randomized speedup of the Bellman–Ford algorithm". Analytic Algorithmics and Combinatorics (ANALCO12), Kyoto, Japan . pp.Ā  41– 47. arXiv : 1111.5414 . doi : 10.1137/1.9781611973020.6 . Fineman, Jeremy T. (2024). "Single-source shortest paths with negative real weights in time". In Mohar, Bojan; Shinkar, Igor; O'Donnell, Ryan (eds.). Proceedings of the 56th Annual ACM Symposium on Theory of Computing, STOC 2024, Vancouver, BC, Canada, June 24–28, 2024 . Association for Computing Machinery. pp.Ā  3– 14. arXiv : 2311.02520 . doi : 10.1145/3618260.3649614 . Ford, L. R. Jr. ; Fulkerson, D. R. (1962). "A shortest chain algorithm". Flows in Networks . Princeton University Press. pp.Ā  130– 134. Bang-Jensen, JĆørgen; Gutin, Gregory (2000). "Section 2.3.4: The Bellman-Ford-Moore algorithm". Digraphs: Theory, Algorithms and Applications (FirstĀ ed.). Springer. ISBN Ā  978-1-84800-997-4 . Schrijver, Alexander (2005). "On the history of combinatorial optimization (till 1960)" (PDF) . Handbook of Discrete Optimization . Elsevier: 1– 68. Cormen, Thomas H. ; Leiserson, Charles E. ; Rivest, Ronald L. ; Stein, Clifford (2022) [1990]. Introduction to Algorithms (4thĀ ed.). MIT Press and McGraw-Hill. ISBN Ā  0-262-04630-X . Section 22.1: The Bellman–Ford algorithm, pp.Ā 612–616. Problem 22–1, p.Ā 640. Heineman, George T.; Pollice, Gary; Selkow, Stanley (2008). "Chapter 6: Graph Algorithms". Algorithms in a Nutshell . O'Reilly Media . pp.Ā  160– 164. ISBN Ā  978-0-596-51624-6 . Kleinberg, Jon ; Tardos, Ɖva (2006). Algorithm Design . New York: Pearson Education, Inc. Sedgewick, Robert (2002). "Section 21.7: Negative Edge Weights". Algorithms in Java (3rdĀ ed.). Addison-Wesley. ISBN Ā  0-201-36121-3 . Archived from the original on 2008-05-31 . Retrieved 2007-05-28 .
Markdown
[Jump to content](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#bodyContent) Main menu Main menu move to sidebar hide Navigation - [Main page](https://en.wikipedia.org/wiki/Main_Page "Visit the main page [z]") - [Contents](https://en.wikipedia.org/wiki/Wikipedia:Contents "Guides to browsing Wikipedia") - [Current events](https://en.wikipedia.org/wiki/Portal:Current_events "Articles related to current events") - [Random article](https://en.wikipedia.org/wiki/Special:Random "Visit a randomly selected article [x]") - [About Wikipedia](https://en.wikipedia.org/wiki/Wikipedia:About "Learn about Wikipedia and how it works") - [Contact us](https://en.wikipedia.org/wiki/Wikipedia:Contact_us "How to contact Wikipedia") Contribute - [Help](https://en.wikipedia.org/wiki/Help:Contents "Guidance on how to use and edit Wikipedia") - [Learn to edit](https://en.wikipedia.org/wiki/Help:Introduction "Learn how to edit Wikipedia") - [Community portal](https://en.wikipedia.org/wiki/Wikipedia:Community_portal "The hub for editors") - [Recent changes](https://en.wikipedia.org/wiki/Special:RecentChanges "A list of recent changes to Wikipedia [r]") - [Upload file](https://en.wikipedia.org/wiki/Wikipedia:File_upload_wizard "Add images or other media for use on Wikipedia") - [Special pages](https://en.wikipedia.org/wiki/Special:SpecialPages "A list of all special pages [q]") [![](https://en.wikipedia.org/static/images/icons/enwiki-25.svg) ![Wikipedia](https://en.wikipedia.org/static/images/mobile/copyright/wikipedia-wordmark-en-25.svg) ![The Free Encyclopedia](https://en.wikipedia.org/static/images/mobile/copyright/wikipedia-tagline-en-25.svg)](https://en.wikipedia.org/wiki/Main_Page) [Search](https://en.wikipedia.org/wiki/Special:Search "Search Wikipedia [f]") Appearance - [Donate](https://donate.wikimedia.org/?wmf_source=donate&wmf_medium=sidebar&wmf_campaign=en.wikipedia.org&uselang=en) - [Create account](https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&returnto=Bellman%E2%80%93Ford+algorithm "You are encouraged to create an account and log in; however, it is not mandatory") - [Log in](https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Bellman%E2%80%93Ford+algorithm "You're encouraged to log in; however, it's not mandatory. [o]") Personal tools - [Donate](https://donate.wikimedia.org/?wmf_source=donate&wmf_medium=sidebar&wmf_campaign=en.wikipedia.org&uselang=en) - [Create account](https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&returnto=Bellman%E2%80%93Ford+algorithm "You are encouraged to create an account and log in; however, it is not mandatory") - [Log in](https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Bellman%E2%80%93Ford+algorithm "You're encouraged to log in; however, it's not mandatory. [o]") ## Contents move to sidebar hide - [(Top)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm) - [1 Algorithm](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#Algorithm) - [2 Proof of correctness](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#Proof_of_correctness) - [3 Finding negative cycles](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#Finding_negative_cycles) - [4 Applications in routing](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#Applications_in_routing) - [5 Improvements](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#Improvements) - [6 Notes](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#Notes) - [7 References](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#References) Toggle References subsection - [7\.1 Original sources](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#Original_sources) - [7\.2 Secondary sources](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#Secondary_sources) Toggle the table of contents # Bellman–Ford algorithm 28 languages - [Ų§Ł„Ų¹Ų±ŲØŁŠŲ©](https://ar.wikipedia.org/wiki/%D8%AE%D9%88%D8%A7%D8%B1%D8%B2%D9%85%D9%8A%D8%A9_%D8%A8%D9%84%D9%85%D8%A7%D9%86_%D9%81%D9%88%D8%B1%D8%AF "Ų®ŁˆŲ§Ų±Ų²Ł…ŁŠŲ© بلمان فورد – Arabic") - [Š‘ŃŠŠ»Š³Š°Ń€ŃŠŗŠø](https://bg.wikipedia.org/wiki/%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D1%8A%D0%BC_%D0%BD%D0%B0_%D0%91%D0%B5%D0%BB%D0%BC%D0%B0%D0%BD-%D0%A4%D0%BE%D1%80%D0%B4 "ŠŠ»Š³Š¾Ń€ŠøŃ‚ŃŠŠ¼ на Белман-ФорГ – Bulgarian") - [CatalĆ ](https://ca.wikipedia.org/wiki/Algorisme_de_Bellman-Ford "Algorisme de Bellman-Ford – Catalan") - [ČeÅ”tina](https://cs.wikipedia.org/wiki/Bellman%C5%AFv%E2%80%93Ford%C5%AFv_algoritmus "BellmanÅÆv–FordÅÆv algoritmus – Czech") - [Deutsch](https://de.wikipedia.org/wiki/Bellman-Ford-Algorithmus "Bellman-Ford-Algorithmus – German") - [EspaƱol](https://es.wikipedia.org/wiki/Algoritmo_de_Bellman-Ford "Algoritmo de Bellman-Ford – Spanish") - [فارسی](https://fa.wikipedia.org/wiki/%D8%A7%D9%84%DA%AF%D9%88%D8%B1%DB%8C%D8%AA%D9%85_%D8%A8%D9%84%D9%85%D9%86%E2%80%93%D9%81%D9%88%D8%B1%D8%AF "Ų§Ł„ŚÆŁˆŲ±ŪŒŲŖŁ… ŲØŁ„Ł…Ł†ā€“ŁŁˆŲ±ŲÆ – Persian") - [FranƧais](https://fr.wikipedia.org/wiki/Algorithme_de_Bellman-Ford "Algorithme de Bellman-Ford – French") - [עברית](https://he.wikipedia.org/wiki/%D7%90%D7%9C%D7%92%D7%95%D7%A8%D7%99%D7%AA%D7%9D_%D7%91%D7%9C%D7%9E%D7%9F-%D7%A4%D7%95%D7%A8%D7%93 "××œ×’×•×Ø×™×Ŗ× בלמן-פורד – Hebrew") - [ą¤¹ą¤æą¤Øą„ą¤¦ą„€](https://hi.wikipedia.org/wiki/%E0%A4%AC%E0%A5%87%E0%A4%B2%E0%A4%AE%E0%A5%88%E0%A4%A8-%E0%A4%AB%E0%A5%8B%E0%A4%B0%E0%A5%8D%E0%A4%A1_%E0%A4%8F%E0%A4%B2%E0%A5%8D%E0%A4%97%E0%A5%8B%E0%A4%B0%E0%A4%BF%E0%A4%A5%E0%A5%8D%E0%A4%AE "ą¤¬ą„‡ą¤²ą¤®ą„ˆą¤Ø-ą¤«ą„‹ą¤°ą„ą¤” ą¤ą¤²ą„ą¤—ą„‹ą¤°ą¤æą¤„ą„ą¤® – Hindi") - [Magyar](https://hu.wikipedia.org/wiki/Bellman%E2%80%93Ford-algoritmus "Bellman–Ford-algoritmus – Hungarian") - [Bahasa Indonesia](https://id.wikipedia.org/wiki/Algoritma_Bellman%E2%80%93Ford "Algoritma Bellman–Ford – Indonesian") - [Italiano](https://it.wikipedia.org/wiki/Algoritmo_di_Bellman-Ford "Algoritmo di Bellman-Ford – Italian") - [ę—„ęœ¬čŖž](https://ja.wikipedia.org/wiki/%E3%83%99%E3%83%AB%E3%83%9E%E3%83%B3%E2%80%93%E3%83%95%E3%82%A9%E3%83%BC%E3%83%89%E6%B3%95 "ćƒ™ćƒ«ćƒžćƒ³ā€“ćƒ•ć‚©ćƒ¼ćƒ‰ę³• – Japanese") - [įƒ„įƒįƒ įƒ—įƒ£įƒšįƒ˜](https://ka.wikipedia.org/wiki/%E1%83%91%E1%83%94%E1%83%9A%E1%83%9B%E1%83%90%E1%83%9C-%E1%83%A4%E1%83%9D%E1%83%A0%E1%83%93%E1%83%98%E1%83%A1_%E1%83%90%E1%83%9A%E1%83%92%E1%83%9D%E1%83%A0%E1%83%98%E1%83%97%E1%83%9B%E1%83%98 "įƒ‘įƒ”įƒšįƒ›įƒįƒœ-įƒ¤įƒįƒ įƒ“įƒ˜įƒ” įƒįƒšįƒ’įƒįƒ įƒ˜įƒ—įƒ›įƒ˜ – Georgian") - [ķ•œźµ­ģ–“](https://ko.wikipedia.org/wiki/%EB%B2%A8%EB%A8%BC-%ED%8F%AC%EB%93%9C_%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98 "벨먼-ķ¬ė“œ ģ•Œź³ ė¦¬ģ¦˜ – Korean") - [LatvieÅ”u](https://lv.wikipedia.org/wiki/Belmana%E2%80%94Forda_algoritms "Belmana—Forda algoritms – Latvian") - [МакеГонски](https://mk.wikipedia.org/wiki/%D0%91%D0%B5%D0%BB%D0%BC%D0%B0%D0%BD-%D0%A4%D0%BE%D1%80%D0%B4%D0%BE%D0%B2_%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%B0%D0%BC "Белман-ФорГов алгоритам – Macedonian") - [Nederlands](https://nl.wikipedia.org/wiki/Algoritme_van_Bellman-Ford "Algoritme van Bellman-Ford – Dutch") - [Polski](https://pl.wikipedia.org/wiki/Algorytm_Bellmana-Forda "Algorytm Bellmana-Forda – Polish") - [PortuguĆŖs](https://pt.wikipedia.org/wiki/Algoritmo_de_Bellman-Ford "Algoritmo de Bellman-Ford – Portuguese") - [Русский](https://ru.wikipedia.org/wiki/%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%91%D0%B5%D0%BB%D0%BB%D0%BC%D0%B0%D0%BD%D0%B0_%E2%80%94_%D0%A4%D0%BE%D1%80%D0%B4%D0%B0 "Алгоритм Беллмана — ФорГа – Russian") - [Simple English](https://simple.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm "Bellman–Ford algorithm – Simple English") - [Дрпски / srpski](https://sr.wikipedia.org/wiki/%D0%91%D0%B5%D0%BB%D0%BC%D0%B0%D0%BD-%D0%A4%D0%BE%D1%80%D0%B4%D0%BE%D0%B2_%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%B0%D0%BC "Белман-ФорГов алгоритам – Serbian") - [ไทย](https://th.wikipedia.org/wiki/%E0%B8%82%E0%B8%B1%E0%B9%89%E0%B8%99%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%A7%E0%B8%B4%E0%B8%98%E0%B8%B5%E0%B8%82%E0%B8%AD%E0%B8%87%E0%B9%80%E0%B8%9A%E0%B8%A5%E0%B9%81%E0%B8%A1%E0%B8%99-%E0%B8%9F%E0%B8%AD%E0%B8%A3%E0%B9%8C%E0%B8%94 "ąø‚ąø±ą¹‰ąø™ąø•ąø­ąø™ąø§ąø“ąø˜ąøµąø‚ąø­ąø‡ą¹€ąøšąø„ą¹ąø”ąø™-ąøŸąø­ąø£ą¹Œąø” – Thai") - [Š£ŠŗŃ€Š°Ń—Š½ŃŃŒŠŗŠ°](https://uk.wikipedia.org/wiki/%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%91%D0%B5%D0%BB%D0%BB%D0%BC%D0%B0%D0%BD%D0%B0_%E2%80%94_%D0%A4%D0%BE%D1%80%D0%B4%D0%B0 "Алгоритм Беллмана — ФорГа – Ukrainian") - [Tiįŗæng Việt](https://vi.wikipedia.org/wiki/Thu%E1%BA%ADt_to%C3%A1n_Bellman%E2%80%93Ford "Thuįŗ­t toĆ”n Bellman–Ford – Vietnamese") - [äø­ę–‡](https://zh.wikipedia.org/wiki/%E8%B4%9D%E5%B0%94%E6%9B%BC-%E7%A6%8F%E7%89%B9%E7%AE%97%E6%B3%95 "č“å°”ę›¼-ē¦ē‰¹ē®—ę³• – Chinese") [Edit links](https://www.wikidata.org/wiki/Special:EntityPage/Q816022#sitelinks-wikipedia "Edit interlanguage links") - [Article](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm "View the content page [c]") - [Talk](https://en.wikipedia.org/wiki/Talk:Bellman%E2%80%93Ford_algorithm "Discuss improvements to the content page [t]") English - [Read](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm) - [Edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit "Edit this page [e]") - [View history](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=history "Past revisions of this page [h]") Tools Tools move to sidebar hide Actions - [Read](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm) - [Edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit "Edit this page [e]") - [View history](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=history) General - [What links here](https://en.wikipedia.org/wiki/Special:WhatLinksHere/Bellman%E2%80%93Ford_algorithm "List of all English Wikipedia pages containing links to this page [j]") - [Related changes](https://en.wikipedia.org/wiki/Special:RecentChangesLinked/Bellman%E2%80%93Ford_algorithm "Recent changes in pages linked from this page [k]") - [Upload file](https://en.wikipedia.org/wiki/Wikipedia:File_Upload_Wizard "Upload files [u]") - [Permanent link](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&oldid=1326384451 "Permanent link to this revision of this page") - [Page information](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=info "More information about this page") - [Cite this page](https://en.wikipedia.org/w/index.php?title=Special:CiteThisPage&page=Bellman%E2%80%93Ford_algorithm&id=1326384451&wpFormIdentifier=titleform "Information on how to cite this page") - [Get shortened URL](https://en.wikipedia.org/w/index.php?title=Special:UrlShortener&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBellman%25E2%2580%2593Ford_algorithm) Print/export - [Download as PDF](https://en.wikipedia.org/w/index.php?title=Special:DownloadAsPdf&page=Bellman%E2%80%93Ford_algorithm&action=show-download-screen "Download this page as a PDF file") - [Printable version](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&printable=yes "Printable version of this page [p]") In other projects - [Wikimedia Commons](https://commons.wikimedia.org/wiki/Category:Bellman%E2%80%93Ford_algorithm) - [Wikidata item](https://www.wikidata.org/wiki/Special:EntityPage/Q816022 "Structured data on this page hosted by Wikidata [g]") Appearance move to sidebar hide From Wikipedia, the free encyclopedia Algorithm for finding the shortest paths in graphs | | | |---|---| | [![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Bellman%E2%80%93Ford_algorithm_example.gif/250px-Bellman%E2%80%93Ford_algorithm_example.gif)](https://en.wikipedia.org/wiki/File:Bellman%E2%80%93Ford_algorithm_example.gif) | | | Class | [Single-source shortest path problem](https://en.wikipedia.org/wiki/Single-source_shortest_path_problem "Single-source shortest path problem") (for weighted directed graphs) | | Data structure | [Graph](https://en.wikipedia.org/wiki/Graph_\(data_structure\) "Graph (data structure)") | | [Worst-case](https://en.wikipedia.org/wiki/Best,_worst_and_average_case "Best, worst and average case") [performance](https://en.wikipedia.org/wiki/Time_complexity "Time complexity") | Θ ( \| V \| \| E \| ) {\\displaystyle \\Theta (\|V\|\|E\|)} ![{\\displaystyle \\Theta (\|V\|\|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/0465422c67bedf2d1659571f5797b3c9c54ed9ad) | The **Bellman–Ford algorithm** is an [algorithm](https://en.wikipedia.org/wiki/Algorithm "Algorithm") that computes [shortest paths](https://en.wikipedia.org/wiki/Shortest_path "Shortest path") from a single source [vertex](https://en.wikipedia.org/wiki/Vertex_\(graph_theory\) "Vertex (graph theory)") to all of the other vertices in a [weighted digraph](https://en.wikipedia.org/wiki/Weighted_digraph "Weighted digraph").[\[1\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Bang-1) It is slower than [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm "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.[\[2\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-web.stanford.edu-2) The algorithm was first proposed by Alfonso Shimbel ([1955](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFShimbel1955)), but is instead named after [Richard Bellman](https://en.wikipedia.org/wiki/Richard_Bellman "Richard Bellman") and [Lester Ford Jr.](https://en.wikipedia.org/wiki/L._R._Ford_Jr. "L. R. Ford Jr."), who published it in [1958](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFBellman1958) and [1956](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFFord1956), respectively.[\[3\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Schrijver-3) [Edward F. Moore](https://en.wikipedia.org/wiki/Edward_F._Moore "Edward F. Moore") also published a variation of the algorithm in [1959](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFMoore1959), and for this reason it is also sometimes called the **Bellman–Ford–Moore algorithm**.[\[1\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Bang-1) Negative edge weights are found in various applications of graphs. This is why this algorithm is useful.[\[4\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTESedgewick2002-4) If a graph contains a "negative cycle" (i.e. a [cycle](https://en.wikipedia.org/wiki/Cycle_\(graph_theory\) "Cycle (graph theory)") 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](https://en.wikipedia.org/wiki/Walk_\(graph_theory\) "Walk (graph theory)") around the negative cycle. In such a case, the Bellman–Ford algorithm can detect and report the negative cycle.[\[1\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Bang-1)[\[5\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTEKleinbergTardos2006-5) ## Algorithm \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=1 "Edit section: Algorithm")\] [![](https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Bellman-Ford_worst-case_example.svg/250px-Bellman-Ford_worst-case_example.svg.png)](https://en.wikipedia.org/wiki/File:Bellman-Ford_worst-case_example.svg) In this example graph, assuming that A is the source and edges are processed in the worst order, from right to left, it requires the full \|*V*\|āˆ’1 or 4 iterations for the distance estimates to converge. Conversely, if the edges are processed in the best order, from left to right, the algorithm converges in a single iteration. Like [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm "Dijkstra's algorithm"), Bellman–Ford proceeds by [relaxation](https://en.wikipedia.org/wiki/Relaxation_\(iterative_method\) "Relaxation (iterative method)"), in which approximations to the correct distance are replaced by better ones until they eventually reach the solution. In both algorithms, the approximate distance to each vertex is always an overestimate of the true distance, and is replaced by the minimum of its old value and the length of a newly found path.[\[6\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTECormenLeisersonRivestStein2022Section_22.1-6) However, Dijkstra's algorithm uses a [priority queue](https://en.wikipedia.org/wiki/Priority_queue "Priority queue") to [greedily](https://en.wikipedia.org/wiki/Greedy_algorithm "Greedy algorithm") select the closest vertex that has not yet been processed, and performs this relaxation process on all of its outgoing edges; by contrast, the Bellman–Ford algorithm simply relaxes *all* the edges, and does this \| V \| āˆ’ 1 {\\displaystyle \|V\|-1} ![{\\displaystyle \|V\|-1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/6407482f919e956e1d22eb304c73a841e395e436) times, where \| V \| {\\displaystyle \|V\|} ![{\\displaystyle \|V\|}](https://wikimedia.org/api/rest_v1/media/math/render/svg/9ddcffc28643ac01a14dd0fb32c3157859e365a7) is the number of vertices in the graph.[\[6\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTECormenLeisersonRivestStein2022Section_22.1-6) In each of these repetitions, the number of vertices with correctly calculated distances grows, from which it follows that eventually all vertices will have their correct distances. This method allows the Bellman–Ford algorithm to be applied to a wider class of inputs than Dijkstra's algorithm. The intermediate answers and the choices among equally short paths depend on the order of edges relaxed, but the final distances remain the same.[\[6\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTECormenLeisersonRivestStein2022Section_22.1-6) Bellman–Ford runs in O ( \| V \| ā‹… \| E \| ) {\\displaystyle O(\|V\|\\cdot \|E\|)} ![{\\displaystyle O(\|V\|\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/93b30ae8ec84ab14193f0b6a384c39e796c80545) [time](https://en.wikipedia.org/wiki/Big_O_notation "Big O notation"), where \| V \| {\\displaystyle \|V\|} ![{\\displaystyle \|V\|}](https://wikimedia.org/api/rest_v1/media/math/render/svg/9ddcffc28643ac01a14dd0fb32c3157859e365a7) and \| E \| {\\displaystyle \|E\|} ![{\\displaystyle \|E\|}](https://wikimedia.org/api/rest_v1/media/math/render/svg/d8c2b9637808cf805d411190b4ae017dbd4ef8d8) are the number of vertices and edges respectively. ``` function BellmanFord(list vertices, list edges, vertex source) is // This implementation takes in a graph, represented as // lists of vertices (represented as integers [0..n-1]) and // edges, and fills two arrays (distance and predecessor) // holding the shortest path from the source to each vertex 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 < distance[v] then distance[v]Ā := distance[u] + w predecessor[v]Ā := u // Step 3: check for negative-weight cycles for each edge (u, v) with weight w in edges do if distance[u] + w < distance[v] then predecessor[v]Ā := u // A negative cycle exists; // find a vertex on the cycle visitedĀ := list of size n initialized with false visited[v]Ā := true while not visited[u] do visited[u]Ā := true uĀ := predecessor[u] // u is a vertex in a negative cycle, // find the cycle itself ncycleĀ := [u] vĀ := predecessor[u] while vĀ != u do ncycleĀ := concatenate([v], ncycle) vĀ := predecessor[v] error "Graph contains a negative-weight cycle", ncycle return distance, predecessor ``` Simply put, the algorithm initializes the distance to the source to 0 and all other nodes to infinity. Then for all edges, if the distance to the destination can be shortened by taking the edge, the distance is updated to the new lower value. The core of the algorithm is a loop that scans across all edges at every loop. For every i ≤ \| V \| āˆ’ 1 {\\displaystyle i\\leq \|V\|-1} ![{\\displaystyle i\\leq \|V\|-1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/56990c69f013b51549590f36f24e4ca6ef9e9226), at the end of the i {\\displaystyle i} ![{\\displaystyle i}](https://wikimedia.org/api/rest_v1/media/math/render/svg/add78d8608ad86e54951b8c8bd6c8d8416533d20)\-th iteration, from any vertex v, following the predecessor trail recorded in predecessor yields a path that has a total weight that is at most distance\[v\], and further, distance\[v\] is a lower bound to the length of any path from source to v that uses at most i edges. Since the longest possible path without a cycle can be \| V \| āˆ’ 1 {\\displaystyle \|V\|-1} ![{\\displaystyle \|V\|-1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/6407482f919e956e1d22eb304c73a841e395e436) edges, the edges must be scanned \| V \| āˆ’ 1 {\\displaystyle \|V\|-1} ![{\\displaystyle \|V\|-1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/6407482f919e956e1d22eb304c73a841e395e436) 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 \| {\\displaystyle \|V\|} ![{\\displaystyle \|V\|}](https://wikimedia.org/api/rest_v1/media/math/render/svg/9ddcffc28643ac01a14dd0fb32c3157859e365a7) 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](https://en.wikipedia.org/wiki/Cycle_detection "Cycle detection") 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 \| ā‹… \| E \| ) {\\displaystyle O(\|V\|\\cdot \|E\|)} ![{\\displaystyle O(\|V\|\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/93b30ae8ec84ab14193f0b6a384c39e796c80545) to O ( l ā‹… \| E \| ) {\\displaystyle O(l\\cdot \|E\|)} ![{\\displaystyle O(l\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/c964835b9317dfc64ec01d68158c337cb32cabba) where l {\\displaystyle l} ![{\\displaystyle l}](https://wikimedia.org/api/rest_v1/media/math/render/svg/829091f745070b9eb97a80244129025440a1cfac) is the maximum length of a shortest path in the graph. ## Proof of correctness \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=2 "Edit section: Proof of correctness")\] The correctness of the algorithm can be shown by [induction](https://en.wikipedia.org/wiki/Mathematical_induction "Mathematical induction"):[\[2\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-web.stanford.edu-2)[\[7\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-7) **Lemma**. After *i* repetitions of *for* loop, - if Distance(*u*) is not infinity, it is equal to the length of some path from *s* to *u*; and - if there is a path from *s* to *u* with at most *i* edges, then Distance(u) is at most the length of the shortest path from *s* to *u* with at most *i* edges. **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 *ith* 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 <= v[i-1 (mod k)].distance + v[i-1 (mod k)]v[i].weight` Summing around the cycle, the *v*\[*i*\].distance and *v*\[*i*āˆ’1 (mod *k*)\].distance terms cancel, leaving `0 <= sum from 1 to k of v[i-1 (mod k)]v[i].weight` I.e., every cycle has nonnegative weight. ## Finding negative cycles \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=3 "Edit section: Finding negative cycles")\] When the algorithm is used to find shortest paths, the existence of negative cycles is a problem, preventing the algorithm from finding a correct answer. However, since it terminates upon finding a negative cycle, the Bellman–Ford algorithm can be used for applications in which this is the target to be sought – for example in [cycle-cancelling](https://en.wikipedia.org/wiki/Minimum-cost_flow_problem "Minimum-cost flow problem") techniques in [network flow](https://en.wikipedia.org/wiki/Flow_network "Flow network") analysis.[\[1\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Bang-1) ## Applications in routing \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=4 "Edit section: Applications in routing")\] A distributed variant of the Bellman–Ford algorithm is used in [distance-vector routing protocols](https://en.wikipedia.org/wiki/Distance-vector_routing_protocol "Distance-vector routing protocol"), for example the [Routing Information Protocol](https://en.wikipedia.org/wiki/Routing_Information_Protocol "Routing Information Protocol") (RIP).[\[8\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-8) The algorithm is distributed because it involves a number of nodes (routers) within an [Autonomous system (AS)](https://en.wikipedia.org/wiki/Autonomous_system_\(Internet\) "Autonomous system (Internet)"), a collection of IP networks typically owned by an ISP. It consists of the following steps: 1. Each node calculates the distances between itself and all other nodes within the AS and stores this information as a table. 2. Each node sends its table to all neighboring nodes. 3. When a node receives distance tables from its neighbors, it calculates the shortest routes to all other nodes and updates its own table to reflect any changes. The main disadvantages of the Bellman–Ford algorithm in this setting are as follows: - It does not scale well. - Changes in [network topology](https://en.wikipedia.org/wiki/Network_topology "Network topology") are not reflected quickly since updates are spread node-by-node. - [Count to infinity](https://en.wikipedia.org/wiki/Distance-vector_routing_protocol#Count_to_infinity_problem "Distance-vector routing protocol") if link or node failures render a node unreachable from some set of other nodes, those nodes may spend forever gradually increasing their estimates of the distance to it, and in the meantime there may be routing loops. ## Improvements \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=5 "Edit section: Improvements")\] The Bellman–Ford algorithm may be improved in practice (although not in the worst case) by the observation that, if an iteration of the main loop of the algorithm terminates without making any changes, the algorithm can be immediately terminated, as subsequent iterations will not make any more changes. With this early termination condition, the main loop may in some cases use many fewer than \|*V*\| āˆ’ 1 iterations, even though the worst case of the algorithm remains unchanged. The following improvements all maintain the O ( \| V \| ā‹… \| E \| ) {\\displaystyle O(\|V\|\\cdot \|E\|)} ![{\\displaystyle O(\|V\|\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/93b30ae8ec84ab14193f0b6a384c39e796c80545) worst-case time complexity. A variation of the Bellman–Ford algorithm described by [Moore (1959)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFMoore1959), reduces the number of relaxation steps that need to be performed within each iteration of the algorithm. If a vertex *v* has a distance value that has not changed since the last time the edges out of *v* were relaxed, then there is no need to relax the edges out of *v* a second time. In this way, as the number of vertices with correct distance values grows, the number whose outgoing edges that need to be relaxed in each iteration shrinks, leading to a constant-factor savings in time for [dense graphs](https://en.wikipedia.org/wiki/Dense_graph "Dense graph"). This variation can be implemented by keeping a collection of vertices whose outgoing edges need to be relaxed, removing a vertex from this collection when its edges are relaxed, and adding to the collection any vertex whose distance value is changed by a relaxation step. In China, this algorithm was popularized by Fanding Duan, who rediscovered it in 1994, as the "shortest path faster algorithm".[\[9\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-9) [Yen (1970)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFYen1970) described another improvement to the Bellman–Ford algorithm. His improvement first assigns some arbitrary linear order on all vertices and then partitions the set of all edges into two subsets. The first subset, *Ef*, contains all edges (*vi*, *vj*) such that *i* \< *j*; the second, *Eb*, contains edges (*vi*, *vj*) such that *i* \> *j*. Each vertex is visited in the order *v1*, *v2*, ..., *v*\|*V*\|, relaxing each outgoing edge from that vertex in *Ef*. Each vertex is then visited in the order *v*\|*V*\|, *v*\|*V*\|āˆ’1, ..., *v*1, relaxing each outgoing edge from that vertex in *Eb*. Each iteration of the main loop of the algorithm, after the first one, adds at least two edges to the set of edges whose relaxed distances match the correct shortest path distances: one from *Ef* and one from *Eb*. This modification reduces the worst-case number of iterations of the main loop of the algorithm from \|*V*\| āˆ’ 1 to \| V \| / 2 {\\displaystyle \|V\|/2} ![{\\displaystyle \|V\|/2}](https://wikimedia.org/api/rest_v1/media/math/render/svg/969159a4606100e8bf46319874300f27fc64c9d9).[\[10\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-10)[\[11\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Sedweb-11) Another improvement, by [Bannister & Eppstein (2012)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFBannisterEppstein2012), replaces the arbitrary linear order of the vertices used in Yen's second improvement by a [random permutation](https://en.wikipedia.org/wiki/Random_permutation "Random permutation"). This change makes the worst case for Yen's improvement (in which the edges of a shortest path strictly alternate between the two subsets *Ef* and *Eb*) very unlikely to happen. With a randomly permuted vertex ordering, the [expected](https://en.wikipedia.org/wiki/Expected_value "Expected value") number of iterations needed in the main loop is at most \| V \| / 3 {\\displaystyle \|V\|/3} ![{\\displaystyle \|V\|/3}](https://wikimedia.org/api/rest_v1/media/math/render/svg/e464851c4b44de4d198e82c9aae50766e3b6a88c).[\[11\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Sedweb-11) [Fineman (2024)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFFineman2024), at [Georgetown University](https://en.wikipedia.org/wiki/Georgetown_University "Georgetown University"), created an improved algorithm that with high probability runs in O ~ ( \| V \| 8 9 ā‹… \| E \| ) {\\displaystyle {\\tilde {O}}(\|V\|^{\\frac {8}{9}}\\cdot \|E\|)} ![{\\displaystyle {\\tilde {O}}(\|V\|^{\\frac {8}{9}}\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/c3568bfa40c7c7a58e040eb2be5323ee25cfb7f6) [time](https://en.wikipedia.org/wiki/Time_complexity "Time complexity"). Here, the O ~ {\\displaystyle {\\tilde {O}}} ![{\\displaystyle {\\tilde {O}}}](https://wikimedia.org/api/rest_v1/media/math/render/svg/838fcae65623915485e4245a38d787c4a99f9be6) is a variant of [big O notation](https://en.wikipedia.org/wiki/Big_O_notation "Big O notation") that hides logarithmic factors. ## Notes \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=6 "Edit section: Notes")\] 1. ^ [***a***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Bang_1-0) [***b***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Bang_1-1) [***c***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Bang_1-2) [***d***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Bang_1-3) [Bang-Jensen & Gutin (2000)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFBang-JensenGutin2000) 2. ^ [***a***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-web.stanford.edu_2-0) [***b***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-web.stanford.edu_2-1) [Lecture 14](https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/lecture14.pdf) stanford.edu 3. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Schrijver_3-0)** [Schrijver (2005)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFSchrijver2005) 4. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTESedgewick2002_4-0)** [Sedgewick (2002)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFSedgewick2002). 5. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTEKleinbergTardos2006_5-0)** [Kleinberg & Tardos (2006)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFKleinbergTardos2006). 6. ^ [***a***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTECormenLeisersonRivestStein2022Section_22.1_6-0) [***b***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTECormenLeisersonRivestStein2022Section_22.1_6-1) [***c***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTECormenLeisersonRivestStein2022Section_22.1_6-2) [Cormen et al. (2022)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFCormenLeisersonRivestStein2022), Section 22.1. 7. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-7)** Dinitz, Yefim; Itzhak, Rotem (2017-01-01). ["Hybrid Bellman–Ford–Dijkstra algorithm"](https://www.sciencedirect.com/science/article/pii/S1570866717300011). *Journal of Discrete Algorithms*. **42**: 35–44\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1016/j.jda.2017.01.001](https://doi.org/10.1016%2Fj.jda.2017.01.001). [ISSN](https://en.wikipedia.org/wiki/ISSN_\(identifier\) "ISSN (identifier)") [1570-8667](https://search.worldcat.org/issn/1570-8667). 8. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-8)** Malkin, Gary S. (November 1998). [RIP Version 2](https://www.rfc-editor.org/rfc/rfc2453) (Report). Internet Engineering Task Force. 9. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-9)** Duan, Fanding (1994). ["å…³äŗŽęœ€ēŸ­č·Æå¾„ēš„SPFAåæ«é€Ÿē®—ę³• \[About the SPFA algorithm\]"](http://wenku.baidu.com/view/3b8c5d778e9951e79a892705.html). *Journal of Southwest Jiaotong University*. **29** (2): 207–212\. 10. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-10)** Cormen et al., 4th ed., Problem 22-1, p. 640. 11. ^ [***a***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Sedweb_11-0) [***b***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Sedweb_11-1) See Sedgewick's [web exercises](http://algs4.cs.princeton.edu/44sp/) for *Algorithms*, 4th ed., exercises 5 and 12 (retrieved 2013-01-30). ## References \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=7 "Edit section: References")\] ### Original sources \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=8 "Edit section: Original sources")\] - Shimbel, A. (1955). *Structure in communication nets*. Proceedings of the Symposium on Information Networks. New York, New York: Polytechnic Press of the Polytechnic Institute of Brooklyn. pp. 199–203\. - [Bellman, Richard](https://en.wikipedia.org/wiki/Richard_Bellman "Richard Bellman") (1958). ["On a routing problem"](https://doi.org/10.1090%2Fqam%2F102435). *Quarterly of Applied Mathematics*. **16**: 87–90\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1090/qam/102435](https://doi.org/10.1090%2Fqam%2F102435). [MR](https://en.wikipedia.org/wiki/MR_\(identifier\) "MR (identifier)") [0102435](https://mathscinet.ams.org/mathscinet-getitem?mr=0102435). - [Ford, Lester R. Jr.](https://en.wikipedia.org/wiki/L._R._Ford_Jr. "L. R. Ford Jr.") (August 14, 1956). [*Network Flow Theory*](http://www.rand.org/pubs/papers/P923.html). Paper P-923. Santa Monica, California: RAND Corporation. - [Moore, Edward F.](https://en.wikipedia.org/wiki/Edward_F._Moore "Edward F. Moore") (1959). *The shortest path through a maze*. Proc. Internat. Sympos. Switching Theory 1957, Part II. Cambridge, Massachusetts: Harvard Univ. Press. pp. 285–292\. [MR](https://en.wikipedia.org/wiki/MR_\(identifier\) "MR (identifier)") [0114710](https://mathscinet.ams.org/mathscinet-getitem?mr=0114710). - Yen, Jin Y. (1970). ["An algorithm for finding shortest routes from all source nodes to a given destination in general networks"](https://doi.org/10.1090%2Fqam%2F253822). *Quarterly of Applied Mathematics*. **27** (4): 526–530\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1090/qam/253822](https://doi.org/10.1090%2Fqam%2F253822). [MR](https://en.wikipedia.org/wiki/MR_\(identifier\) "MR (identifier)") [0253822](https://mathscinet.ams.org/mathscinet-getitem?mr=0253822). - Bannister, M. J.; [Eppstein, D.](https://en.wikipedia.org/wiki/David_Eppstein "David Eppstein") (2012). "Randomized speedup of the Bellman–Ford algorithm". *Analytic Algorithmics and Combinatorics (ANALCO12), Kyoto, Japan*. pp. 41–47\. [arXiv](https://en.wikipedia.org/wiki/ArXiv_\(identifier\) "ArXiv (identifier)"):[1111\.5414](https://arxiv.org/abs/1111.5414). [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1137/1.9781611973020.6](https://doi.org/10.1137%2F1.9781611973020.6). - Fineman, Jeremy T. (2024). "Single-source shortest paths with negative real weights in O ~ ( m n 8 / 9 ) {\\displaystyle {\\tilde {O}}(mn^{8/9})} ![{\\displaystyle {\\tilde {O}}(mn^{8/9})}](https://wikimedia.org/api/rest_v1/media/math/render/svg/c0e5c63e4346832a930fcb0cc0db70ea35b66dde) time". In Mohar, Bojan; Shinkar, Igor; O'Donnell, Ryan (eds.). *Proceedings of the 56th Annual ACM Symposium on Theory of Computing, STOC 2024, Vancouver, BC, Canada, June 24–28, 2024*. Association for Computing Machinery. pp. 3–14\. [arXiv](https://en.wikipedia.org/wiki/ArXiv_\(identifier\) "ArXiv (identifier)"):[2311\.02520](https://arxiv.org/abs/2311.02520). [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1145/3618260.3649614](https://doi.org/10.1145%2F3618260.3649614). ### Secondary sources \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=9 "Edit section: Secondary sources")\] - [Ford, L. R. Jr.](https://en.wikipedia.org/wiki/L._R._Ford_Jr. "L. R. Ford Jr."); [Fulkerson, D. R.](https://en.wikipedia.org/wiki/D._R._Fulkerson "D. R. Fulkerson") (1962). "A shortest chain algorithm". *Flows in Networks*. Princeton University Press. pp. 130–134\. - Bang-Jensen, JĆørgen; Gutin, Gregory (2000). "Section 2.3.4: The Bellman-Ford-Moore algorithm". [*Digraphs: Theory, Algorithms and Applications*](http://www.cs.rhul.ac.uk/books/dbook/) (First ed.). Springer. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)") [978-1-84800-997-4](https://en.wikipedia.org/wiki/Special:BookSources/978-1-84800-997-4 "Special:BookSources/978-1-84800-997-4") . - Schrijver, Alexander (2005). ["On the history of combinatorial optimization (till 1960)"](http://homepages.cwi.nl/~lex/files/histco.pdf) (PDF). *Handbook of Discrete Optimization*. Elsevier: 1–68\. - [Cormen, Thomas H.](https://en.wikipedia.org/wiki/Thomas_H._Cormen "Thomas H. Cormen"); [Leiserson, Charles E.](https://en.wikipedia.org/wiki/Charles_E._Leiserson "Charles E. Leiserson"); [Rivest, Ronald L.](https://en.wikipedia.org/wiki/Ron_Rivest "Ron Rivest"); [Stein, Clifford](https://en.wikipedia.org/wiki/Clifford_Stein "Clifford Stein") (2022) \[1990\]. [*Introduction to Algorithms*](https://en.wikipedia.org/wiki/Introduction_to_Algorithms "Introduction to Algorithms") (4th ed.). MIT Press and McGraw-Hill. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)") [0-262-04630-X](https://en.wikipedia.org/wiki/Special:BookSources/0-262-04630-X "Special:BookSources/0-262-04630-X") . Section 22.1: The Bellman–Ford algorithm, pp. 612–616. Problem 22–1, p. 640. - Heineman, George T.; Pollice, Gary; Selkow, Stanley (2008). "Chapter 6: Graph Algorithms". *Algorithms in a Nutshell*. [O'Reilly Media](https://en.wikipedia.org/wiki/O%27Reilly_Media "O'Reilly Media"). pp. 160–164\. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)") [978-0-596-51624-6](https://en.wikipedia.org/wiki/Special:BookSources/978-0-596-51624-6 "Special:BookSources/978-0-596-51624-6") . - [Kleinberg, Jon](https://en.wikipedia.org/wiki/Jon_Kleinberg "Jon Kleinberg"); [Tardos, Ɖva](https://en.wikipedia.org/wiki/%C3%89va_Tardos "Ɖva Tardos") (2006). *Algorithm Design*. New York: Pearson Education, Inc. - [Sedgewick, Robert](https://en.wikipedia.org/wiki/Robert_Sedgewick_\(computer_scientist\) "Robert Sedgewick (computer scientist)") (2002). "Section 21.7: Negative Edge Weights". [*Algorithms in Java*](https://web.archive.org/web/20080531142256/http://safari.oreilly.com/0201361213/ch21lev1sec7) (3rd ed.). Addison-Wesley. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)") [0-201-36121-3](https://en.wikipedia.org/wiki/Special:BookSources/0-201-36121-3 "Special:BookSources/0-201-36121-3") . Archived from [the original](http://safari.oreilly.com/0201361213/ch21lev1sec7) on 2008-05-31. Retrieved 2007-05-28. | [v](https://en.wikipedia.org/wiki/Template:Graph_traversal_algorithms "Template:Graph traversal algorithms") [t](https://en.wikipedia.org/wiki/Template_talk:Graph_traversal_algorithms "Template talk:Graph traversal algorithms") [e](https://en.wikipedia.org/wiki/Special:EditPage/Template:Graph_traversal_algorithms "Special:EditPage/Template:Graph traversal algorithms")[Graph](https://en.wikipedia.org/wiki/Graph_traversal "Graph traversal") and [tree](https://en.wikipedia.org/wiki/Tree_traversal "Tree traversal") traversal algorithms | | |---|---| | [Search](https://en.wikipedia.org/wiki/Graph_traversal "Graph traversal") | [α–β pruning](https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning "Alpha–beta pruning") [**A\***](https://en.wikipedia.org/wiki/A*_search_algorithm "A* search algorithm") [IDA\*](https://en.wikipedia.org/wiki/Iterative_deepening_A* "Iterative deepening A*") [LPA\*](https://en.wikipedia.org/wiki/Lifelong_Planning_A* "Lifelong Planning A*") [SMA\*](https://en.wikipedia.org/wiki/SMA* "SMA*") [Best-first search](https://en.wikipedia.org/wiki/Best-first_search "Best-first search") [Beam search](https://en.wikipedia.org/wiki/Beam_search "Beam search") [Bidirectional search](https://en.wikipedia.org/wiki/Bidirectional_search "Bidirectional search") [Breadth-first search](https://en.wikipedia.org/wiki/Breadth-first_search "Breadth-first search") [Lexicographic](https://en.wikipedia.org/wiki/Lexicographic_breadth-first_search "Lexicographic breadth-first search") [Parallel](https://en.wikipedia.org/wiki/Parallel_breadth-first_search "Parallel breadth-first search") [B\*](https://en.wikipedia.org/wiki/B* "B*") [Depth-first search](https://en.wikipedia.org/wiki/Depth-first_search "Depth-first search") [Iterative deepening](https://en.wikipedia.org/wiki/Iterative_deepening_depth-first_search "Iterative deepening depth-first search") [D\*](https://en.wikipedia.org/wiki/D* "D*") [Fringe search](https://en.wikipedia.org/wiki/Fringe_search "Fringe search") [Jump point search](https://en.wikipedia.org/wiki/Jump_point_search "Jump point search") [Monte Carlo tree search](https://en.wikipedia.org/wiki/Monte_Carlo_tree_search "Monte Carlo tree search") [SSS\*](https://en.wikipedia.org/wiki/SSS* "SSS*") | | [Shortest path](https://en.wikipedia.org/wiki/Shortest_path_problem "Shortest path problem") | [Bellman–Ford]() [Dijkstra's](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm "Dijkstra's algorithm") [Floyd–Warshall](https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm "Floyd–Warshall algorithm") [Johnson's](https://en.wikipedia.org/wiki/Johnson%27s_algorithm "Johnson's algorithm") [Shortest path faster](https://en.wikipedia.org/wiki/Shortest_path_faster_algorithm "Shortest path faster algorithm") [Yen's](https://en.wikipedia.org/wiki/Yen%27s_algorithm "Yen's algorithm") | | [Minimum spanning tree](https://en.wikipedia.org/wiki/Minimum_spanning_tree "Minimum spanning tree") | [BorÅÆvka's](https://en.wikipedia.org/wiki/Bor%C5%AFvka%27s_algorithm "BorÅÆvka's algorithm") [Kruskal's](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm "Kruskal's algorithm") [Prim's](https://en.wikipedia.org/wiki/Prim%27s_algorithm "Prim's algorithm") [Reverse-delete](https://en.wikipedia.org/wiki/Reverse-delete_algorithm "Reverse-delete algorithm") | | [List of graph search algorithms](https://en.wikipedia.org/wiki/List_of_algorithms#Graph_search "List of algorithms") | | ![](https://en.wikipedia.org/wiki/Special:CentralAutoLogin/start?useformat=desktop&type=1x1&usesul3=1) Retrieved from "<https://en.wikipedia.org/w/index.php?title=Bellman–Ford_algorithm&oldid=1326384451>" [Categories](https://en.wikipedia.org/wiki/Help:Category "Help:Category"): - [Graph algorithms](https://en.wikipedia.org/wiki/Category:Graph_algorithms "Category:Graph algorithms") - [Polynomial-time problems](https://en.wikipedia.org/wiki/Category:Polynomial-time_problems "Category:Polynomial-time problems") - [Dynamic programming](https://en.wikipedia.org/wiki/Category:Dynamic_programming "Category:Dynamic programming") - [Graph distance](https://en.wikipedia.org/wiki/Category:Graph_distance "Category:Graph distance") Hidden categories: - [Articles with short description](https://en.wikipedia.org/wiki/Category:Articles_with_short_description "Category:Articles with short description") - [Short description is different from Wikidata](https://en.wikipedia.org/wiki/Category:Short_description_is_different_from_Wikidata "Category:Short description is different from Wikidata") - [Articles with example C code](https://en.wikipedia.org/wiki/Category:Articles_with_example_C_code "Category:Articles with example C code") - [Articles with example pseudocode](https://en.wikipedia.org/wiki/Category:Articles_with_example_pseudocode "Category:Articles with example pseudocode") - This page was last edited on 8 December 2025, at 18:03 (UTC). - Text is available under the [Creative Commons Attribution-ShareAlike 4.0 License](https://en.wikipedia.org/wiki/Wikipedia:Text_of_the_Creative_Commons_Attribution-ShareAlike_4.0_International_License "Wikipedia:Text of the Creative Commons Attribution-ShareAlike 4.0 International License"); additional terms may apply. By using this site, you agree to the [Terms of Use](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Terms_of_Use "foundation:Special:MyLanguage/Policy:Terms of Use") and [Privacy Policy](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy "foundation:Special:MyLanguage/Policy:Privacy policy"). WikipediaĀ® is a registered trademark of the [Wikimedia Foundation, Inc.](https://wikimediafoundation.org/), a non-profit organization. - [Privacy policy](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy) - [About Wikipedia](https://en.wikipedia.org/wiki/Wikipedia:About) - [Disclaimers](https://en.wikipedia.org/wiki/Wikipedia:General_disclaimer) - [Contact Wikipedia](https://en.wikipedia.org/wiki/Wikipedia:Contact_us) - [Legal & safety contacts](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Legal:Wikimedia_Foundation_Legal_and_Safety_Contact_Information) - [Code of Conduct](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Universal_Code_of_Conduct) - [Developers](https://developer.wikimedia.org/) - [Statistics](https://stats.wikimedia.org/#/en.wikipedia.org) - [Cookie statement](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Cookie_statement) - [Mobile view](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&mobileaction=toggle_view_mobile) - [![Wikimedia Foundation](https://en.wikipedia.org/static/images/footer/wikimedia.svg)](https://www.wikimedia.org/) - [![Powered by MediaWiki](https://en.wikipedia.org/w/resources/assets/mediawiki_compact.svg)](https://www.mediawiki.org/) Search Toggle the table of contents Bellman–Ford algorithm 28 languages [Add topic](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm)
Readable Markdown
From Wikipedia, the free encyclopedia | | | |---|---| | [![](https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Bellman%E2%80%93Ford_algorithm_example.gif/250px-Bellman%E2%80%93Ford_algorithm_example.gif)](https://en.wikipedia.org/wiki/File:Bellman%E2%80%93Ford_algorithm_example.gif) | | | Class | [Single-source shortest path problem](https://en.wikipedia.org/wiki/Single-source_shortest_path_problem "Single-source shortest path problem") (for weighted directed graphs) | | Data structure | [Graph](https://en.wikipedia.org/wiki/Graph_\(data_structure\) "Graph (data structure)") | | [Worst-case](https://en.wikipedia.org/wiki/Best,_worst_and_average_case "Best, worst and average case") [performance](https://en.wikipedia.org/wiki/Time_complexity "Time complexity") | ![{\\displaystyle \\Theta (\|V\|\|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/0465422c67bedf2d1659571f5797b3c9c54ed9ad) | The **Bellman–Ford algorithm** is an [algorithm](https://en.wikipedia.org/wiki/Algorithm "Algorithm") that computes [shortest paths](https://en.wikipedia.org/wiki/Shortest_path "Shortest path") from a single source [vertex](https://en.wikipedia.org/wiki/Vertex_\(graph_theory\) "Vertex (graph theory)") to all of the other vertices in a [weighted digraph](https://en.wikipedia.org/wiki/Weighted_digraph "Weighted digraph").[\[1\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Bang-1) It is slower than [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm "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.[\[2\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-web.stanford.edu-2) The algorithm was first proposed by Alfonso Shimbel ([1955](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFShimbel1955)), but is instead named after [Richard Bellman](https://en.wikipedia.org/wiki/Richard_Bellman "Richard Bellman") and [Lester Ford Jr.](https://en.wikipedia.org/wiki/L._R._Ford_Jr. "L. R. Ford Jr."), who published it in [1958](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFBellman1958) and [1956](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFFord1956), respectively.[\[3\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Schrijver-3) [Edward F. Moore](https://en.wikipedia.org/wiki/Edward_F._Moore "Edward F. Moore") also published a variation of the algorithm in [1959](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFMoore1959), and for this reason it is also sometimes called the **Bellman–Ford–Moore algorithm**.[\[1\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Bang-1) Negative edge weights are found in various applications of graphs. This is why this algorithm is useful.[\[4\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTESedgewick2002-4) If a graph contains a "negative cycle" (i.e. a [cycle](https://en.wikipedia.org/wiki/Cycle_\(graph_theory\) "Cycle (graph theory)") 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](https://en.wikipedia.org/wiki/Walk_\(graph_theory\) "Walk (graph theory)") around the negative cycle. In such a case, the Bellman–Ford algorithm can detect and report the negative cycle.[\[1\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Bang-1)[\[5\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTEKleinbergTardos2006-5) [![](https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Bellman-Ford_worst-case_example.svg/250px-Bellman-Ford_worst-case_example.svg.png)](https://en.wikipedia.org/wiki/File:Bellman-Ford_worst-case_example.svg) In this example graph, assuming that A is the source and edges are processed in the worst order, from right to left, it requires the full \|*V*\|āˆ’1 or 4 iterations for the distance estimates to converge. Conversely, if the edges are processed in the best order, from left to right, the algorithm converges in a single iteration. Like [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm "Dijkstra's algorithm"), Bellman–Ford proceeds by [relaxation](https://en.wikipedia.org/wiki/Relaxation_\(iterative_method\) "Relaxation (iterative method)"), in which approximations to the correct distance are replaced by better ones until they eventually reach the solution. In both algorithms, the approximate distance to each vertex is always an overestimate of the true distance, and is replaced by the minimum of its old value and the length of a newly found path.[\[6\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTECormenLeisersonRivestStein2022Section_22.1-6) However, Dijkstra's algorithm uses a [priority queue](https://en.wikipedia.org/wiki/Priority_queue "Priority queue") to [greedily](https://en.wikipedia.org/wiki/Greedy_algorithm "Greedy algorithm") select the closest vertex that has not yet been processed, and performs this relaxation process on all of its outgoing edges; by contrast, the Bellman–Ford algorithm simply relaxes *all* the edges, and does this ![{\\displaystyle \|V\|-1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/6407482f919e956e1d22eb304c73a841e395e436) times, where ![{\\displaystyle \|V\|}](https://wikimedia.org/api/rest_v1/media/math/render/svg/9ddcffc28643ac01a14dd0fb32c3157859e365a7) is the number of vertices in the graph.[\[6\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTECormenLeisersonRivestStein2022Section_22.1-6) In each of these repetitions, the number of vertices with correctly calculated distances grows, from which it follows that eventually all vertices will have their correct distances. This method allows the Bellman–Ford algorithm to be applied to a wider class of inputs than Dijkstra's algorithm. The intermediate answers and the choices among equally short paths depend on the order of edges relaxed, but the final distances remain the same.[\[6\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-FOOTNOTECormenLeisersonRivestStein2022Section_22.1-6) Bellman–Ford runs in ![{\\displaystyle O(\|V\|\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/93b30ae8ec84ab14193f0b6a384c39e796c80545) [time](https://en.wikipedia.org/wiki/Big_O_notation "Big O notation"), where ![{\\displaystyle \|V\|}](https://wikimedia.org/api/rest_v1/media/math/render/svg/9ddcffc28643ac01a14dd0fb32c3157859e365a7) and ![{\\displaystyle \|E\|}](https://wikimedia.org/api/rest_v1/media/math/render/svg/d8c2b9637808cf805d411190b4ae017dbd4ef8d8) are the number of vertices and edges respectively. ``` function BellmanFord(list vertices, list edges, vertex source) is // This implementation takes in a graph, represented as // lists of vertices (represented as integers [0..n-1]) and // edges, and fills two arrays (distance and predecessor) // holding the shortest path from the source to each vertex 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 < distance[v] then distance[v]Ā := distance[u] + w predecessor[v]Ā := u // Step 3: check for negative-weight cycles for each edge (u, v) with weight w in edges do if distance[u] + w < distance[v] then predecessor[v]Ā := u // A negative cycle exists; // find a vertex on the cycle visitedĀ := list of size n initialized with false visited[v]Ā := true while not visited[u] do visited[u]Ā := true uĀ := predecessor[u] // u is a vertex in a negative cycle, // find the cycle itself ncycleĀ := [u] vĀ := predecessor[u] while vĀ != u do ncycleĀ := concatenate([v], ncycle) vĀ := predecessor[v] error "Graph contains a negative-weight cycle", ncycle return distance, predecessor ``` Simply put, the algorithm initializes the distance to the source to 0 and all other nodes to infinity. Then for all edges, if the distance to the destination can be shortened by taking the edge, the distance is updated to the new lower value. The core of the algorithm is a loop that scans across all edges at every loop. For every ![{\\displaystyle i\\leq \|V\|-1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/56990c69f013b51549590f36f24e4ca6ef9e9226), at the end of the ![{\\displaystyle i}](https://wikimedia.org/api/rest_v1/media/math/render/svg/add78d8608ad86e54951b8c8bd6c8d8416533d20)\-th iteration, from any vertex v, following the predecessor trail recorded in predecessor yields a path that has a total weight that is at most distance\[v\], and further, distance\[v\] is a lower bound to the length of any path from source to v that uses at most i edges. Since the longest possible path without a cycle can be ![{\\displaystyle \|V\|-1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/6407482f919e956e1d22eb304c73a841e395e436) edges, the edges must be scanned ![{\\displaystyle \|V\|-1}](https://wikimedia.org/api/rest_v1/media/math/render/svg/6407482f919e956e1d22eb304c73a841e395e436) 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 ![{\\displaystyle \|V\|}](https://wikimedia.org/api/rest_v1/media/math/render/svg/9ddcffc28643ac01a14dd0fb32c3157859e365a7) 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](https://en.wikipedia.org/wiki/Cycle_detection "Cycle detection") 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 ![{\\displaystyle O(\|V\|\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/93b30ae8ec84ab14193f0b6a384c39e796c80545) to ![{\\displaystyle O(l\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/c964835b9317dfc64ec01d68158c337cb32cabba) where ![{\\displaystyle l}](https://wikimedia.org/api/rest_v1/media/math/render/svg/829091f745070b9eb97a80244129025440a1cfac) is the maximum length of a shortest path in the graph. ## Proof of correctness \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=2 "Edit section: Proof of correctness")\] The correctness of the algorithm can be shown by [induction](https://en.wikipedia.org/wiki/Mathematical_induction "Mathematical induction"):[\[2\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-web.stanford.edu-2)[\[7\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-7) **Lemma**. After *i* repetitions of *for* loop, - if Distance(*u*) is not infinity, it is equal to the length of some path from *s* to *u*; and - if there is a path from *s* to *u* with at most *i* edges, then Distance(u) is at most the length of the shortest path from *s* to *u* with at most *i* edges. **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 *ith* 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 <= v[i-1 (mod k)].distance + v[i-1 (mod k)]v[i].weight` Summing around the cycle, the *v*\[*i*\].distance and *v*\[*i*āˆ’1 (mod *k*)\].distance terms cancel, leaving `0 <= sum from 1 to k of v[i-1 (mod k)]v[i].weight` I.e., every cycle has nonnegative weight. ## Finding negative cycles \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=3 "Edit section: Finding negative cycles")\] When the algorithm is used to find shortest paths, the existence of negative cycles is a problem, preventing the algorithm from finding a correct answer. However, since it terminates upon finding a negative cycle, the Bellman–Ford algorithm can be used for applications in which this is the target to be sought – for example in [cycle-cancelling](https://en.wikipedia.org/wiki/Minimum-cost_flow_problem "Minimum-cost flow problem") techniques in [network flow](https://en.wikipedia.org/wiki/Flow_network "Flow network") analysis.[\[1\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Bang-1) ## Applications in routing \[[edit](https://en.wikipedia.org/w/index.php?title=Bellman%E2%80%93Ford_algorithm&action=edit&section=4 "Edit section: Applications in routing")\] A distributed variant of the Bellman–Ford algorithm is used in [distance-vector routing protocols](https://en.wikipedia.org/wiki/Distance-vector_routing_protocol "Distance-vector routing protocol"), for example the [Routing Information Protocol](https://en.wikipedia.org/wiki/Routing_Information_Protocol "Routing Information Protocol") (RIP).[\[8\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-8) The algorithm is distributed because it involves a number of nodes (routers) within an [Autonomous system (AS)](https://en.wikipedia.org/wiki/Autonomous_system_\(Internet\) "Autonomous system (Internet)"), a collection of IP networks typically owned by an ISP. It consists of the following steps: 1. Each node calculates the distances between itself and all other nodes within the AS and stores this information as a table. 2. Each node sends its table to all neighboring nodes. 3. When a node receives distance tables from its neighbors, it calculates the shortest routes to all other nodes and updates its own table to reflect any changes. The main disadvantages of the Bellman–Ford algorithm in this setting are as follows: - It does not scale well. - Changes in [network topology](https://en.wikipedia.org/wiki/Network_topology "Network topology") are not reflected quickly since updates are spread node-by-node. - [Count to infinity](https://en.wikipedia.org/wiki/Distance-vector_routing_protocol#Count_to_infinity_problem "Distance-vector routing protocol") if link or node failures render a node unreachable from some set of other nodes, those nodes may spend forever gradually increasing their estimates of the distance to it, and in the meantime there may be routing loops. The Bellman–Ford algorithm may be improved in practice (although not in the worst case) by the observation that, if an iteration of the main loop of the algorithm terminates without making any changes, the algorithm can be immediately terminated, as subsequent iterations will not make any more changes. With this early termination condition, the main loop may in some cases use many fewer than \|*V*\| āˆ’ 1 iterations, even though the worst case of the algorithm remains unchanged. The following improvements all maintain the ![{\\displaystyle O(\|V\|\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/93b30ae8ec84ab14193f0b6a384c39e796c80545) worst-case time complexity. A variation of the Bellman–Ford algorithm described by [Moore (1959)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFMoore1959), reduces the number of relaxation steps that need to be performed within each iteration of the algorithm. If a vertex *v* has a distance value that has not changed since the last time the edges out of *v* were relaxed, then there is no need to relax the edges out of *v* a second time. In this way, as the number of vertices with correct distance values grows, the number whose outgoing edges that need to be relaxed in each iteration shrinks, leading to a constant-factor savings in time for [dense graphs](https://en.wikipedia.org/wiki/Dense_graph "Dense graph"). This variation can be implemented by keeping a collection of vertices whose outgoing edges need to be relaxed, removing a vertex from this collection when its edges are relaxed, and adding to the collection any vertex whose distance value is changed by a relaxation step. In China, this algorithm was popularized by Fanding Duan, who rediscovered it in 1994, as the "shortest path faster algorithm".[\[9\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-9) [Yen (1970)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFYen1970) described another improvement to the Bellman–Ford algorithm. His improvement first assigns some arbitrary linear order on all vertices and then partitions the set of all edges into two subsets. The first subset, *Ef*, contains all edges (*vi*, *vj*) such that *i* \< *j*; the second, *Eb*, contains edges (*vi*, *vj*) such that *i* \> *j*. Each vertex is visited in the order *v1*, *v2*, ..., *v*\|*V*\|, relaxing each outgoing edge from that vertex in *Ef*. Each vertex is then visited in the order *v*\|*V*\|, *v*\|*V*\|āˆ’1, ..., *v*1, relaxing each outgoing edge from that vertex in *Eb*. Each iteration of the main loop of the algorithm, after the first one, adds at least two edges to the set of edges whose relaxed distances match the correct shortest path distances: one from *Ef* and one from *Eb*. This modification reduces the worst-case number of iterations of the main loop of the algorithm from \|*V*\| āˆ’ 1 to ![{\\displaystyle \|V\|/2}](https://wikimedia.org/api/rest_v1/media/math/render/svg/969159a4606100e8bf46319874300f27fc64c9d9).[\[10\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-10)[\[11\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Sedweb-11) Another improvement, by [Bannister & Eppstein (2012)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFBannisterEppstein2012), replaces the arbitrary linear order of the vertices used in Yen's second improvement by a [random permutation](https://en.wikipedia.org/wiki/Random_permutation "Random permutation"). This change makes the worst case for Yen's improvement (in which the edges of a shortest path strictly alternate between the two subsets *Ef* and *Eb*) very unlikely to happen. With a randomly permuted vertex ordering, the [expected](https://en.wikipedia.org/wiki/Expected_value "Expected value") number of iterations needed in the main loop is at most ![{\\displaystyle \|V\|/3}](https://wikimedia.org/api/rest_v1/media/math/render/svg/e464851c4b44de4d198e82c9aae50766e3b6a88c).[\[11\]](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_note-Sedweb-11) [Fineman (2024)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFFineman2024), at [Georgetown University](https://en.wikipedia.org/wiki/Georgetown_University "Georgetown University"), created an improved algorithm that with high probability runs in ![{\\displaystyle {\\tilde {O}}(\|V\|^{\\frac {8}{9}}\\cdot \|E\|)}](https://wikimedia.org/api/rest_v1/media/math/render/svg/c3568bfa40c7c7a58e040eb2be5323ee25cfb7f6) [time](https://en.wikipedia.org/wiki/Time_complexity "Time complexity"). Here, the ![{\\displaystyle {\\tilde {O}}}](https://wikimedia.org/api/rest_v1/media/math/render/svg/838fcae65623915485e4245a38d787c4a99f9be6) is a variant of [big O notation](https://en.wikipedia.org/wiki/Big_O_notation "Big O notation") that hides logarithmic factors. 1. ^ [***a***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Bang_1-0) [***b***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Bang_1-1) [***c***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Bang_1-2) [***d***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Bang_1-3) [Bang-Jensen & Gutin (2000)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFBang-JensenGutin2000) 2. ^ [***a***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-web.stanford.edu_2-0) [***b***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-web.stanford.edu_2-1) [Lecture 14](https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/lecture14.pdf) stanford.edu 3. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Schrijver_3-0)** [Schrijver (2005)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFSchrijver2005) 4. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTESedgewick2002_4-0)** [Sedgewick (2002)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFSedgewick2002). 5. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTEKleinbergTardos2006_5-0)** [Kleinberg & Tardos (2006)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFKleinbergTardos2006). 6. ^ [***a***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTECormenLeisersonRivestStein2022Section_22.1_6-0) [***b***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTECormenLeisersonRivestStein2022Section_22.1_6-1) [***c***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-FOOTNOTECormenLeisersonRivestStein2022Section_22.1_6-2) [Cormen et al. (2022)](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#CITEREFCormenLeisersonRivestStein2022), Section 22.1. 7. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-7)** Dinitz, Yefim; Itzhak, Rotem (2017-01-01). ["Hybrid Bellman–Ford–Dijkstra algorithm"](https://www.sciencedirect.com/science/article/pii/S1570866717300011). *Journal of Discrete Algorithms*. **42**: 35–44\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1016/j.jda.2017.01.001](https://doi.org/10.1016%2Fj.jda.2017.01.001). [ISSN](https://en.wikipedia.org/wiki/ISSN_\(identifier\) "ISSN (identifier)") [1570-8667](https://search.worldcat.org/issn/1570-8667). 8. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-8)** Malkin, Gary S. (November 1998). [RIP Version 2](https://www.rfc-editor.org/rfc/rfc2453) (Report). Internet Engineering Task Force. 9. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-9)** Duan, Fanding (1994). ["å…³äŗŽęœ€ēŸ­č·Æå¾„ēš„SPFAåæ«é€Ÿē®—ę³• \[About the SPFA algorithm\]"](http://wenku.baidu.com/view/3b8c5d778e9951e79a892705.html). *Journal of Southwest Jiaotong University*. **29** (2): 207–212\. 10. **[^](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-10)** Cormen et al., 4th ed., Problem 22-1, p. 640. 11. ^ [***a***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Sedweb_11-0) [***b***](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm#cite_ref-Sedweb_11-1) See Sedgewick's [web exercises](http://algs4.cs.princeton.edu/44sp/) for *Algorithms*, 4th ed., exercises 5 and 12 (retrieved 2013-01-30). - Shimbel, A. (1955). *Structure in communication nets*. Proceedings of the Symposium on Information Networks. New York, New York: Polytechnic Press of the Polytechnic Institute of Brooklyn. pp. 199–203\. - [Bellman, Richard](https://en.wikipedia.org/wiki/Richard_Bellman "Richard Bellman") (1958). ["On a routing problem"](https://doi.org/10.1090%2Fqam%2F102435). *Quarterly of Applied Mathematics*. **16**: 87–90\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1090/qam/102435](https://doi.org/10.1090%2Fqam%2F102435). [MR](https://en.wikipedia.org/wiki/MR_\(identifier\) "MR (identifier)") [0102435](https://mathscinet.ams.org/mathscinet-getitem?mr=0102435). - [Ford, Lester R. Jr.](https://en.wikipedia.org/wiki/L._R._Ford_Jr. "L. R. Ford Jr.") (August 14, 1956). [*Network Flow Theory*](http://www.rand.org/pubs/papers/P923.html). Paper P-923. Santa Monica, California: RAND Corporation. - [Moore, Edward F.](https://en.wikipedia.org/wiki/Edward_F._Moore "Edward F. Moore") (1959). *The shortest path through a maze*. Proc. Internat. Sympos. Switching Theory 1957, Part II. Cambridge, Massachusetts: Harvard Univ. Press. pp. 285–292\. [MR](https://en.wikipedia.org/wiki/MR_\(identifier\) "MR (identifier)") [0114710](https://mathscinet.ams.org/mathscinet-getitem?mr=0114710). - Yen, Jin Y. (1970). ["An algorithm for finding shortest routes from all source nodes to a given destination in general networks"](https://doi.org/10.1090%2Fqam%2F253822). *Quarterly of Applied Mathematics*. **27** (4): 526–530\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1090/qam/253822](https://doi.org/10.1090%2Fqam%2F253822). [MR](https://en.wikipedia.org/wiki/MR_\(identifier\) "MR (identifier)") [0253822](https://mathscinet.ams.org/mathscinet-getitem?mr=0253822). - Bannister, M. J.; [Eppstein, D.](https://en.wikipedia.org/wiki/David_Eppstein "David Eppstein") (2012). "Randomized speedup of the Bellman–Ford algorithm". *Analytic Algorithmics and Combinatorics (ANALCO12), Kyoto, Japan*. pp. 41–47\. [arXiv](https://en.wikipedia.org/wiki/ArXiv_\(identifier\) "ArXiv (identifier)"):[1111\.5414](https://arxiv.org/abs/1111.5414). [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1137/1.9781611973020.6](https://doi.org/10.1137%2F1.9781611973020.6). - Fineman, Jeremy T. (2024). "Single-source shortest paths with negative real weights in ![{\\displaystyle {\\tilde {O}}(mn^{8/9})}](https://wikimedia.org/api/rest_v1/media/math/render/svg/c0e5c63e4346832a930fcb0cc0db70ea35b66dde) time". In Mohar, Bojan; Shinkar, Igor; O'Donnell, Ryan (eds.). *Proceedings of the 56th Annual ACM Symposium on Theory of Computing, STOC 2024, Vancouver, BC, Canada, June 24–28, 2024*. Association for Computing Machinery. pp. 3–14\. [arXiv](https://en.wikipedia.org/wiki/ArXiv_\(identifier\) "ArXiv (identifier)"):[2311\.02520](https://arxiv.org/abs/2311.02520). [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1145/3618260.3649614](https://doi.org/10.1145%2F3618260.3649614). - [Ford, L. R. Jr.](https://en.wikipedia.org/wiki/L._R._Ford_Jr. "L. R. Ford Jr."); [Fulkerson, D. R.](https://en.wikipedia.org/wiki/D._R._Fulkerson "D. R. Fulkerson") (1962). "A shortest chain algorithm". *Flows in Networks*. Princeton University Press. pp. 130–134\. - Bang-Jensen, JĆørgen; Gutin, Gregory (2000). "Section 2.3.4: The Bellman-Ford-Moore algorithm". [*Digraphs: Theory, Algorithms and Applications*](http://www.cs.rhul.ac.uk/books/dbook/) (First ed.). Springer. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)") [978-1-84800-997-4](https://en.wikipedia.org/wiki/Special:BookSources/978-1-84800-997-4 "Special:BookSources/978-1-84800-997-4") . - Schrijver, Alexander (2005). ["On the history of combinatorial optimization (till 1960)"](http://homepages.cwi.nl/~lex/files/histco.pdf) (PDF). *Handbook of Discrete Optimization*. Elsevier: 1–68\. - [Cormen, Thomas H.](https://en.wikipedia.org/wiki/Thomas_H._Cormen "Thomas H. Cormen"); [Leiserson, Charles E.](https://en.wikipedia.org/wiki/Charles_E._Leiserson "Charles E. Leiserson"); [Rivest, Ronald L.](https://en.wikipedia.org/wiki/Ron_Rivest "Ron Rivest"); [Stein, Clifford](https://en.wikipedia.org/wiki/Clifford_Stein "Clifford Stein") (2022) \[1990\]. [*Introduction to Algorithms*](https://en.wikipedia.org/wiki/Introduction_to_Algorithms "Introduction to Algorithms") (4th ed.). MIT Press and McGraw-Hill. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)") [0-262-04630-X](https://en.wikipedia.org/wiki/Special:BookSources/0-262-04630-X "Special:BookSources/0-262-04630-X") . Section 22.1: The Bellman–Ford algorithm, pp. 612–616. Problem 22–1, p. 640. - Heineman, George T.; Pollice, Gary; Selkow, Stanley (2008). "Chapter 6: Graph Algorithms". *Algorithms in a Nutshell*. [O'Reilly Media](https://en.wikipedia.org/wiki/O%27Reilly_Media "O'Reilly Media"). pp. 160–164\. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)") [978-0-596-51624-6](https://en.wikipedia.org/wiki/Special:BookSources/978-0-596-51624-6 "Special:BookSources/978-0-596-51624-6") . - [Kleinberg, Jon](https://en.wikipedia.org/wiki/Jon_Kleinberg "Jon Kleinberg"); [Tardos, Ɖva](https://en.wikipedia.org/wiki/%C3%89va_Tardos "Ɖva Tardos") (2006). *Algorithm Design*. New York: Pearson Education, Inc. - [Sedgewick, Robert](https://en.wikipedia.org/wiki/Robert_Sedgewick_\(computer_scientist\) "Robert Sedgewick (computer scientist)") (2002). "Section 21.7: Negative Edge Weights". [*Algorithms in Java*](https://web.archive.org/web/20080531142256/http://safari.oreilly.com/0201361213/ch21lev1sec7) (3rd ed.). Addison-Wesley. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)") [0-201-36121-3](https://en.wikipedia.org/wiki/Special:BookSources/0-201-36121-3 "Special:BookSources/0-201-36121-3") . Archived from [the original](http://safari.oreilly.com/0201361213/ch21lev1sec7) on 2008-05-31. Retrieved 2007-05-28.
Shard152 (laksa)
Root Hash17790707453426894952
Unparsed URLorg,wikipedia!en,/wiki/Bellman%E2%80%93Ford_algorithm s443