-
Notifications
You must be signed in to change notification settings - Fork 0
Johnson's Algorithm For Sparse Graphs
Johnson's algorithm finds shortest paths between all pairs in
- For all pairs of vertices u,v
$\in$ V, a path p is a shortest path from u to v using weight function w if and only if p is also a shortest path from u to v using$w^*$ - For all edges (u,v), the new weight
$w^*$ (u,v) is nonnegative.
We can process
Given a weighted, directed graph G and a weight function w : E -> R, let h : V -> R be any function mapping vertices to real numbers. For each edge (u,v)
Let p = {
That is, w(p) =
The next goal is to ensure that the second property holds: we want
We extend the weight function w so that w(s,v) = 0 for all v in V. Note that because s has no edges that eneter it, no shortest path in G', other than thiose with source s, contain s. Moreover, G' has no negative-weight cycles if and only if G has no negative weight cycles.
Now suppose that G and G' have no negative-weight cycles. Let us define h(v) =
Jonhson compute the all-pairs shortest path using BF and Dijkstra as subroutines. It assumes that the edges are stored in adjacency lists. The algorithm returns the usual |V| x |V| matrix D =
JOHNSON(G,w)
compute G', where G'.V = G.V U {s}, G'.E = G.E U {(s,v) : v in G.V} and w(s,v) = 0 for all v in G.V
if BF(G', w, s) == FALSE
print "Input has negative weight cycle"
else for v in G'.V
set h(v) to delta(s,v) computed by BF
for (u,v) in G'.E
w*(u,v) = w(u,v) + h(u) - h(v)
let D be a new nxn matrix
for u in G.V
run DIJKSTRA(G, w*,u) to get delta*(u,v) for all v in G.V
for v in G.V
d_uv = delta*(u,v) + h(V) - h(u)
return D