<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.formulasearchengine.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=AaliyahT21</id>
	<title>formulasearchengine - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.formulasearchengine.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=AaliyahT21"/>
	<link rel="alternate" type="text/html" href="https://en.formulasearchengine.com/wiki/Special:Contributions/AaliyahT21"/>
	<updated>2026-05-24T03:00:34Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0-wmf.28</generator>
	<entry>
		<id>https://en.formulasearchengine.com/index.php?title=Main_Page&amp;diff=39099</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://en.formulasearchengine.com/index.php?title=Main_Page&amp;diff=39099"/>
		<updated>2014-08-10T17:39:44Z</updated>

		<summary type="html">&lt;p&gt;AaliyahT21: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Infobox Algorithm&lt;br /&gt;
|class=[[Single-source shortest path problem]] (for weighted directed graphs)&lt;br /&gt;
|image=&lt;br /&gt;
|caption = &lt;br /&gt;
|data=[[Graph (data structure)|Graph]]&lt;br /&gt;
|time=&amp;lt;math&amp;gt;O (|V| |E|)&amp;lt;/math&amp;gt;&lt;br /&gt;
|space=&amp;lt;math&amp;gt;O (|V|)&amp;lt;/math&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{Tree search algorithm}}&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;Bellman–Ford algorithm&#039;&#039;&#039; computes single-source [[shortest path]]s in a [[weighted digraph]].&amp;lt;ref name=&amp;quot;Bang&amp;quot;&amp;gt;{{Cite book|first=Jørgen |last=Bang-Jensen||coauthors=Gregory Gutin|title=Digraphs: Theory, Algorithms and Applications|edition=First |isbn=978-1-84800-997-4|chapter=Section 2.3.4: The Bellman-Ford-Moore algorithm|url=http://www.cs.rhul.ac.uk/books/dbook/}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
For graphs with only non-negative edge weights, the faster [[Dijkstra&#039;s algorithm]] also solves the problem.&lt;br /&gt;
Thus, Bellman–Ford is used primarily for graphs with negative edge weights.&lt;br /&gt;
The algorithm is named after its developers, [[Richard Bellman]] and [[L. R. Ford, Jr.|Lester Ford, Jr.]]&lt;br /&gt;
&lt;br /&gt;
Negative edge weights are found in various applications of graphs, hence the usefulness of this algorithm.&amp;lt;ref&amp;gt;{{Cite book|first=Robert |last=Sedgewick|title= Algorithms in Java|edition= Third |isbn= 0-201-36121-3|chapter=Section 21.7: Negative Edge Weights|url=http://safari.oreilly.com/0201361213/ch21lev1sec7|ref=harv|postscript=&amp;lt;!-- Bot inserted parameter. Either remove it; or change its value to &amp;quot;.&amp;quot; for the cite to end in a &amp;quot;.&amp;quot;, as necessary. --&amp;gt;{{inconsistent citations}}}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
However, if a graph contains a &amp;quot;negative cycle&amp;quot;, i.e., a [[cycle (graph theory)|cycle]] whose edges sum to a negative value, then [[Walk (graph theory)|walks]] of arbitrarily low weight can be constructed by repeatedly following the cycle, so there may not be a &#039;&#039;shortest&#039;&#039; path.&lt;br /&gt;
In such a case, the Bellman-Ford algorithm can detect negative cycles and report their existence, but it cannot produce a correct &amp;quot;shortest path&amp;quot; answer if a negative cycle is reachable from the source.&amp;lt;ref&amp;gt;Kleinberg, Jon; Tardos, Éva (2006), Algorithm Design, New York: Pearson Education, Inc..&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;Bang&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Algorithm==&lt;br /&gt;
Bellman–Ford is based on [[dynamic programming]] approach. In its basic structure it is similar to [[Dijkstra&#039;s Algorithm]], but instead of [[Greedy algorithm|greedily]] selecting the minimum-weight node not yet processed to relax, it simply relaxes &#039;&#039;all&#039;&#039; the edges, and does this |&#039;&#039;V&#039;&#039; |&amp;amp;nbsp;&amp;amp;minus;&amp;amp;nbsp;1 times, where |&#039;&#039;V&#039;&#039; | is the number of vertices in the graph. The repetitions allow minimum distances to propagate accurately throughout the graph, since, in the absence of negative cycles, the shortest path can visit each node at most only once. Unlike the greedy approach, which depends on certain structural assumptions derived from positive weights, this straightforward approach extends to the general case.&lt;br /&gt;
&lt;br /&gt;
Bellman–Ford runs in &#039;&#039;[[Big O notation|O]]&#039;&#039;(|&#039;&#039;V&#039;&#039;|·|&#039;&#039;E&#039;&#039;|) time, where |&#039;&#039;V&#039;&#039;| and |&#039;&#039;E&#039;&#039;| are the number of vertices and edges respectively.&lt;br /&gt;
&lt;br /&gt;
 &#039;&#039;&#039;procedure&#039;&#039;&#039; BellmanFord(&#039;&#039;list&#039;&#039; vertices, &#039;&#039;list&#039;&#039; edges, &#039;&#039;vertex&#039;&#039; source)&lt;br /&gt;
    &#039;&#039;// This implementation takes in a graph, represented as lists of vertices&#039;&#039;&lt;br /&gt;
    &#039;&#039;// and edges, and modifies the vertices so that their &#039;&#039;distance&#039;&#039; and&#039;&#039;&lt;br /&gt;
    &#039;&#039;//&#039;&#039; predecessor &#039;&#039;attributes store the shortest paths.&#039;&#039;&lt;br /&gt;
 &lt;br /&gt;
    &#039;&#039;// Step 1: initialize graph&#039;&#039;&lt;br /&gt;
    &#039;&#039;&#039;for each&#039;&#039;&#039; vertex v &#039;&#039;&#039;in&#039;&#039;&#039; vertices:&lt;br /&gt;
        &#039;&#039;&#039;if&#039;&#039;&#039; v &#039;&#039;&#039;is&#039;&#039;&#039; source &#039;&#039;&#039;then&#039;&#039;&#039; v.distance := 0&lt;br /&gt;
        &#039;&#039;&#039;else&#039;&#039;&#039; v.distance := &#039;&#039;&#039;infinity&#039;&#039;&#039;&lt;br /&gt;
        v.predecessor := &#039;&#039;&#039;null&#039;&#039;&#039;&lt;br /&gt;
 &lt;br /&gt;
    &#039;&#039;// Step 2: relax edges repeatedly&#039;&#039;&lt;br /&gt;
    &#039;&#039;&#039;for&#039;&#039;&#039; i &#039;&#039;&#039;from&#039;&#039;&#039; 1 &#039;&#039;&#039;to&#039;&#039;&#039; size(vertices)-1:&lt;br /&gt;
        &#039;&#039;&#039;for each&#039;&#039;&#039; edge uv &#039;&#039;&#039;in&#039;&#039;&#039; edges: &#039;&#039;// uv is the edge from u to v&#039;&#039;&lt;br /&gt;
            u := uv.source&lt;br /&gt;
            v := uv.destination&lt;br /&gt;
            &#039;&#039;&#039;if&#039;&#039;&#039; u.distance + uv.weight &amp;lt; v.distance:&lt;br /&gt;
                v.distance := u.distance + uv.weight&lt;br /&gt;
                v.predecessor := u&lt;br /&gt;
 &lt;br /&gt;
    &#039;&#039;// Step 3: check for negative-weight cycles&#039;&#039;&lt;br /&gt;
    &#039;&#039;&#039;for each&#039;&#039;&#039; edge uv &#039;&#039;&#039;in&#039;&#039;&#039; edges:&lt;br /&gt;
        u := uv.source&lt;br /&gt;
        v := uv.destination&lt;br /&gt;
        &#039;&#039;&#039;if&#039;&#039;&#039; u.distance + uv.weight &amp;lt; v.distance:&lt;br /&gt;
            &#039;&#039;&#039;error&#039;&#039;&#039; &amp;quot;Graph contains a negative-weight cycle&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Proof of correctness==&lt;br /&gt;
&lt;br /&gt;
The correctness of the algorithm can be shown by [[mathematical induction|induction]]. The precise statement shown by induction is:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Lemma&#039;&#039;&#039;. After &#039;&#039;i&#039;&#039; repetitions of &#039;&#039;for&#039;&#039; cycle:&lt;br /&gt;
* If Distance(&#039;&#039;u&#039;&#039;) is not infinity, it is equal to the length of some path from &#039;&#039;s&#039;&#039; to &#039;&#039;u&#039;&#039;;&lt;br /&gt;
* If there is a path from &#039;&#039;s&#039;&#039; to &#039;&#039;u&#039;&#039; with at most &#039;&#039;i&#039;&#039; edges, then Distance(u) is at most the length of the shortest path from &#039;&#039;s&#039;&#039; to &#039;&#039;u&#039;&#039; with at most &#039;&#039;i&#039;&#039; edges.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Proof&#039;&#039;&#039;. For the base case of induction, consider &amp;lt;code&amp;gt;i=0&amp;lt;/code&amp;gt; and the moment before &#039;&#039;for&#039;&#039; cycle is executed for the first time. Then, for the source vertex, &amp;lt;code&amp;gt;source.distance = 0&amp;lt;/code&amp;gt;, which is correct. For other vertices &#039;&#039;u&#039;&#039;, &amp;lt;code&amp;gt;u.distance = &#039;&#039;&#039;infinity&#039;&#039;&#039;&amp;lt;/code&amp;gt;, which is also correct because there is no path from &#039;&#039;source&#039;&#039; to &#039;&#039;u&#039;&#039; with 0 edges.&lt;br /&gt;
&lt;br /&gt;
For the inductive case, we first prove the first part. Consider a moment when a vertex&#039;s distance is updated by&lt;br /&gt;
&amp;lt;code&amp;gt;v.distance := u.distance + uv.weight&amp;lt;/code&amp;gt;. By inductive assumption, &amp;lt;code&amp;gt;u.distance&amp;lt;/code&amp;gt; is the length of some path from &#039;&#039;source&#039;&#039; to &#039;&#039;u&#039;&#039;. Then &amp;lt;code&amp;gt;u.distance + uv.weight&amp;lt;/code&amp;gt; is the length of the path from &#039;&#039;source&#039;&#039; to &#039;&#039;v&#039;&#039; that follows the path from  &#039;&#039;source&#039;&#039; to &#039;&#039;u&#039;&#039; and then goes to &#039;&#039;v&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
For the second part, consider the shortest path from &#039;&#039;source&#039;&#039; to &#039;&#039;u&#039;&#039; with at most &#039;&#039;i&#039;&#039; edges. Let &#039;&#039;v&#039;&#039; be the last vertex before &#039;&#039;u&#039;&#039; on this path. Then, the part of the path from &#039;&#039;source&#039;&#039; to &#039;&#039;v&#039;&#039; is the shortest path from &#039;&#039;source&#039;&#039; to &#039;&#039;v&#039;&#039; with at most &#039;&#039;i-1&#039;&#039; edges. By inductive assumption, &amp;lt;code&amp;gt;v.distance&amp;lt;/code&amp;gt; after &#039;&#039;i&#039;&#039;−1 cycles is at most the length of this path. Therefore, &amp;lt;code&amp;gt;uv.weight + v.distance&amp;lt;/code&amp;gt; is at most the length of the path from &#039;&#039;s&#039;&#039; to &#039;&#039;u&#039;&#039;. In the &#039;&#039;i&amp;lt;sup&amp;gt;th&amp;lt;/sup&amp;gt;&#039;&#039; cycle, &amp;lt;code&amp;gt;u.distance&amp;lt;/code&amp;gt; gets compared with &amp;lt;code&amp;gt;uv.weight + v.distance&amp;lt;/code&amp;gt;, and is set equal to it if &amp;lt;code&amp;gt;uv.weight + v.distance&amp;lt;/code&amp;gt; was smaller. Therefore, after &#039;&#039;i&#039;&#039; cycles, &amp;lt;code&amp;gt;u.distance&amp;lt;/code&amp;gt; is at most the length of the shortest path from &#039;&#039;source&#039;&#039; to &#039;&#039;u&#039;&#039; that uses at most &#039;&#039;i&#039;&#039; edges.&lt;br /&gt;
&lt;br /&gt;
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 &#039;&#039;v&#039;&#039;[0], ..., &#039;&#039;v&#039;&#039;[&#039;&#039;k&#039;&#039;−1],&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;v[i].distance &amp;lt;= v[(i-1) mod k].distance + v[(i-1) mod k]v[i].weight&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Summing around the cycle, the &#039;&#039;v&#039;&#039;[&#039;&#039;i&#039;&#039;].distance terms and the &#039;&#039;v&#039;&#039;[&#039;&#039;i&#039;&#039;−1 (mod &#039;&#039;k&#039;&#039;)] distance terms cancel, leaving&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;0 &amp;lt;= sum from 1 to k of v[i-1 (mod k)]v[i].weight&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
I.e., every cycle has nonnegative weight.&lt;br /&gt;
&lt;br /&gt;
==Finding negative cycles==&lt;br /&gt;
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 [[Flow network|network flow]] analysis.&amp;lt;ref name=&amp;quot;Bang&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Applications in routing ==&lt;br /&gt;
&lt;br /&gt;
A distributed variant of the Bellman–Ford algorithm is used in [[distance-vector routing protocol]]s, for example the [[Routing Information Protocol]] (RIP). The algorithm is distributed because it involves a number of nodes (routers) within an [[autonomous system (Internet)|Autonomous system]], a collection of IP networks typically owned by an ISP.&lt;br /&gt;
It consists of the following steps:&lt;br /&gt;
&lt;br /&gt;
# Each node calculates the distances between itself and all other nodes within the AS and stores this information as a table.&lt;br /&gt;
# Each node sends its table to all neighboring nodes.&lt;br /&gt;
# 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.&lt;br /&gt;
&lt;br /&gt;
The main disadvantages of the Bellman–Ford algorithm in this setting are as follows:&lt;br /&gt;
&lt;br /&gt;
* It does not scale well.&lt;br /&gt;
* Changes in [[network topology]] are not reflected quickly since updates are spread node-by-node.&lt;br /&gt;
* [[Count to infinity#Count-to-infinity problem|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).&lt;br /&gt;
&lt;br /&gt;
==Yen&#039;s improvement==&lt;br /&gt;
{{harvtxt|Yen|1970}} described an improvement to the Bellman–Ford algorithm for a graph without negative-weight cycles. Yen&#039;s improvement first assigns some arbitrary linear order on all vertices and then partitions the set of all edges into one of two subsets. The first subset, &#039;&#039;E&amp;lt;sub&amp;gt;f&amp;lt;/sub&amp;gt;&#039;&#039;, contains all edges (&#039;&#039;v&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;&#039;&#039;, &#039;&#039;v&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt;&#039;&#039;) such that &#039;&#039;i&#039;&#039; &amp;lt; &#039;&#039;j&#039;&#039;; the second, &#039;&#039;E&amp;lt;sub&amp;gt;b&amp;lt;/sub&amp;gt;&#039;&#039;, contains edges (&#039;&#039;v&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;&#039;&#039;, &#039;&#039;v&amp;lt;sub&amp;gt;j&amp;lt;/sub&amp;gt;&#039;&#039;) such that &#039;&#039;i&#039;&#039; &amp;gt; &#039;&#039;j&#039;&#039;. Each vertex is visited in the order &#039;&#039;v&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;&#039;&#039;, &#039;&#039;v&amp;lt;sub&amp;gt;2&amp;lt;/sub&amp;gt;&#039;&#039;, ..., &#039;&#039;v&#039;&#039;&amp;lt;sub&amp;gt;|&#039;&#039;V&#039;&#039;|&amp;lt;/sub&amp;gt;, relaxing each outgoing edge from that vertex in &#039;&#039;E&amp;lt;sub&amp;gt;f&amp;lt;/sub&amp;gt;&#039;&#039;. Each vertex is then visited in the order &#039;&#039;v&#039;&#039;&amp;lt;sub&amp;gt;|&#039;&#039;V&#039;&#039;|&amp;lt;/sub&amp;gt;, &#039;&#039;v&#039;&#039;&amp;lt;sub&amp;gt;|&#039;&#039;V&#039;&#039;|−1&amp;lt;/sub&amp;gt;, ..., &#039;&#039;v&#039;&#039;&amp;lt;sub&amp;gt;1&amp;lt;/sub&amp;gt;, relaxing each outgoing edge from that vertex in &#039;&#039;E&amp;lt;sub&amp;gt;b&amp;lt;/sub&amp;gt;&#039;&#039;. Yen&#039;s improvement effectively halves the number of &amp;quot;passes&amp;quot; required for the single-source-shortest-path solution.&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*{{cite journal&lt;br /&gt;
 | last = Bellman | first = Richard | authorlink = Richard Bellman&lt;br /&gt;
 | mr = 0102435&lt;br /&gt;
 | journal = Quarterly of Applied Mathematics&lt;br /&gt;
 | pages = 87–90&lt;br /&gt;
 | title = On a routing problem&lt;br /&gt;
 | volume = 16&lt;br /&gt;
 | year = 1958&lt;br /&gt;
 | ref = harv}}.&lt;br /&gt;
*{{cite book|author1-link=L. R. Ford, Jr.|last1=Ford|first1=L. R., Jr.|author2-link=D. R. Fulkerson|last2=Fulkerson|first2=D. R.|title=Flows in Networks|publisher=Princeton University Press|year=1962}}.&lt;br /&gt;
*{{Introduction to Algorithms}}, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Section 24.1: The Bellman–Ford algorithm, pp.&amp;amp;nbsp;588&amp;amp;ndash;592. Problem 24-1, pp.&amp;amp;nbsp;614&amp;amp;ndash;615.&lt;br /&gt;
*{{Introduction to Algorithms}}, Third Edition. MIT Press, 2009. ISBN 978-0-262-53305-8. Section 24.1: The Bellman–Ford algorithm, pp.&amp;amp;nbsp;651&amp;amp;ndash;655.&lt;br /&gt;
*{{cite book | first1 = George T. | last1 = Heineman | first2 = Gary | last2 = Pollice | first3 = Stanley | last3 = Selkow | title= Algorithms in a Nutshell | publisher=[[O&#039;Reilly Media]] | year=2008 | chapter=Chapter 6: Graph Algorithms | pages = 160–164 | isbn=978-0-596-51624-6 }}&lt;br /&gt;
*{{cite journal&lt;br /&gt;
 | last = Yen | first = Jin Y.&lt;br /&gt;
 | mr = 0253822&lt;br /&gt;
 | journal = Quarterly of Applied Mathematics&lt;br /&gt;
 | pages = 526–530&lt;br /&gt;
 | title = An algorithm for finding shortest routes from all source nodes to a given destination in general networks&lt;br /&gt;
 | volume = 27&lt;br /&gt;
 | year = 1970&lt;br /&gt;
 | ref = harv}}.&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://snippets.dzone.com/posts/show/4828 C code example]&lt;br /&gt;
* [http://code.google.com/p/annas/ Open Source Java Graph package with Bellman-Ford Algorithms]&lt;br /&gt;
* [http://www.technical-recipes.com/2012/the-k-shortest-paths-algorithm-in-c/ C++ implementation of Yen&#039;s Algorithm (Microsoft Visual Studio project)]&lt;br /&gt;
&lt;br /&gt;
[[Category:Graph algorithms]]&lt;br /&gt;
[[Category:Polynomial-time problems]]&lt;br /&gt;
[[Category:Articles with example C code]]&lt;br /&gt;
[[Category:Articles with example pseudocode]]&lt;br /&gt;
&lt;br /&gt;
[[ca:Algorisme de Bellman-Ford]]&lt;br /&gt;
[[cs:Bellmanův-Fordův algoritmus]]&lt;br /&gt;
[[de:Bellman-Ford-Algorithmus]]&lt;br /&gt;
[[es:Algoritmo de Bellman-Ford]]&lt;br /&gt;
[[fa:الگوریتم بلمن–فورد]]&lt;br /&gt;
[[fr:Algorithme de Bellman-Ford]]&lt;br /&gt;
[[ko:벨만-포드 알고리즘]]&lt;br /&gt;
[[it:Algoritmo di Bellman-Ford]]&lt;br /&gt;
[[he:אלגוריתם בלמן-פורד]]&lt;br /&gt;
[[ka:ბელმან-ფორდის ალგორითმი]]&lt;br /&gt;
[[lv:Bellmana-Forda algoritms]]&lt;br /&gt;
[[nl:Algoritme van Bellman-Ford]]&lt;br /&gt;
[[ja:ベルマン-フォード法]]&lt;br /&gt;
[[pl:Algorytm Bellmana-Forda]]&lt;br /&gt;
[[pt:Algoritmo de Bellman-Ford]]&lt;br /&gt;
[[ru:Алгоритм Беллмана — Форда]]&lt;br /&gt;
[[th:ขั้นตอนวิธีของเบลแมน-ฟอร์ด]]&lt;br /&gt;
[[uk:Алгоритм Беллмана—Форда]]&lt;br /&gt;
[[vi:Thuật toán Bellman-Ford]]&lt;br /&gt;
[[zh:贝尔曼-福特算法]]&lt;/div&gt;</summary>
		<author><name>AaliyahT21</name></author>
	</entry>
</feed>