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 or distance of each segment.

Definition The shortest path problem can be defined for graphs whether undirected, directed, or mixed. The definition for undirected graphs states that every edge can be traversed in either direction. Directed graphs require 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 such that is adjacent to for . Such a path is called a path of length from to . (The are variables; their numbering relates to their position in the sequence and need not relate to a canonical labeling.)

Let where is the edge incident to both and . Given a real-valued weight function , and an undirected (simple) graph , the shortest path from to is the path (where and ) that over all possible minimizes the sum When each edge in the graph has unit weight or , 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 Several well-known algorithms exist for solving this problem and its variants. Dijkstra's algorithm solves the single-source shortest path problem with only non-negative edge weights. Bellman–Ford algorithm solves the single-source problem if edge weights may be negative. A* search algorithm solves for single-pair shortest path using heuristics to try to speed up the search. Floyd–Warshall algorithm solves all pairs shortest paths. Johnson's algorithm solves all pairs shortest paths, and may be faster than Floyd–Warshall on sparse graphs. Viterbi algorithm solves the shortest stochastic path problem with an additional probabilistic weight on each node.

Additional algorithms and associated evaluations may be found in .

Single-source shortest paths

Undirected graphs Weights Time complexity Author + + (binary heap) + (Fibonacci heap) (requires constant-time multiplication) +

Unweighted graphs Algorithm Time complexity Author Breadth-first search

Directed acyclic graphs An algorithm using topological sorting can solve the single-source shortest path problem in time in arbitrarily-weighted directed acyclic graphs.

Directed graphs with nonnegative weights The following table is taken from , 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 Bellman–Ford algorithm , , Dijkstra's algorithm with list , , Minty (see ), Dijkstra's algorithm with binary heap Dijkstra's algorithm with Fibonacci heap , Quantum Dijkstra algorithm with adjacency list Dürr et al. 2006Dijkstra's-Bellman–Ford hybrid with a divide-and-conquer frontier reduction Dial's algorithm (Dijkstra's algorithm using a bucket queue with L buckets) , Gabow's algorithm , Thorup

Directed graphs with arbitrary weights without negative cycles Weights Algorithm Time complexity Author Bellman–Ford algorithm , , Johnson-Dijkstra with binary heap Johnson-Dijkstra with Fibonacci heap , , adapted after Johnson's technique applied to Dial's algorithm , adapted after Interior-point method with Laplacian solverInterior-point method with flow solverRobust interior-point method with sketching interior-point method with dynamic min-ratio cycle data structureBased on low-diameter decompositionHop-limited shortest pathsSteiner-Tree Gadgets

Directed graphs with arbitrary weights with negative cycles Finds a negative cycle or calculates distances to all vertices. Weights Algorithm Time complexity Author Andrew V. Goldberg

Planar graphs with nonnegative weights Weights Algorithm Time complexity Author

Applications Network flows are a fundamental concept in graph theory and operations research, often used to model problems involving the transportation of goods, liquids, or information through a network. A network flow problem typically involves a directed graph where each edge represents a pipe, wire, or road, and each edge has a capacity, which is the maximum amount that can flow through it. The goal is to find a feasible flow that maximizes the flow from a source node to a sink node.

Shortest Path Problems can be used to solve certain network flow problems, particularly when dealing with single-source, single-sink networks. In these scenarios, we can transform the network flow problem into a series of shortest path problems.

Transformation Steps

Create a Residual Graph: For each edge (u, v) in the original graph, create two edges in the residual graph: (u, v) with capacity c(u, v) (v, u) with capacity 0 The residual graph represents the remaining capacity available in the network. Find the Shortest Path: Use a shortest path algorithm (e.g., Dijkstra's algorithm, Bellman-Ford algorithm) to find the shortest path from the source node to the sink node in the residual graph. Augment the Flow: Find the minimum capacity along the shortest path. Increase the flow on the edges of the shortest path by this minimum capacity. Decrease the capacity of the edges in the forward direction and increase the capacity of the edges in the backward direction. Update the Residual Graph: Update the residual graph based on the augmented flow. Repeat: Repeat steps 2-4 until no more paths can be found from the source to the sink.

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

For undirected and unweighted graphs, Dory et al. gives an algorithm computing a 2-approximation for all pairs of vertices in time. In 2026, Gupta and Shandilya gives a randomized algorithm running in time that yields a 2-approximation for all pairs whose distance is at least a fixed constant, with high probability.

Undirected graph Weights Time complexity Algorithm + Floyd–Warshall algorithm Seidel's algorithm (expected running time using fast matrix multiplication algorithms) + applied to every vertex (requires constant-time multiplication).

Directed graph Weights Time complexity Algorithm (no negative cycles) Floyd–Warshall algorithm (no negative cycles) Quantum search (no negative cycles) Johnson–Dijkstra (no negative cycles)

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.

Another 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.

Road networks 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.Abraham, Ittai; Fiat, Amos; Goldberg, Andrew V.; Werneck, Renato F. "Highway Dimension, Shortest Paths, and Provably Efficient Algorithms". ACM-SIAM Symposium on Discrete Algorithms, pages 782–793, 2010. 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.Abraham, Ittai; Delling, Daniel; Goldberg, Andrew V.; Werneck, Renato F. research.microsoft.com/pubs/142356/HL-TR.pdf "A Hub-Based Labeling Algorithm for Shortest Paths on Road Networks". Symposium on Experimental Algorithms, pages 230–241, 2011. Other techniques that have been used are:

ALT (A* search, landmarks, and triangle inequality) Arc flags Contraction hierarchies Transit node routing Reach-based pruning Labeling Hub labels

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