Why it matters

APSP with negatives is common need. Johnson's is optimal for sparse. Understanding shapes graph algorithms.

Advertisement

The architecture

Reweight via Bellman-Ford: eliminates negatives.

Dijkstra from each vertex on reweighted graph.

Johnson's algorithmBellman-Fordreweight to non-negDijkstra from eachV timesRecover distancesunshiftBetter than Floyd-Warshall (O(V³)) for sparse graphs
Johnson's steps.
Advertisement

How it works end to end

Reweighting: w'(u,v) = w(u,v) + h(u) - h(v). h from Bellman-Ford from added source.

Preserves shortest paths.

Dijkstra runs O(V(E + V log V)) total.

Recovery: subtract h difference back.