3. All-pairs shortest paths, point-to-point shortest-paths in geographical networks
6 min read
All-pairs shortest-paths problem
Given a weighted directed graph G=(V,E) and w(v,u) being the weight of edge (v,u), we want to either find:
information whether G contains a negative cycle; if it doesn’t,
an n×n matrix D=(di,j) such that di,j is equal to δ(i,j) (shortest-path from node i to node j); and shortest-paths trees, one from each node in graph
We skip computation of shortest-paths trees.
Solving the problem by repeatedly applying single-source shortest-paths algorithm
If all edge weights are non-negative:
Use Dijkstra’s algorithm
Total running time is n×O(min{mlogn,n2})=O(min{nmlogn,n3})
No method with a better (worst-case) running time is known
In general case, when edge weights may be negative:
We may use Bellman-Ford algorithm
Total running time is n×O(nm)=O(n2m)
This is Θ(n4) if m=Θ(n2)
Floyd-Warshall algorithm: Θ(n3)
Johnson’s algorithm: O(nmlogn)
Changing weights of edges without changing shortest-paths
Say we have a graph as follows, and we want to change the weight of edges (ideally without removing negative weights) without changing shortest paths?
Adding the same (large) number to the weight of each edge doesn’t work.
In Johnson’s algorithm, we add and subtract on each edge:
Johnson’s algorithm
The main idea behind Johnson’s algorithm is that it reduces the general case (where edge weights may be negative) to the case where all edge weights are non-negative.
Re-weighting: compute new edge weights w^ with properties:
For all u,v∈V, shortest path from u to v using original weights w is also a shortest path from u to v using the new weights wˉ
wˉ(u,v)≥0 for each (u,v)∈E
Explaining re-weighting
General idea for re-weighting:
for each node v∈V, assign a number h(v) to v
for each (v,u)∈E, let w^(u,v)=w(u,v)+h(u)−h(v)
For any numbers h(v), the new edge weights satisfy Property 1.
If no negative cycle in G, then we can find numbers h(v) which also satisfy Property 2.
Thus when we change the edge weights from w to w^, then for each pair of nodes u and v, the weight of each path from u to v changes by the same amount: h(u)−h(v)
Hence a path P is a shortest path from u to v according to weights w, if and only if, P is a shortest path from u to v according to weights w^.
We also have, from (∗): δ^(u,v)=δ(u,v)+[h(u)−h(v)].
Example: computation of new edge weights
Johnson's Algoithm
def johnson(G, w): (V, E) = G G' = G # clone the graph (V', E') = G' V' = V | {s} # s is a new node E' = E | {(s, v) | v in V} # edges from s to all v for v in V: w(s, v) = 0 if not bellman_ford(G', w, s): # terminate: input graph G contains negative cycle else: # bellman_ford has computed values δ(s,v) for v in V: h(v) = δ(s, v) w' = w # clone weights for (u, v) in E: w'(u, v) = w(u, v) + h(u) - h(v) for u in V: dijkstra(G, w', u) # compute δ(u, v) for all v in V for v in V: d_uv = δ(u, v) - [h(u) - h(v)] return D = d_uv
Correctness of Johnson's algorithm
Input graph G contains a negative cycle, if and only if, there is a negative cycle in graph G′ reachable from s. This means the algorithm correctly identifies whether the input graph has a negative cycle.
For each edge (u,v) in G, the new weight w^(u,v) assigned to this edge in Johnson’s algorithm is non-negative. δ(s,u)+w(u,v)≥δ(s,v)
(Triangle inequality for shortest-path weights) so w^(u,v)=w(u,v)+h(u)−h(v)=w(u,v)+δ(s,u)−δ(s,v)≥0
Non-negative weights imply that the algorithm correctly computes δ(u,v) for any pairs of vertices.
For any numbers h(.) and for each pair of nodes u and v in G: δ(u,v)=δ(u,v)=[h(u)−h(v)]
Thus the algorithm correctly computes δ(h,v) for all pairs of vertices.
Point-to-point shortest-paths in geographical networks
Geographical networks: geographical coordinates of nodes are known; weights of edges are at least straight-line (geographical) distances between the nodes
Dijkstra’s for single-source single-destination
We can run in parallel, two Dijkstra’s computations, one from the source and one from the destination(s) (considering edges backward). We stop when shortest s→d path is found. We may or may not check the whole network.
However, we need to figure out if the combined path (one part from the shortest-path tree from s and the other part from the shortest-path tree to d) is the shortest s−d path.
Dijkstra’s for ‘geographical’ networks (basis of A* algorithm)
Given the nodes are geographical places and we know their coordinates: the weights of edges are at least straight-line distances between the nodes. We want to consider this information to speed-up Dijkstra’s computation for shortest source-destination path.
We can use re-weighting of edges to prioritise those which are ‘geographically’ closer to the destination, we use h(v)=−dist(v,d) where dist(v,d) is the straight-line distance from node v to destination d.
w^(u,v)=w(u,v)−dist(u,d)+dist(v,d)
As the search is directed towards the destination, not many additional edges (away from the shortest path) are considered. This can lead to considerable improvement in average running time.
Worst-case running time remains as in the main Dijkstra's algorithm for single-source (all destinations) case.
Correctness of re-weighting with distance
Re-weighting by straight-line distances to destination works because the straight-line distances satisfy the triangle inequality:
dist(u,d)≤dist(u,v)+dist(v,d)≤w(u,v)+dist(v,d)
So the new weights are non-negative:
w^(u,v)=w(u,v)−dist(u,d)+dist(v,d)≥0
The straight-line distances are used here as (under)-estimates of the shortest-path weights.
A* search algorithm
The modified Dijkstra’s algorithm with re-weighting using a cost estimate function b is the A search algorithm*.
In A*, the function b(.) is called a heuristic function, often denoted by h(.).
The property of the heuristic function h which guarantees that when the target t is taken from Q, then the shortest path to t is computed: For each edge (u,v), h(u)≤w(u,v)+h(v).
A heuristic function h(.) which satisfies this property is called consistent or monotone, and is equivalent to the property that all new weights w^(u,v)=w(u,v)−h(u)+h(v) are non-negative.