# All Pairs Shortest Path (APSP)

## 1 Formulation of the problem

Let $G = (V, E)$ be a given graph with edge weights $f(e)$, $e \in E$. The sequence $P(u, v)$ of edges $e_1 = (u, w_1)$, $e_2 = (w_1, w_2)$, …, $e_k = (w_{k-1}, v)$ is called a "path from the vertex $u$ to $v$. We say that $v$ is reachable from $u$ if there exists at least one path $P(u, v)$. The total weight of this path is

$f(P(u, v)) = \sum_{i = 1}^k f(e_i).$

For each vertex pair $(u, v)$ such that $v$ is reachable from $u$, it is required to indicate a path $P^*(u, v)$ having the minimum possible total weight:

$f^*(u, v) = f(P^*(u, v)) = \min f(P(u, v)).$

## 2 Variants of the problem

If a shortest path is required only for a single source rather than for all vertices, then see single source shortest path.

This problem can be stated for both directed and undirected graphs. The above formulation is applicable in both cases.

Depending on possible values of the weights, the following cases may be distinguished:

• Unit weights. In this case, the problem significantly simplifies and can be solved in quadratic time by using the breadth first search.
• Integer weights . The problem can again be solved in quadratic time [1] (though with a larger constant in the complexity estimate).
• Nonnegative weights. A constraint commonly occurring in practice is that the edge weights correspond to the times (or costs) of passing through portions of the route and, hence, cannot be negative.
• Arbitrary weights. This is the most general situation. The graph should not contain cycles with a negative total weight; otherwise, there is no shortest path.

Depending on the organization of calculations, the following variants are possible:

• static graph: Calculation of all shortest paths (classical approach);
• online calculation [2]: Certain preliminary calculations are performed; then a path between two vertices is found by inquiry. (This approach is reasonable if the shortest paths are actually needed only for certain vertex pairs, but these pairs are not a priori known);
• dynamic graph [3]: edges and vertices may be added or removed, which is accompanied by the recalculation of the shortest paths. (This variant is appropriate for permanently varying graphs, for instance, social networks.)

## 3 Input and output data

Input data: weighted graph $(V, E, W)$ ($n$ vertices $v_i$ and $m$ edges $e_j = (v^{(1)}_{j}, v^{(2)}_{j})$ with weights $f_j$).

Size of the input data: $O(m + n)$.

Output data (possible variants):

1. for each vertex pair $(u, v)$ of the original graph: the last edge $e^*_v = (w, v)$ of the shortest path leading from the vertex $u$ to $v$ or the corresponding vertex $w$;
2. for each vertex pair $(u, v)$ of the original graph: the total weight $f^*(u, v)$ of the shortest path leading from the source $u$ to $v$.

Size of the output data: $O(n^2)$.

### 3.1 Output data conversion and shortest path search

The shortest path from $u$ to $v$ can be reconstructed in time $O(\left |P^*(u, v) \right |)$ if, starting from the vertex $v$, the edges $e^*_{uw}$ are passed in the reverse direction until the vertex $u$ is visited.

The edges $e^*_{uv}$ can be regenerated in time $O(mn)$ by sorting the edges for each vertex according to the rule

$e^*_{uv} \in \{ (w, v) \in E \mid f^*(u, v) = f^*(u, w) + f((v, w)) \}.$

The sorting can be performed in parallel.

For the given vertex $u$, the search for shortest distances $f^*(u, v)$ for the set of edges $e^*_{uv}$ should be preceded by the construction of a tree from these edges. Then the breadth first search with time complexity $O(m)$ (and with the possibility of parallelization) is applied to this tree. The total time for all vertex pairs is $O(mn)$.

## 4 Algorithms for solving the problem

• Johnson's algorithm[4] for directed graphs with arbitrary weights. The complexity is $O(mn+ n^2 \ln n)$.
• The Floyd--Warshall algorithm[5][6][7] for directed graphs with arbitrary weights. The complexity is $O(n^3)$.
• Repeated application of any algorithm for the single source shortest path:
• Breadth first search (BFS) for directed unweighted graphs. The complexity is $O(mn)$.
• Dijkstra's algorithm[8][9] for directed graphs with nonnegative weights. The complexity is $O(mn + n^2 \ln n)$.
• The Bellman--Ford algorithm[10][11][12] for directed graphs with arbitrary weights. The complexity is $O(mn^2)$.
• Δ-stepping algorithm[13] for directed graphs with nonnegative weights. The average-case complexity for graphs with random weights is $O(n(n + m + dL))$; the parallel complexity is $O((dL + \ln n) n \ln n$, and the average-case work is $O(n (n + m + dL \ln n))$.
• The Thorup algorithm[1] is a theoretical proof of the fact that the problem for undirected graphs with positive integer weights can be solved in linear time $O(mn)$.
• For a directed acyclic graph with arbitrary weights, the algorithm is based on the topological sorting of the graph. The complexity is $O(mn)$.

Notation: $m$ is the number of edges, $n$ is the number of vertices, $d$ is the maximum degree of a vertex, and $L$ is the maximum total weight of a shortest path.

## 5 Parallelization resource

The problem can be solved by applying any algorithm for the single source shortest path to all the vertices of the graph. For different vertices, the calculations are independent and may be performed in parallel.

Since the size of the output data is quadratic and the complexity of calculations is higher than quadratic, it makes sense to perform a preliminary processing of the data and use various graph decompositions in order to reduce the original problem to the parallel finding of shortest paths on smaller subgraphs [14]:

Which decompositions are advantageous in a specific situation depends on the structure of the graph and can be determined only by analyzing this structure.

## 6 References

1. Thorup, Mikkel. “Undirected Single-Source Shortest Paths with Positive Integer Weights in Linear Time.” Journal of the ACM 46, no. 3 (May 1, 1999): 362–94. doi:10.1145/316542.316548.
2. Djidjev, Hristo N, Grammati E Pantziou, and Christos D Zaroliagis. “On-Line and Dynamic Algorithms for Shortest Path Problems.” In Proceedings of STACS 95, 193–204. Lecture Notes in Computer Science Vol. 900, Berlin, Heidelberg: Springer, 1995. doi:10.1007/3-540-59042-0_73.
3. Demetrescu, Camil, and Giuseppe F Italiano. “Fully Dynamic All Pairs Shortest Paths with Real Edge Weights.” Journal of Computer and System Sciences 72, no. 5 (August 2006): 813–37. doi:10.1016/j.jcss.2005.05.005.
4. Johnson, Donald B. “Efficient Algorithms for Shortest Paths in Sparse Networks.” Journal of the ACM 24, no. 1 (January 1977): 1–13. doi:10.1145/321992.321993.
5. Roy, Bernard. “Transitivité Et Connexité.” Comptes Rendus De l'Académie Des Sciences 249 (1959): 216–218.
6. Warshall, Stephen. “A Theorem on Boolean Matrices.” Journal of the ACM 9, no. 1 (January 1, 1962): 11–12. doi:10.1145/321105.321107.
7. Floyd, Robert W. “Algorithm 97: Shortest Path.” Communications of the ACM 5, no. 6 (June 1, 1962): 345. doi:10.1145/367766.368168.
8. Dijkstra, E W. “A Note on Two Problems in Connexion with Graphs.” Numerische Mathematik 1, no. 1 (December 1959): 269–71. doi:10.1007/BF01386390.
9. Fredman, Michael L, and Robert Endre Tarjan. “Fibonacci Heaps and Their Uses in Improved Network Optimization Algorithms.” Journal of the ACM 34, no. 3 (July 1, 1987): 596–615. doi:10.1145/28869.28874.
10. Bellman, Richard. “On a Routing Problem.” Quarterly of Applied Mathematics 16 (1958): 87–90.
11. Ford, L R. Network Flow Theory. Rand.org, RAND Corporation, 1958.
12. Moore, Edward F. “The Shortest Path Through a Maze,” International Symposium on the Theory of Switching, 285–92, 1959.
13. Meyer, U, and P Sanders. “Δ-Stepping: a Parallelizable Shortest Path Algorithm.” Journal of Algorithms 49, no. 1 (October 2003): 114–52. doi:10.1016/S0196-6774(03)00076-2.
14. Banerjee, Dip Sankar, Ashutosh Kumar, Meher Chaitanya, Shashank Sharma, and Kishore Kothapalli. “Work Efficient Parallel Algorithms for Large Graph Exploration on Emerging Heterogeneous Architectures.” Journal of Parallel and Distributed Computing, December 2014. doi:10.1016/j.jpdc.2014.11.006.