10.8. All-Pairs Shortest Paths (optional)¶
We next consider the problem of finding the shortest distance between all pairs of vertices in the graph, called the all-pairs shortest paths problem. To be precise, for every \(u, v \in \mathbf{V}\), calculate \(d(u, v)\).
One solution is to run Dijkstra’s algorithm
for finding the shortest path
\(|\mathbf{V}|\) times, each
time computing the shortest path from a different start vertex.
If \(\mathbf{G}\) is sparse
(that is, \(|\mathbf{E}| = \Theta(|\mathbf{V}|)\))
then this is a good solution, because the total cost will be
\(\Theta(|\mathbf{V}|^2 + |\mathbf{V}||\mathbf{E}| \log
|\mathbf{V}|) = \Theta(|\mathbf{V}|^2 \log |\mathbf{V}|)\)
for the version of Dijkstra’s algorithm based on priority queues.
For a dense graph, the priority queue version of Dijkstra’s algorithm
yields a cost of \(\Theta(|\mathbf{V}|^3 \log |\mathbf{V}|)\),
but the version using MinVertex
yields a cost
of \(\Theta(|\mathbf{V}|^3)\).
Another solution that limits processing time to \(\Theta(|\mathbf{V}|^3)\) regardless of the number of edges is known as Floyd’s algorithm. It is an example of dynamic programming. The chief problem with solving this problem is organizing the search process so that we do not repeatedly solve the same subproblems. We will do this organization through the use of the \(k\)-path. Define a k-path from vertex \(v\) to vertex \(u\) to be any path whose intermediate vertices (aside from \(v\) and \(u\)) all have indices less than \(k\). A 0-path is defined to be a direct edge from \(v\) to \(u\). Figure illustrates the concept of \(k\)-paths.
Define \({\rm D}_k(v, u)\) to be the length of the shortest
\(k\)-path from vertex \(v\) to vertex \(u\).
Assume that we already know the shortest \(k\)-path from \(v\)
to \(u\).
The shortest \((k+1)\)-path either goes through vertex \(k\)
or it does not.
If it does go through \(k\), then the best path is
the best \(k\)-path from \(v\) to \(k\) followed by the
best \(k\)-path from \(k\) to \(u\).
Otherwise, we should keep the best \(k\)-path seen before.
Floyd’s algorithm simply checks all of the possibilities in a triple
loop.
Here is the implementation for Floyd’s algorithm.
At the end of the algorithm, array D
stores the all-pairs shortest
distances.
/** Compute all-pairs shortest paths */
static void Map<V,Map<V,Double>> Floyd(Graph<V> G) {
// Initialize D with weights
Map<V,Map<V,Double>> D = new Map<>();
for (V i : G.vertices()) {
Map<V,Double> imap = new Map<>();
D.put(i, imap);
for (V j : G.vertices())
imap.put(j, i.equals(j) ? 0 : Double.POSITIVE_INFINITY);
for (edge : G.outgoingEdges(i))
imap.put(edge.end, edge.weight);
}
// Compute all k-paths
for (V k : G.vertices()) {
Map<V,Double> kmap = D.get(k);
for (V i : G.vertices()) {
Map<V,Double> imap = D.get(i);
for (V j : G.vertices()) {
double dist = imap.get(k) + kmap.get(j);
if (imap.get(j) > dist)
imap.put(j, dist);
}
}
}
return D;
}
# Compute all-pairs shortest paths
def floyd(G):
# Initialize D with weights
D = {}
for i in G.vertices():
D[i] = {j : 0 if i==j else float("inf")
for j in G.vertices()}
for edge in G.outgoingEdges(i):
D[i][edge.end] = edge.weight
# Compute all k-paths
for k in G.vertices():
for i in G.vertices():
for j in G.vertices():
dist = D[i][k] + D[k][j]
if D[i][j] > dist:
D[i][j] = dist
return D
Clearly this algorithm requires \(\Theta(|\mathbf{V}|^3)\) running time, and it is the best choice for dense graphs because it is (relatively) fast and easy to implement.