{"id":201,"date":"2026-11-13T12:34:24","date_gmt":"2026-11-13T04:34:24","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-johnson-reweights-edges-and-runs-dijkstra\/"},"modified":"2026-11-13T21:52:38","modified_gmt":"2026-11-13T13:52:38","slug":"how-johnson-reweights-edges-and-runs-dijkstra","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-johnson-reweights-edges-and-runs-dijkstra\/","title":{"rendered":"How Johnson reweights edges and runs Dijkstra"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Johnson&#8217;s algorithm<\/strong> adds a super-source, runs <strong>Bellman-Ford<\/strong> to get potentials, and reweights every edge to <strong>w'(u,v) = w(u,v) + h(u) \u2212 h(v)<\/strong>. The new weights are nonnegative, so <strong>Dijkstra<\/strong> from each vertex is legal. A path&#8217;s reweighted length differs from its true length by h(start) \u2212 h(end), which depends only on the endpoints. The time is one Bellman-Ford plus V Dijkstra runs. A negative cycle is the Bellman-Ford failure.<\/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\">Floyd-Warshall always costs n\u00b3. Repeated Dijkstra costs about V(V+E) log V with a binary heap and cannot accept a negative edge. Many graphs are sparse and still have a few negative edges, with no negative cycle. A change of weight that makes every edge nonnegative, and that adds the same constant to every path between a fixed pair, preserves the identity of the shortest path.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The potentials come from distances in an augmented graph. Feasible potentials satisfy h(v) \u2264 h(u) + w(u,v), which is the same statement as w&#8217; \u2265 0.<\/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\">Let h(v) be the shortest-path distance from a new source s that has a zero-weight edge to every vertex. Bellman-Ford computes h, or reports a negative cycle. Then w'(u,v) = w(u,v) + h(u) \u2212 h(v) \u2265 0 for every original edge. For any path from x to y, the w&#8217; length equals the w length plus h(x) \u2212 h(y). Minimizing w&#8217; therefore minimizes w, and the true distance is the Dijkstra distance minus h(x) plus h(y).<\/p>\n\n\n<p class=\"wp-block-paragraph\">With a binary heap the V Dijkstra runs cost O(V(V+E) log V), and the initial Bellman-Ford costs O(VE). With a Fibonacci heap the Dijkstra portion is O(VE + V\u00b2 log V). Either one beats \u0398(V\u00b3) when the graph is sparse. Extra memory is the potential array and one distance array reused per source, plus the heap.<\/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>Add a source s with an edge of weight 0 to every vertex. Run Bellman-Ford from s. A reachable negative cycle aborts the algorithm. The distances are the potentials h.<\/li>\n<li>Replace each original edge weight w(u,v) by w(u,v) + h(u) \u2212 h(v). Drop s.<\/li>\n<li>For each vertex x, run Dijkstra from x under the new weights.<\/li>\n<li>For each reached y, report dist_w(x,y) = dist_w'(x,y) \u2212 h(x) + h(y).<\/li>\n<li>Unreached y stays at infinity. The predecessor tree from that Dijkstra, read under either weighting, is a true shortest path.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The reweighting preserves shortest paths only because h(x)\u2212h(y) depends on the endpoints and not on the intermediate vertices. It does not preserve the numeric weight of an individual edge in a way that would let you skip the final correction. Nonnegative input does not need Johnson. One Dijkstra per source is enough, and the Bellman-Ford setup is pure overhead.<\/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 the potential correction is omitted<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Run Dijkstra on w&#8217; and return those numbers as distances<\/td>\n<td>w&#8217; equals w only up to h(x)\u2212h(y). The path is right and the reported length is wrong until the correction.<\/td>\n<\/tr>\n<tr>\n<td>Take h from anything other than feasible distances<\/td>\n<td>An arbitrary h can make w&#8217; negative. Dijkstra then settles too early. Bellman-Ford distances are feasible precisely when no negative cycle exists.<\/td>\n<\/tr>\n<tr>\n<td>Ignore a negative cycle from the setup Bellman-Ford<\/td>\n<td>Some pair&#8217;s true distance is unbounded below. There is no all-pairs matrix to return.<\/td>\n<\/tr>\n<tr>\n<td>Use it on a dense graph in place of Floyd-Warshall<\/td>\n<td>V binary-heap Dijkstra runs are slower than n\u00b3 when E is \u0398(V\u00b2). The sparse case is the one this construction wins.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Dense all-pairs distances belong to Floyd-Warshall. Nonnegative sparse all-pairs distances belong to plain repeated Dijkstra, without a super-source.<\/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\">This Python fragment adds a super-source, runs Bellman\u2013Ford for potentials, and reweights every edge to a nonnegative reduced cost. Dijkstra then runs from each vertex. One negative edge 1\u21922 of weight -1 is present, and the true distance from 0 to 2 is <code>1<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import heapq\nn = 3\nedges = [(0, 1, 2), (1, 2, -1), (0, 2, 5)]\npot = [0] * (n + 1)\nextra = edges + [(n, v, 0) for v in range(n)]\nfor _ in range(n):\n    for u, v, w in extra:\n        if pot[u] + w &lt; pot[v]:\n            pot[v] = pot[u] + w\nreduced = [(u, v, w + pot[u] - pot[v]) for u, v, w in edges]\nadj = [[] for _ in range(n)]\nfor u, v, w in reduced:\n    adj[u].append((v, w))\n\ndef dijkstra(src):\n    dist = [10**9] * n\n    dist[src] = 0\n    pq = [(0, src)]\n    while pq:\n        d, u = heapq.heappop(pq)\n        if d != dist[u]:\n            continue\n        for v, w in adj[u]:\n            if d + w &lt; dist[v]:\n                dist[v] = d + w\n                heapq.heappush(pq, (dist[v], v))\n    return [dist[v] - pot[src] + pot[v] for v in range(n)]\n\nassert dijkstra(0)[2] == 1\nprint(dijkstra(0)[2])\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\">Johnson&#8217;s algorithm is all-pairs shortest paths on a sparse graph that may contain negative edges: one Bellman-Ford reweighting, then Dijkstra from each vertex. A dense tariff table uses Floyd-Warshall. A graph with a negative cycle has no answer for either.<\/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 Johnson when every weight is nonnegative or when the graph is dense. Keep it for a sparse graph that may contain negative edges and must not contain a negative cycle, which the setup pass detects.<\/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 Johnson&#8217;s algorithm returns the reweighting w + h(u) \u2212 h(v) and the endpoint correction that recovers true distances. <strong>Johnson&#8217;s algorithm makes negative edges nonnegative with Bellman-Ford potentials, then runs Dijkstra from every vertex and corrects distances by h(x) \u2212 h(y).<\/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: Johnson&#8217;s algorithm adds a super-source, runs Bellman-Ford to get potentials, and reweights every edge to w'(u,v) = w(u,v) + h(u) \u2212 h(v). The new weights are nonnegative, so Dijkstra from each vertex is legal. A path&#8217;s reweighted length differs from its true length by h(start) \u2212 h(end),&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-201","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\/201","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=201"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"predecessor-version":[{"id":600,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/201\/revisions\/600"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}