Why it matters

Better than Floyd-Warshall for sparse graphs. Understanding shapes algorithm choice.

Advertisement

The architecture

Add virtual source with 0-edges to all.

Bellman-Ford: compute h.

Reweight: w' = w + h(u) - h(v).

Dijkstra from each vertex.

Johnson's algorithmBellman-Fordcompute potentials hReweight edgesnon-negativeDijkstra x Vall-pairs SPFaster than Floyd-Warshall on sparse; Floyd better for dense
Johnson's flow.
Advertisement

How it works end to end

Bellman-Ford detects negative cycles.

Reweighting preserves shortest paths.

Dijkstra runs on non-negative weights.