# Shortest path problem

In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.

The problem of finding the shortest path between two intersections on a road map may be modeled as a special case of the shortest path problem in graphs, where the vertices correspond to intersections and the edges correspond to road segments, each weighted by the length of the segment.

## Definition

The shortest path problem can be defined for graphs whether undirected, directed, or mixed. It is defined here for undirected graphs; for directed graphs the definition of path requires that consecutive vertices be connected by an appropriate directed edge.

Two vertices are adjacent when they are both incident to a common edge. A path in an undirected graph is a sequence of vertices $P=(v_{1},v_{2},\ldots ,v_{n})\in V\times V\times \cdots \times V$ such that $v_{i}$ is adjacent to $v_{i+1}$ for $1\leq i . Such a path $P$ is called a path of length $n-1$ from $v_{1}$ to $v_{n}$ . (The $v_{i}$ are variables; their numbering here relates to their position in the sequence and needs not to relate to any canonical labeling of the vertices.)

Let $e_{i,j}$ be the edge incident to both $v_{i}$ and $v_{j}$ . Given a real-valued weight function $f:E\rightarrow \mathbb {R}$ , and an undirected (simple) graph $G$ , the shortest path from $v$ to $v'$ is the path $P=(v_{1},v_{2},\ldots ,v_{n})$ (where $v_{1}=v$ and $v_{n}=v'$ ) that over all possible $n$ minimizes the sum $\sum _{i=1}^{n-1}f(e_{i,i+1}).$ When each edge in the graph has unit weight or $f:E\rightarrow \{1\}$ , this is equivalent to finding the path with fewest edges.

The problem is also sometimes called the single-pair shortest path problem, to distinguish it from the following variations:

• The single-source shortest path problem, in which we have to find shortest paths from a source vertex v to all other vertices in the graph.
• The single-destination shortest path problem, in which we have to find shortest paths from all vertices in the directed graph to a single destination vertex v. This can be reduced to the single-source shortest path problem by reversing the arcs in the directed graph.
• The all-pairs shortest path problem, in which we have to find shortest paths between every pair of vertices v, v' in the graph.

These generalizations have significantly more efficient algorithms than the simplistic approach of running a single-pair shortest path algorithm on all relevant pairs of vertices.

## Algorithms

The most important algorithms for solving this problem are:

Additional algorithms and associated evaluations may be found in Cherkassky, Goldberg & Radzik (1996).

## Single-source shortest paths

### Undirected graphs

Weights Time complexity Author
$\mathbb {R}$ + O(V2) Dijkstra 1959
$\mathbb {R}$ + O((E + V) log V) Johnson 1977 (binary heap)
$\mathbb {R}$ + O(E + V log V) Fredman & Tarjan 1984 (Fibonacci heap)
$\mathbb {N}$ O(E) Thorup 1999 (requires constant-time multiplication)

### Unweighted graphs

Algorithm Time complexity Author
Breadth-first search O(E + V)

### Directed acyclic graphs (DAGs)

An algorithm using topological sorting can solve the single-source shortest path problem in time Θ(E + V) in arbitrarily-weighted DAGs.

### Directed graphs with nonnegative weights

The following table is taken from Schrijver (2004), with some corrections and additions. A green background indicates an asymptotically best bound in the table; L is the maximum length (or weight) among all edges, assuming integer edge weights.

Weights Algorithm Time complexity Author
$\mathbb {R}$ $O(V^{2}EL)$ Ford 1956
$\mathbb {R}$ Bellman–Ford algorithm $O(VE)$ Shimbel 1955, Bellman 1958, Moore 1959
$\mathbb {R}$ $O(V^{2}\log {V})$ Dantzig 1960
$\mathbb {R}$ Dijkstra's algorithm with list $O(V^{2})$ Leyzorek et al. 1957, Dijkstra 1959, Minty (see Pollack & Wiebenson 1960), Whiting & Hillier 1960
$\mathbb {R}$ Dijkstra's algorithm with binary heap $O((E+V)\log {V})$ Johnson 1977
$\mathbb {R}$ Dijkstra's algorithm with Fibonacci heap $O(E+V\log {V})$ Fredman & Tarjan 1984, Fredman & Tarjan 1987
$\mathbb {N}$ Dial's algorithm (Dijkstra's algorithm using a bucket queue with L buckets) $O(E+LV)$ Dial 1969
$O(E\log {\log {L}})$ Johnson 1981, Karlsson & Poblete 1983
Gabow's algorithm $O(E\log _{E/V}L)$ Gabow 1983, Gabow 1985
$O(E+V{\sqrt {\log {L}}})$ Ahuja et al. 1990
Thorup $O(E+V\log {\log {V}})$ Thorup 2004

### Directed graphs with arbitrary weights without negative cycles

Weights Algorithm Time complexity Author
$\mathbb {R}$ O(V 2EL) Ford 1956
$\mathbb {R}$ Bellman–Ford algorithm O(VE) Shimbel 1955, Bellman 1958, Moore 1959
$\mathbb {R}$ Johnson-Dijkstra with binary heap O(V (E + log V)) Johnson 1977
$\mathbb {R}$ Johnson-Dijkstra with Fibonacci heap O(V (E + log V)) Fredman & Tarjan 1984, Fredman & Tarjan 1987, adapted after Johnson 1977
$\mathbb {N}$ Johnson's technique applied to Dial's algorithm O(V (E + L)) Dial 1969, adapted after Johnson 1977

### Directed graphs with arbitrary weights with negative cycles

Finds a negative cycle or calculates distances to all vertices.

Weights Algorithm Time complexity Author
$\mathbb {Z}$ Andrew V. Goldberg $O(E{\sqrt {V}}\log {N})$ ### Planar graphs with nonnegative weights

Weights Algorithm Time complexity Author
$\mathbb {R} _{\geq 0}$ $O(V)$ Henzinger et al. 1997

## All-pairs shortest paths

The all-pairs shortest path problem finds the shortest paths between every pair of vertices v, v' in the graph. The all-pairs shortest paths problem for unweighted directed graphs was introduced by Shimbel (1953), who observed that it could be solved by a linear number of matrix multiplications that takes a total time of O(V4).

### Undirected graph

Weights Time complexity Algorithm
$\mathbb {R}$ + O(V3) Floyd–Warshall algorithm
$\{1,\infty \}$ $O(V^{\omega }\log V)$ Seidel's algorithm (expected running time)
$\mathbb {N}$ $O(V^{3}/2^{\Omega (\log n)^{1/2}})$ Williams 2014
$\mathbb {R}$ + O(EV log α(E,V)) Pettie & Ramachandran 2002
$\mathbb {N}$ O(EV) Thorup 1999 applied to every vertex (requires constant-time multiplication).

### Directed graph

Weights Time complexity Algorithm
$\mathbb {R}$ (no negative cycles) O(V3) Floyd–Warshall algorithm
$\mathbb {N}$ $O(V^{3}/2^{\Omega (\log n)^{1/2}})$ Williams 2014
$\mathbb {R}$ (no negative cycles) O(EV + V2 log V) Johnson–Dijkstra
$\mathbb {R}$ (no negative cycles) O(EV + V2 log log V) Pettie 2004
$\mathbb {N}$ O(EV + V2 log log V) Hagerup 2000

## Applications

Shortest path algorithms are applied to automatically find directions between physical locations, such as driving directions on web mapping websites like MapQuest or Google Maps. For this application fast specialized algorithms are available.

If one represents a nondeterministic abstract machine as a graph where vertices describe states and edges describe possible transitions, shortest path algorithms can be used to find an optimal sequence of choices to reach a certain goal state, or to establish lower bounds on the time needed to reach a given state. For example, if vertices represent the states of a puzzle like a Rubik's Cube and each directed edge corresponds to a single move or turn, shortest path algorithms can be used to find a solution that uses the minimum possible number of moves.

In a networking or telecommunications mindset, this shortest path problem is sometimes called the min-delay path problem and usually tied with a widest path problem. For example, the algorithm may seek the shortest (min-delay) widest path, or widest shortest (min-delay) path.

A more lighthearted application is the games of "six degrees of separation" that try to find the shortest path in graphs like movie stars appearing in the same film.

Other applications, often studied in operations research, include plant and facility layout, robotics, transportation, and VLSI design.

A road network can be considered as a graph with positive weights. The nodes represent road junctions and each edge of the graph is associated with a road segment between two junctions. The weight of an edge may correspond to the length of the associated road segment, the time needed to traverse the segment, or the cost of traversing the segment. Using directed edges it is also possible to model one-way streets. Such graphs are special in the sense that some edges are more important than others for long-distance travel (e.g. highways). This property has been formalized using the notion of highway dimension. There are a great number of algorithms that exploit this property and are therefore able to compute the shortest path a lot quicker than would be possible on general graphs.

All of these algorithms work in two phases. In the first phase, the graph is preprocessed without knowing the source or target node. The second phase is the query phase. In this phase, source and target node are known. The idea is that the road network is static, so the preprocessing phase can be done once and used for a large number of queries on the same road network.

The algorithm with the fastest known query time is called hub labeling and is able to compute shortest path on the road networks of Europe or the US in a fraction of a microsecond. Other techniques that have been used are:

## Related problems

For shortest path problems in computational geometry, see Euclidean shortest path.

The shortest multiple disconnected path  is a representation of the primitive path network within the framework of Reptation theory. The widest path problem seeks a path so that the minimum label of any edge is as large as possible.

Other related problems may be classified into the following categories.

### Paths with constraints

Unlike the shortest path problem, which can be solved in polynomial time in graphs without negative cycles, shortest path problems which include additional constraints on the desired solution path are called Constrained Shortest Path First, and are harder to solve. One example is the constrained shortest path problem, which attempts to minimize the total cost of the path while at the same time maintaining another metric below a given threshold. This makes the problem NP-complete (such problems are not believed to be efficiently solvable for large sets of data, see P = NP problem). Another NP-complete example requires a specific set of vertices to be included in the path, which makes the problem similar to the Traveling Salesman Problem (TSP). The TSP is the problem of finding the shortest path that goes through every vertex exactly once, and returns to the start. The problem of finding the longest path in a graph is also NP-complete.

### Partial observability

The Canadian traveller problem and the stochastic shortest path problem are generalizations where either the graph isn't completely known to the mover, changes over time, or where actions (traversals) are probabilistic.  

### Strategic shortest paths

Sometimes, the edges in a graph have personalities: each edge has its own selfish interest. An example is a communication network, in which each edge is a computer that possibly belongs to a different person. Different computers have different transmission speeds, so every edge in the network has a numeric weight equal to the number of milliseconds it takes to transmit a message. Our goal is to send a message between two points in the network in the shortest time possible. If we know the transmission-time of each computer (the weight of each edge), then we can use a standard shortest-paths algorithm. If we do not know the transmission times, then we have to ask each computer to tell us its transmission-time. But, the computers may be selfish: a computer might tell us that its transmission time is very long, so that we will not bother it with our messages. A possible solution to this problem is to use a variant of the VCG mechanism, which gives the computers an incentive to reveal their true weights.

### Negative cycle detection

In some cases, the main goal is not to find the shortest path, but only to detect if the graph contains a negative cycle. Some shortest-paths algorithms can be used for this purpose:

• The Bellman–Ford algorithm can be used to detect a negative cycle in time $O(|V||E|)$ .
• Cherkassky and Goldberg survey several other algorithms for negative cycle detection.

## Linear programming formulation

There is a natural linear programming formulation for the shortest path problem, given below. It is very simple compared to most other uses of linear programs in discrete optimization, however it illustrates connections to other concepts.

Given a directed graph (V, A) with source node s, target node t, and cost wij for each edge (i, j) in A, consider the program with variables xij

minimize $\sum _{ij\in A}w_{ij}x_{ij}$ subject to $x\geq 0$ and for all i, $\sum _{j}x_{ij}-\sum _{j}x_{ji}={\begin{cases}1,&{\text{if }}i=s;\\-1,&{\text{if }}i=t;\\0,&{\text{ otherwise.}}\end{cases}}$ The intuition behind this is that $x_{ij}$ is an indicator variable for whether edge (i, j) is part of the shortest path: 1 when it is, and 0 if it is not. We wish to select the set of edges with minimal weight, subject to the constraint that this set forms a path from s to t, represented by the equality constraint: for all vertices except s and t the number of incoming and outcoming edges that are part of the path must be the same (i.e., that it should be a path from s to t).

This LP has the special property that it is integral; more specifically, every basic optimal solution (when one exists) has all variables equal to 0 or 1, and the set of edges whose variables equal 1 form an s-t dipath. See Ahuja et al. for one proof, although the origin of this approach dates back to mid-20th century.

The dual for this linear program is

maximize ytys subject to for all ij, yjyiwij

and feasible duals correspond to the concept of a consistent heuristic for the A* algorithm for shortest paths. For any feasible dual y the reduced costs $w'_{ij}=w_{ij}-y_{j}+y_{i}$ are nonnegative and A* essentially runs Dijkstra's algorithm on these reduced costs.

## General algebraic framework on semirings: the algebraic path problem

Many problems can be framed as a form of the shortest path for some suitably substituted notions of addition along a path and taking the minimum. The general approach to these is to consider the two operations to be those of a semiring. Semiring multiplication is done along the path, and the addition is between paths. This general framework is known as the algebraic path problem.

Most of the classic shortest-path algorithms (and new ones) can be formulated as solving linear systems over such algebraic structures.

More recently, an even more general framework for solving these (and much less obviously related problems) has been developed under the banner of valuation algebras.

## Shortest path in stochastic time-dependent networks

In real-life situations, the transportation network is usually stochastic and time-dependent. In fact, a traveler traversing a link daily may experiences different travel times on that link due not only to the fluctuations in travel demand (origin-destination matrix) but also due to such incidents as work zones, bad weather conditions, accidents and vehicle breakdowns. As a result, a stochastic time-dependent (STD) network is a more realistic representation of an actual road network compared with the deterministic one.

Despite considerable progress during the course of the past decade, it remains a controversial question how an optimal path should be defined and identified in stochastic road networks. In other words, there is no unique definition of an optimal path under uncertainty. One possible and common answer to this question is to find a path with the minimum expected travel time. The main advantage of using this approach is that efficient shortest path algorithms introduced for the deterministic networks can be readily employed to identify the path with the minimum expected travel time in a stochastic network. However, the resulting optimal path identified by this approach may not be reliable, because this approach fails to address travel time variability. To tackle this issue some researchers use distribution of travel time instead of expected value of it so they find the probability distribution of total travelling time using different optimization methods such as dynamic programming and Dijkstra's algorithm . These methods use stochastic optimization, specifically stochastic dynamic programming to find the shortest path in networks with probabilistic arc length. The concept of travel time reliability is used interchangeably with travel time variability in the transportation research literature, so that, in general, one can say that the higher the variability in travel time, the lower the reliability would be, and vice versa.

In order to account for travel time reliability more accurately, two common alternative definitions for an optimal path under uncertainty have been suggested. Some have introduced the concept of the most reliable path, aiming to maximize the probability of arriving on time or earlier than a given travel time budget. Others, alternatively, have put forward the concept of an α-reliable path based on which they intended to minimize the travel time budget required to ensure a pre-specified on-time arrival probability.