Johnson’s Algorithm

Context: FIT2004_MOC · all-pair shortest distance on a sparse graph with negative edges — one Bellman-Ford makes every weight non-negative, then runs of Dijkstra; the sparse counterpart to Floyd-Warshall Parent Framework: Graph

Quick Revision

  • 🎯 Objective: potentials Bellman-Ford distance from a weight- super source ➔ reweight ➔ Dijkstra ➔ undo with .
  • 📦 Core Components: ① super source Bellman-Ford | ② reweight | ③ Dijkstra | ④ undo .
  • ⚡ Key Constraint: the shift must be per vertex, not per edge — adding a constant to every edge (applied P1) charges long paths more than short ones; potentials telescope, so every path moves by the same . No negative cycle allowed — step ① detects one and stops.

📝 How It Works

1. Why a Constant Shift Fails (applied P1)

  • The tempting repair ➔ subtract the minimum weight from every edge, run Dijkstra, add it back.
  • The flaw ➔ a -edge path moves by — the shift depends on edge count, so a many-edge shortest path is overtaken by a few-edge rival.
  • Counterexample recipe ➔ a shortest path with more edges than an alternative ➔ worked below.

2. Potentials Make Every Edge Non-Negative (applied P7a)

  • Definition ➔ , the shortest distance to from a fixed source.
  • Claim ➔ for every edge .
  • Proof the triangle inequality ➔ , else beats the shortest path to — contradiction. Subtract from both sides. ∎

3. Computing Valid Potentials (applied P7b)

  • An arbitrary source almost works ➔ a vertex it cannot reach gets , and becomes undefined.
  • Fix: the super source ➔ add with a weight- edge to every vertex and run Bellman-Ford from ⟹ every is finite — the same move as Bellman-Ford §5.
  • Free safety check ➔ that run’s -th pass reports any negative cycle ⟹ stop; no all-pair answer exists.

4. Reweighting Preserves Shortest Paths (applied P7c)

  • Telescoping ➔ for a path :
  • Endpoint-only offset ➔ is the same for every path, so the ranking of paths is unchanged ⟹ a shortest path under is a shortest path under . ∎
  • Undo ➔ .

5. Assembling the Algorithm (applied P7d)

  • The pipeline ➔ ① potentials by super source Bellman-Ford ② reweight every edge ③ Dijkstra from every vertex on ④ undo the shift on each finite answer.
  • Bellman-Ford runs once, Dijkstra times ➔ negative weights exist only before step ②; after it Dijkstra’s precondition holds, so the expensive algorithm pays once and the cheap one does the repetition.

⚙️ Core Implementation

🔹 Johnson’s all-pair shortest distance

⚖️ Complexity

StepTimeSpace
① Bellman-Ford on vertices, edges (connected)
② Reweight new adjacency list
③ Dijkstra per run
④ Undo output
Total output auxiliary
  • Sparse ➔ — the sheet’s bound, beating Floyd-Warshall’s .
  • Dense ➔ — loses to Floyd-Warshall; the all-pair selection rule lives in Floyd-Warshall §Decision Matrix.

📊 Exam Execution Trace & Applied Exercises

Applied P1 graph — , , , . True shortest is .

Applied Exercise — the constant shift breaks it (applied P1)

Final Extracted Output: wrong answer — the shift penalised the 3-edge shortest path three times as hard as its 1-edge rival.

Manual Execution Trace — potentials fix it (same graph)

Super source with weight- edges to ; Bellman-Ford from , then reweight.

Edge

Final Extracted Output: Dijkstra from takes at over the direct edge at ⟹ ✓.

  • Read the zeros ➔ edges on the super source’s shortest-path tree make the triangle inequality tight, so they reweight to exactly ; only slack edges stay positive.

⚠️ Common Mistakes

  • 💡 Shifting every edge by a constant ➔ the shift scales with edge count (§1); only per-vertex potentials telescope.
  • 💡 Potentials from an ordinary source ➔ unreachable vertices get ; use the weight- super source.
  • 💡 Quoting unconditionally ➔ that is the sparse instantiation; the general bound is , and on a dense graph Floyd-Warshall wins.
  • 💡 “Johnson’s handles negative cycles” ➔ no all-pair answer exists then; step ① detects and reports, exactly as Bellman-Ford does.

🧠 Active Recall