# Single Source Shortest Path (SSSP)

## 1 Formulation of the problem

Let $G = (V, E)$ be a given graph with edge weights $f(e)$ and a marked vertex (called the source) $u$. 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$. The total weight of this path is defined as

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

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

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

## 2 Variants of the problem

If shortest paths are needed for all the vertices rather than for a single one, then see all pairs 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 linear time by using the breadth first search.
• Integer weights . The problem can again be solved in linear 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.

## 3 Properties of the problem

The solution to the problem satisfies the following optimality principle: If the path $P^*(u, w)$ is a part of the shortest path $P^*(u, v)$, then $P^*(u, w)$ is the shortest path from the source $u$ to the vertex $w$.

The optimality principle implies that, for each vertex $v$, it suffices to store only the last edge $e^*_v$ of the shortest path $P^*(u, v)$ rather than the entire path. Consequently, the storage estimate is $O(m)$, where $m$ is the number of edges.

## 4 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$), source $u$.

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

Output data (possible variants):

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

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

### 4.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^*_w$ are passed in the reverse direction until the source $u$ is visited.

The edges $e^*_v$ can be regenerated in time $O(m)$ by sorting the edges according to the rule

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

The sorting can be performed in parallel.

The search for shortest distances $f^*(v)$ for the given set of edges $e^*_v$ should be preceded by the construction of a tree. Then the breadth first search is applied to this tree. The time complexity is $O(m)$ (with the possibility of parallelization).

## 5 Algorithms for solving the problem

• Breadth-first search (BFS) for directed unweighted graphs. The complexity is $O(m)$.
• Dijkstra's algorithm[2][3] for directed graphs with nonnegative weights. The complexity is $O(m + n \ln n)$.
• Bellman-Ford algorithm[4][5][6] for directed graphs with arbitrary weights. The complexity is $O(mn)$.
• Δ-stepping algorithm[7] for directed graphs with nonnegative weights. The average-case complexity for graphs with random weights is $O(n + m + dL)$; the parallel complexity is $O((dL + \ln n) \ln n$, and the average-case work is $O(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(m)$.
• For a directed acyclic graph with arbitrary weights, the search for a shortest path has linear time complexity $O(m + n)$. The algorithm is based on the topological sorting of the graph.

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.

The algorithms listed above can also be used for solving the APSP problem All Pairs Shortest Path. (To this end, the corresponding algorithm should be run for each vertex of the graph, which, if required, can be done in parallel.) In this case, the complexity estimate is multiplied by $n$.

## 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. 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.
3. 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.
4. Bellman, Richard. “On a Routing Problem.” Quarterly of Applied Mathematics 16 (1958): 87–90.
5. Ford, L R. Network Flow Theory. Rand.org, RAND Corporation, 1958.
6. Moore, Edward F. “The Shortest Path Through a Maze,” International Symposium on the Theory of Switching, 285–92, 1959.
7. 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.