How Johnson reweights edges and runs Dijkstra

Short answer: Johnson’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) − h(v). The new weights are nonnegative, so Dijkstra from each vertex is legal. A path’s reweighted length differs from its true length by h(start) − 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.


What the problem actually asks

Floyd-Warshall always costs n³. 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.

The potentials come from distances in an augmented graph. Feasible potentials satisfy h(v) ≤ h(u) + w(u,v), which is the same statement as w’ ≥ 0.


What the algorithm guarantees

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) − h(v) ≥ 0 for every original edge. For any path from x to y, the w’ length equals the w length plus h(x) − h(y). Minimizing w’ therefore minimizes w, and the true distance is the Dijkstra distance minus h(x) plus h(y).

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² log V). Either one beats Θ(V³) when the graph is sparse. Extra memory is the potential array and one distance array reused per source, plus the heap.


The working steps

  1. 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.
  2. Replace each original edge weight w(u,v) by w(u,v) + h(u) − h(v). Drop s.
  3. For each vertex x, run Dijkstra from x under the new weights.
  4. For each reached y, report dist_w(x,y) = dist_w'(x,y) − h(x) + h(y).
  5. Unreached y stays at infinity. The predecessor tree from that Dijkstra, read under either weighting, is a true shortest path.

The reweighting preserves shortest paths only because h(x)−h(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.


Approaches that look simpler and fail

Approach Why it fails when the potential correction is omitted
Run Dijkstra on w’ and return those numbers as distances w’ equals w only up to h(x)−h(y). The path is right and the reported length is wrong until the correction.
Take h from anything other than feasible distances An arbitrary h can make w’ negative. Dijkstra then settles too early. Bellman-Ford distances are feasible precisely when no negative cycle exists.
Ignore a negative cycle from the setup Bellman-Ford Some pair’s true distance is unbounded below. There is no all-pairs matrix to return.
Use it on a dense graph in place of Floyd-Warshall V binary-heap Dijkstra runs are slower than n³ when E is Θ(V²). The sparse case is the one this construction wins.

Dense all-pairs distances belong to Floyd-Warshall. Nonnegative sparse all-pairs distances belong to plain repeated Dijkstra, without a super-source.


A working example

This Python fragment adds a super-source, runs Bellman–Ford for potentials, and reweights every edge to a nonnegative reduced cost. Dijkstra then runs from each vertex. One negative edge 1→2 of weight -1 is present, and the true distance from 0 to 2 is 1.

import heapq
n = 3
edges = [(0, 1, 2), (1, 2, -1), (0, 2, 5)]
pot = [0] * (n + 1)
extra = edges + [(n, v, 0) for v in range(n)]
for _ in range(n):
    for u, v, w in extra:
        if pot[u] + w < pot[v]:
            pot[v] = pot[u] + w
reduced = [(u, v, w + pot[u] - pot[v]) for u, v, w in edges]
adj = [[] for _ in range(n)]
for u, v, w in reduced:
    adj[u].append((v, w))

def dijkstra(src):
    dist = [10**9] * n
    dist[src] = 0
    pq = [(0, src)]
    while pq:
        d, u = heapq.heappop(pq)
        if d != dist[u]:
            continue
        for v, w in adj[u]:
            if d + w < dist[v]:
                dist[v] = d + w
                heapq.heappush(pq, (dist[v], v))
    return [dist[v] - pot[src] + pot[v] for v in range(n)]

assert dijkstra(0)[2] == 1
print(dijkstra(0)[2])

Where it is used

Johnson’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.


When you can drop this algorithm

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.


Why this stays published

We publish this so a search for Johnson’s algorithm returns the reweighting w + h(u) − h(v) and the endpoint correction that recovers true distances. Johnson’s algorithm makes negative edges nonnegative with Bellman-Ford potentials, then runs Dijkstra from every vertex and corrects distances by h(x) − h(y).

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *