<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Bellman%E2%80%93Ford_algorithm</id>
		<title>Bellman–Ford algorithm - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Bellman%E2%80%93Ford_algorithm"/>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;action=history"/>
		<updated>2026-04-26T00:28:25Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.25.2</generator>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2247&amp;oldid=prev</id>
		<title>162.158.62.18 at 09:05, 25 June 2022</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2247&amp;oldid=prev"/>
				<updated>2022-06-25T09:05:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 09:05, 25 June 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The '''Bellman-Ford algorithm''' finds [[Shortest_path#Single-source_shortest_paths|single-source shortest paths]] in a directed, weighted graph which contains no negative-weight cycles. That is, unlike [[Dijkstra's algorithm]], it is guaranteed to correctly compute shortest paths even when some edge weights are negative. (Note however that it is still a requirement that no negative-weight ''cycle'' occurs; finding shortest paths in such a graph becomes either meaningless if non-simple paths are allowed, or computationally difficult when they are not.) When single-source shortest paths are all that which is needed, and not [[Shortest path#All-pairs_shortest_paths|all-pairs shortest paths]], The Bellman–Ford algorithm, with time complexity &amp;lt;math&amp;gt;\mathcal{O}(VE)&amp;lt;/math&amp;gt;, outperforms the [[Floyd–Warshall algorithm]] at &amp;lt;math&amp;gt;\mathcal{O}(V^3)&amp;lt;/math&amp;gt; in sparse graphs. It may also be combined with Dijkstra's algorithm to yield [[Johnson's algorithm]], which again outperforms Floyd–Warshall in sparse graphs.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The '''Bellman-Ford algorithm''' finds [[Shortest_path#Single-source_shortest_paths|single-source shortest paths]] in a directed, weighted graph which contains no negative-weight cycles. That is, unlike [[Dijkstra's algorithm]], it is guaranteed to correctly compute shortest paths even when some edge weights are negative. (Note however that it is still a requirement that no negative-weight ''cycle'' occurs; finding shortest paths in such a graph becomes either meaningless if non-simple paths are allowed, or computationally difficult when they are not.) When single-source shortest paths are all that which is needed, and not [[Shortest path#All-pairs_shortest_paths|all-pairs shortest paths]], The Bellman–Ford algorithm, with time complexity &amp;lt;math&amp;gt;\mathcal{O}(VE)&amp;lt;/math&amp;gt;, outperforms the [[Floyd–Warshall algorithm]] at &amp;lt;math&amp;gt;\mathcal{O}(V^3)&amp;lt;/math&amp;gt; in sparse graphs. It may also be combined with Dijkstra's algorithm to yield [[Johnson's algorithm]], which again outperforms Floyd–Warshall in sparse graphs.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;input G,v&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;input G,v&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;for each u ∈ V(G)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;for each u ∈ V(G)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L12&quot; &gt;Line 12:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 12:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160;  if dist[w] &amp;gt; dist[u]+wt(u,w)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160;  if dist[w] &amp;gt; dist[u]+wt(u,w)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; error &amp;quot;Graph contains negative-weight cycles&amp;quot;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; error &amp;quot;Graph contains negative-weight cycles&amp;quot;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/pre&amp;gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;i&amp;gt;G&amp;lt;/i&amp;gt; is the directed, weighted graph in question, and &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; the source. The output is the array &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;; at the completion of the algorithm, &amp;lt;i&amp;gt;dist[x]&amp;lt;/i&amp;gt; contains the shortest-path distance from &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;. If the graph contains a cycle of negative weight, an error message is generated to that effect.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;i&amp;gt;G&amp;lt;/i&amp;gt; is the directed, weighted graph in question, and &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; the source. The output is the array &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;; at the completion of the algorithm, &amp;lt;i&amp;gt;dist[x]&amp;lt;/i&amp;gt; contains the shortest-path distance from &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;. If the graph contains a cycle of negative weight, an error message is generated to that effect.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>162.158.62.18</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2246&amp;oldid=prev</id>
		<title>162.158.62.94: removed unneeded html tag</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2246&amp;oldid=prev"/>
				<updated>2022-06-25T09:02:24Z</updated>
		
		<summary type="html">&lt;p&gt;removed unneeded html tag&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 09:02, 25 June 2022&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L12&quot; &gt;Line 12:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 12:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160;  if dist[w] &amp;gt; dist[u]+wt(u,w)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160;  if dist[w] &amp;gt; dist[u]+wt(u,w)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; error &amp;quot;Graph contains negative-weight cycles&amp;quot;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&amp;#160; &amp;#160; &amp;#160; &amp;#160; &amp;#160; error &amp;quot;Graph contains negative-weight cycles&amp;quot;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;/pre&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;i&amp;gt;G&amp;lt;/i&amp;gt; is the directed, weighted graph in question, and &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; the source. The output is the array &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;; at the completion of the algorithm, &amp;lt;i&amp;gt;dist[x]&amp;lt;/i&amp;gt; contains the shortest-path distance from &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;. If the graph contains a cycle of negative weight, an error message is generated to that effect.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;i&amp;gt;G&amp;lt;/i&amp;gt; is the directed, weighted graph in question, and &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; the source. The output is the array &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;; at the completion of the algorithm, &amp;lt;i&amp;gt;dist[x]&amp;lt;/i&amp;gt; contains the shortest-path distance from &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;x&amp;lt;/i&amp;gt;. If the graph contains a cycle of negative weight, an error message is generated to that effect.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>162.158.62.94</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2155&amp;oldid=prev</id>
		<title>162.158.146.132: /* Intuitive explanation */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2155&amp;oldid=prev"/>
				<updated>2017-11-03T00:35:15Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Intuitive explanation&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 00:35, 3 November 2017&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L16&quot; &gt;Line 16:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 16:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Theory of the algorithm==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Theory of the algorithm==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;===Intuitive explanation===&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The algorithm works by performing a series of &amp;lt;i&amp;gt;relaxations&amp;lt;/i&amp;gt;. A relaxation occurs whenever the current shortest distance from node &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; to node &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; is improved because, by travelling from &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; to some intermediate vertex &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt;, and then from &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;, a shorter path is obtained. (Floyd–Warshall and Dijkstra's algorithms rely upon this same technique.) The key is that, after &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; passes of the main loop in Bellman–Ford have completed, at least &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt;+1 of the shortest-path distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. (We consider all pairs of vertices to be connected, so that all &amp;quot;missing&amp;quot; edges are assigned a weight of positive infinity.)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The algorithm works by performing a series of &amp;lt;i&amp;gt;relaxations&amp;lt;/i&amp;gt;. A relaxation occurs whenever the current shortest distance from node &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; to node &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; is improved because, by travelling from &amp;lt;i&amp;gt;v&amp;lt;/i&amp;gt; to some intermediate vertex &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt;, and then from &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;, a shorter path is obtained. (Floyd–Warshall and Dijkstra's algorithms rely upon this same technique.) The key is that, after &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt; passes of the main loop in Bellman–Ford have completed, at least &amp;lt;i&amp;gt;n&amp;lt;/i&amp;gt;+1 of the shortest-path distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. (We consider all pairs of vertices to be connected, so that all &amp;quot;missing&amp;quot; edges are assigned a weight of positive infinity.)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>162.158.146.132</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2154&amp;oldid=prev</id>
		<title>162.158.146.132: /* The algorithm */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2154&amp;oldid=prev"/>
				<updated>2017-11-03T00:34:56Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;The algorithm&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 00:34, 3 November 2017&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The '''Bellman-Ford algorithm''' finds [[Shortest_path#Single-source_shortest_paths|single-source shortest paths]] in a directed, weighted graph which contains no negative-weight cycles. That is, unlike [[Dijkstra's algorithm]], it is guaranteed to correctly compute shortest paths even when some edge weights are negative. (Note however that it is still a requirement that no negative-weight ''cycle'' occurs; finding shortest paths in such a graph becomes either meaningless if non-simple paths are allowed, or computationally difficult when they are not.) When single-source shortest paths are all that which is needed, and not [[Shortest path#All-pairs_shortest_paths|all-pairs shortest paths]], The Bellman–Ford algorithm, with time complexity &amp;lt;math&amp;gt;\mathcal{O}(VE)&amp;lt;/math&amp;gt;, outperforms the [[Floyd–Warshall algorithm]] at &amp;lt;math&amp;gt;\mathcal{O}(V^3)&amp;lt;/math&amp;gt; in sparse graphs. It may also be combined with Dijkstra's algorithm to yield [[Johnson's algorithm]], which again outperforms Floyd–Warshall in sparse graphs.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The '''Bellman-Ford algorithm''' finds [[Shortest_path#Single-source_shortest_paths|single-source shortest paths]] in a directed, weighted graph which contains no negative-weight cycles. That is, unlike [[Dijkstra's algorithm]], it is guaranteed to correctly compute shortest paths even when some edge weights are negative. (Note however that it is still a requirement that no negative-weight ''cycle'' occurs; finding shortest paths in such a graph becomes either meaningless if non-simple paths are allowed, or computationally difficult when they are not.) When single-source shortest paths are all that which is needed, and not [[Shortest path#All-pairs_shortest_paths|all-pairs shortest paths]], The Bellman–Ford algorithm, with time complexity &amp;lt;math&amp;gt;\mathcal{O}(VE)&amp;lt;/math&amp;gt;, outperforms the [[Floyd–Warshall algorithm]] at &amp;lt;math&amp;gt;\mathcal{O}(V^3)&amp;lt;/math&amp;gt; in sparse graphs. It may also be combined with Dijkstra's algorithm to yield [[Johnson's algorithm]], which again outperforms Floyd–Warshall in sparse graphs.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;==The algorithm==&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;#160;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;The idea behind the algorithm is very easy to understand, and may be satisfactorily illustrated by the following pseudocode:&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;&amp;lt;pre&amp;gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;input G,v&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;input G,v&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;for each u ∈ V(G)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;for each u ∈ V(G)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>162.158.146.132</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2022&amp;oldid=prev</id>
		<title>141.52.249.2: /* Proof of correctness for graphs containing no negative-weight cycles */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2022&amp;oldid=prev"/>
				<updated>2016-10-04T17:49:41Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Proof of correctness for graphs containing no negative-weight cycles&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 17:49, 4 October 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L24&quot; &gt;Line 24:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 24:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;N=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;N=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(N+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(N+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>141.52.249.2</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2021&amp;oldid=prev</id>
		<title>141.52.249.2: /* Proof of correctness for graphs containing no negative-weight cycles */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2021&amp;oldid=prev"/>
				<updated>2016-10-04T17:46:37Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Proof of correctness for graphs containing no negative-weight cycles&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 17:46, 4 October 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L24&quot; &gt;Line 24:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 24:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;N=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;N=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(N+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(N+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>141.52.249.2</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2020&amp;oldid=prev</id>
		<title>141.52.249.2: /* Proof of correctness for graphs containing no negative-weight cycles */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2020&amp;oldid=prev"/>
				<updated>2016-10-04T17:46:26Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Proof of correctness for graphs containing no negative-weight cycles&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 17:46, 4 October 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L23&quot; &gt;Line 23:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 23:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Proof of correctness for graphs containing no negative-weight cycles===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Proof of correctness for graphs containing no negative-weight cycles===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(N+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(N+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>141.52.249.2</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2019&amp;oldid=prev</id>
		<title>141.52.249.2: /* Proof of correctness for graphs containing no negative-weight cycles */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2019&amp;oldid=prev"/>
				<updated>2016-10-04T17:46:13Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Proof of correctness for graphs containing no negative-weight cycles&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 17:46, 4 October 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L23&quot; &gt;Line 23:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 23:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Proof of correctness for graphs containing no negative-weight cycles===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Proof of correctness for graphs containing no negative-weight cycles===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/del&gt;=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/ins&gt;=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(N+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;N&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;N+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(N+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>141.52.249.2</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2018&amp;oldid=prev</id>
		<title>141.52.249.2: /* Proof of correctness for graphs containing no negative-weight cycles */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2018&amp;oldid=prev"/>
				<updated>2016-10-04T17:46:00Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Proof of correctness for graphs containing no negative-weight cycles&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 17:46, 4 October 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L24&quot; &gt;Line 24:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 24:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;N=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;N=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>141.52.249.2</name></author>	</entry>

	<entry>
		<id>http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2017&amp;oldid=prev</id>
		<title>141.52.249.2: /* Proof of correctness for graphs containing no negative-weight cycles */</title>
		<link rel="alternate" type="text/html" href="http://wcipeg.com/wiki/index.php?title=Bellman%E2%80%93Ford_algorithm&amp;diff=2017&amp;oldid=prev"/>
				<updated>2016-10-04T17:45:34Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Proof of correctness for graphs containing no negative-weight cycles&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 17:45, 4 October 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L23&quot; &gt;Line 23:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 23:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Proof of correctness for graphs containing no negative-weight cycles===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Proof of correctness for graphs containing no negative-weight cycles===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We proceed by induction:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n&lt;/del&gt;=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* When &amp;lt;math&amp;gt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;N&lt;/ins&gt;=0&amp;lt;/math&amp;gt;, there is at least 1 correct entry in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt;, the one stating that the distance from the source to itself is zero.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(n+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;* Now suppose that &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; passes have occurred and that we know the shortest-path distances from the source to &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; of the vertices. Now, either &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; is equal to &amp;lt;math&amp;gt;V-1&amp;lt;/math&amp;gt;, and we are done, or the vertices may be partitioned into two sets: &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;, which contains &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; vertices for which we already know shortest-path distances (with any &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; being chosen if there are more than this number), and &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt;, which contains the rest. Now, since a shortest-paths tree exists (it always does when there are no negative-weight cycles; the proof is in the [[Shortest path]] article), there must exist some vertex &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt; in &amp;lt;math&amp;gt;\overline{S}&amp;lt;/math&amp;gt; whose parent &amp;lt;i&amp;gt;u&amp;lt;/i&amp;gt; in the shortest-paths tree is in &amp;lt;math&amp;gt;S&amp;lt;/math&amp;gt;. Then, when the edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; is relaxed, the &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; array will contain the correct shortest-path distance to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Thus, after the next pass of the outer loop has occurred, &amp;lt;math&amp;gt;n+1&amp;lt;/math&amp;gt; passes will have occurred in total, and the shortest-path distances to at least &amp;lt;math&amp;gt;(n+1)+1&amp;lt;/math&amp;gt; vertices will be correctly known.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Thus, when a negative-weight cycle does not exist, after the main loop has finished, all distances in &amp;lt;i&amp;gt;dist&amp;lt;/i&amp;gt; are correct. Now, if an edge &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; still exists such that &amp;lt;code&amp;gt;dist[w] &amp;gt; dist[u]+wt(u,w)&amp;lt;/code&amp;gt;, then the distances could not possibly have been correct, because relaxation of &amp;lt;i&amp;gt;(u,w)&amp;lt;/i&amp;gt; would give a shorter path to &amp;lt;i&amp;gt;w&amp;lt;/i&amp;gt;. Since this is a contradiction, the assumption of the non-existence of negative-weight cycles must be incorrect in this case. We see then that as long as there are no negative-weight cycles, the algorithm always computes all distances correctly and terminates successfully.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>141.52.249.2</name></author>	</entry>

	</feed>