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 P=(v1,v2,…,vn)∈V×V×⋯×V such that vi is adjacent to vi+1 for 1≤i<n. Such a path P is called a path of length n−1 from v1 to vn. (The vi are variables; their numbering relates to their position in the sequence and need not relate to a canonical labeling.) Let E={ei,j} where ei,j is the edge incident to both vi and vj. Given a real-valued weight function f:E→ℝ, and an undirected (simple) graph G, the shortest path from v to v′ is the path P=(v1,v2,…,vn) (where v1=v and vn=v′) that over all possible n minimizes the sum ∑i=1n−1f(ei,i+1). When each edge in the graph has unit weight or f:E→{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 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 Cherkassky, Goldberg & Radzik (1996).
Single-source shortest paths
Undirected graphs
Unweighted graphs
Directed acyclic graphs An algorithm using topological sorting can solve the single-source shortest path problem in time Θ(E + V) in arbitrarily-weighted directed acyclic graphs.
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.
Directed graphs with arbitrary weights without negative cycles
Directed graphs with arbitrary weights with negative cycles Finds a negative cycle or calculates distances to all vertices.
Planar graphs with nonnegative weights
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 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). For undirected and unweighted graphs, Dory et al. gives an algorithm computing a 2-approximation for all pairs of vertices in O(n2.032) time. In 2026, Gupta and Shandilya gives a randomized algorithm running in O~(n2) time that yields a 2-approximation for all pairs whose distance is at least a fixed constant, with high probability.
Undirected graph
Directed graph
… excerpt ends here. Continue reading the full article.

