Johnson's Algorithm
Johnson's Algorithm
006
Massachusetts Institute of Technology
Instructors: Erik Demaine, Jason Ku, and Justin Solomon Lecture 14: Johnson’s Algorithm
Previously
• Useful when understanding whole network, e.g., transportation, circuit layout, supply chains...
• Just doing a SSSP algorithm |V | times is actually pretty good, since output has size O(|V |2 )
– |V | · O(|V | + |E|) with BFS if weights positive and bounded by O(|V | + |E|)
– |V | · O(|V | + |E|) with DAG Relaxation if acyclic
– |V | · O(|V | log |V | + |E|) with Dijkstra if weights non-negative or graph undirected
– |V | · O(|V | · |E|) with Bellman-Ford (general)
• Today: Solve APSP in any weighted graph in |V | · O(|V | log |V | + |E|) time
2 Lecture 14: Johnson’s Algorithm
Approach
• Idea: Make all edge weights non-negative while preserving shortest paths!
• Claim: Can compute distances in G from distances in G0 in O(|V |(|V | + |E|)) time
– Compute shortest-path tree from distances, for each s ∈ V 0 in O(|V | + |E|) time (L11)
– Also shortest-paths tree in G, so traverse tree with DFS while also computing distances
– Takes O(|V | · (|V | + |E|)) time (which is less time than |V | times Dijkstra)
• But how to make G0 with non-negative edge weights? Is this even possible??
• Proof: Shortest paths are simple if no negative weights, but not if negative-weight cycle
• FAIL: Does not preserve shortest paths! Biases toward paths traversing fewer edges :(
• Idea! Given vertex v, add h to all outgoing edges and subtract h from all incoming edges
• Proof:
• This is a very general and useful trick to transform a graph while preserving shortest paths!
Lecture 14: Johnson’s Algorithm 3
• Make graph G0 : same as G but edge (u, v) ∈ E has weight w0 (u, v) = w(u, v) + h(u) − h(v)
• Proof:
– (Sum of h’s telescope, since there is a positive and negative h(vi ) for each interior i)
– Every path from v0 to vk changes by the same amount
– So any shortest path will still be shortest
• i.e., is there an h such that w(u, v) + h(u) − h(v) ≥ 0 for every (u, v) ∈ E?
• Re-arrange this condition to h(v) ≤ h(u) + w(u, v), looks like triangle inequality!
• Idea! Condition would be satisfied if h(v) = δ(s, v) and δ(s, v) is finite for some s
• But graph may be disconnected, so may not exist any such vertex s... :(
• Claim: If δ(s, v) = −∞ for any v ∈ V , then the original graph has a negative-weight cycle
• Proof:
Johnson’s Algorithm
• Construct Gx from G by adding vertex x connected to each vertex v ∈ V with 0-weight edge
• Else:
Correctness
• Already proved that transformation from G to G0 preserves shortest paths
Running Time
• O(|V | + |E|) time to construct Gx
For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms