TL;DR: Dijkstra's alone isn't sufficient to solve the all-pairs shortest paths problem, but on an undirected graph, you're right that there is information contained about some of the pairwise shortest paths (i.e., those involving the source vertex as the source or the destination). Depending on the queries (if you know them ahead of time and they were sufficiently contained in a small subgraph), perhaps, you could run Dijkstra fewer than
Hmmmm... I'm don't think Dijkstra alone would give you all-pairs shortest paths (APSPs) among all vertices inside the envelope[1] as it proceeds (or even at the end). The distance estimates are from a single, source vertex
Johnson's is an APSP algorithm, and the only other ones I can claim to "know" are:
- running Bellman-Ford
$|V|$ times, which is$O(|V|^{4})$ in the worst case - Floyd-Warshall, which is
$O(|V|^{3})$
Johnson's is basically:
- Create a "super node"
$x$ connected to every vertex in your graph$G$ by an edge of$0$ -weight. - Run Bellman-Ford (BF) once from this "super node": (1) to detect negative-weight cycles (if any exists, exit early) and (2) the distances
$d(x,v)$ computed by (BF) are the potentials$h(v)$ you use to reweight$G$ so all edges are nonnegative, giving transformed graph$G^{\prime}$ . - Run Dijkstra from every vertex in
$G^{\prime}$ . This will give you APSP in$G^{\prime}$ , i.e.,$\delta^{\prime}(u, v)$ for all$u, v \in V^{\prime}$ . - Recover APSP in
$G$ by reversing the reweighting you did above:$\delta(u, v) = \delta^{\prime}(u, v) - h(u) + h(v)$ .
Theoretically (using Fibonacci heaps, which I know literally nothing about other than the runtimes of the operations it supports ad that the constant factors can be so big that, in practice, it doesn't make sense to use them 🙊), Johnson's runtime is
Johnson's runtime is contingent on your choice of priority queue (similar to Dijkstra...because you're essentially just running Dijkstra
NB: I'm far from an expert on any of this stuff, so please do weigh in if you've anything to add!
[1]: A vertex