{"id":139,"date":"2026-11-13T12:32:29","date_gmt":"2026-11-13T04:32:29","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bellman-ford-relaxes-every-edge-v-minus-1-times\/"},"modified":"2026-11-13T21:51:08","modified_gmt":"2026-11-13T13:51:08","slug":"how-bellman-ford-relaxes-every-edge-v-minus-1-times","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bellman-ford-relaxes-every-edge-v-minus-1-times\/","title":{"rendered":"How Bellman-Ford relaxes every edge V minus 1 times"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Bellman-Ford<\/strong> relaxes every edge once per round, for <strong>|V|\u22121<\/strong> rounds. A shortest path is simple when no negative cycle is reachable, so it has at most |V|\u22121 edges, and round k has considered every path of k edges. One more round that still relaxes an edge is a <strong>negative cycle<\/strong> reachable from the source. The time is <strong>\u0398(VE)<\/strong>. Negative edges are allowed. The queue optimization of the same relaxations is SPFA, whose worst case is no better.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Dijkstra&#8217;s settle step assumes a path never gets cheaper by growing. A negative edge makes that false, so a vertex cannot be closed the first time it looks finished. The replacement is exhaustive: if a better path with at most k edges exists, it will be found by relaxing every edge onto the distances produced with at most k\u22121 edges.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A negative cycle reachable from the source makes distances undefined, not merely slow. The algorithm&#8217;s second result is a witness of that cycle, from the parent pointers of a vertex that improves on round |V|.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">After |V|\u22121 full passes over the edge list, dist[v] is the shortest-path distance from the source whenever no negative cycle is reachable, and infinity when v is unreachable. A further pass that updates any distance proves a negative cycle is reachable from the source. Walking parent pointers from the updated vertex, far enough to repeat a vertex, extracts a cycle. Time is \u0398(VE) with an adjacency list of E edges. Extra memory is \u0398(V).<\/p>\n\n\n<p class=\"wp-block-paragraph\">A pass that performs no update can stop early. That optimization is safe and does not change the worst case. Difference constraints of the form x_i \u2212 x_j \u2264 w_k are feasible exactly when a constraint graph has no negative cycle, so the same algorithm decides them.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Set dist[source] = 0 and every other distance to infinity.<\/li>\n<li>Repeat |V|\u22121 times: for each edge u\u2192v of weight w, if dist[u] is finite and dist[u]+w &lt; dist[v], set dist[v] = dist[u]+w and parent[v] = u.<\/li>\n<li>Scan every edge once more. An update on this extra pass means a negative cycle is reachable from the source.<\/li>\n<li>To extract the cycle, start from the vertex just updated and follow parents |V| times so the walk is inside the cycle, then follow parents until a vertex repeats.<\/li>\n<li>Optional: stop the main loop after a pass that changes nothing. The extra pass is then unnecessary only if that early stop happened.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The O(VE) bound is the general single-source bound with negative edges. Nonnegative edges do not make the loop faster than its early-exit special case, and Dijkstra is the algorithm that actually uses nonnegativity. A queue of only the vertices whose distances just changed is SPFA. It is the same relaxation order with a different schedule, and its worst case is still \u0398(VE).<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when a negative cycle is treated as a long path<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Stop after V\u22121 passes and always trust the distances<\/td>\n<td>A reachable negative cycle makes some distances able to fall forever. The extra pass is the test that separates a finite answer from that case.<\/td>\n<\/tr>\n<tr>\n<td>Dijkstra on the same graph because the negatives look rare<\/td>\n<td>One reachable negative edge is enough to settle a vertex too early. Rarity is not the hypothesis.<\/td>\n<\/tr>\n<tr>\n<td>Relax edges in a fixed order and skip the extra pass because a teaching example had no cycle<\/td>\n<td>The algorithm without the detection pass returns numbers that are not distances when a cycle exists.<\/td>\n<\/tr>\n<tr>\n<td>Replace the loop with SPFA and quote a linear typical time as a bound<\/td>\n<td>SPFA&#8217;s worst case is \u0398(VE), and there are families on which the queue version is slow. The bound to publish is the same worst case.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Nonnegative single-source distances belong to Dijkstra. All-pairs distances, including a negative-cycle test on the diagonal, belong to Floyd-Warshall. The queued variant of these relaxations is a scheduling choice, not a better guarantee.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The Bash script and the Python script relax every edge V\u22121 times. Edges are 0\u21921 weight 1, 1\u21922 weight 2, and a useless 0\u21922 weight 10. Both print distances <code>0 1 3<\/code>.<\/p>\n\n<pre><code class=\"language-bash\"># edges: u v w\neu=(0 1 0)\nev=(1 2 2)\new=(1 2 10)\nm=${#eu[@]}\nn=3\ndist=(0 1000000000 1000000000)\nfor ((round=0; round&lt;n-1; round++)); do\n  for ((e=0; e&lt;m; e++)); do\n    u=${eu[e]}; v=${ev[e]}; w=${ew[e]}\n    if (( dist[u] + w &lt; dist[v] )); then dist[v]=$((dist[u]+w)); fi\n  done\ndone\necho &quot;${dist[*]}&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">n = 3\nedges = [(0, 1, 1), (1, 2, 2), (0, 2, 10)]\ndist = [0, 10**9, 10**9]\nfor _ in range(n - 1):\n    for u, v, w in edges:\n        if dist[u] + w &lt; dist[v]:\n            dist[v] = dist[u] + w\nupdated = False\nfor u, v, w in edges:\n    if dist[u] + w &lt; dist[v]:\n        updated = True\nassert dist == [0, 1, 3] and not updated\nprint(*dist)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>RIP<\/strong>, the Routing Information Protocol, is a distributed Bellman-Ford: each router advertises distances and relaxes them. The same relaxation detects a negative cycle, which is the arbitrage check on a currency graph. OSPF uses Dijkstra because its link weights are nonnegative.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop Bellman-Ford when every weight is known nonnegative. Dijkstra does less work. Keep it when a negative edge may exist, when a negative cycle must be reported, or when difference constraints must be checked.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Bellman-Ford returns the |V|\u22121 relaxation rounds and the extra round that witnesses a negative cycle. <strong>Bellman-Ford computes single-source distances with negative edges in \u0398(VE) time, and a successful relaxation on round |V| is a reachable negative cycle.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Bellman-Ford relaxes every edge once per round, for |V|\u22121 rounds. A shortest path is simple when no negative cycle is reachable, so it has at most |V|\u22121 edges, and round k has considered every path of k edges. One more round that still relaxes an edge is&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,32],"tags":[],"class_list":["post-139","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-graphs"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"predecessor-version":[{"id":538,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/139\/revisions\/538"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}