The shortest paths problem is one of the most fundamental problems in graph theory. Given a directed graph , possibly weighted, and a set of pairs of vertices , the problem is to compute, for each , a simple path in from to (a list of vertices such that for all , ) such that no other simple path in from to has a lower total weight.
Shortest paths in undirected graphs can be computed by replacing each undirected edge with two arcs of the same weight, one going in each direction, to obtain a directed graph.
Theorem: In a graph with no cycles of negative weight, the shortest path is no shorter than the shortest simple path. (On the other hand, in a graph with a negative-weight cycle, lengths of paths may be unbounded below.)
Proof: We show that any path from to can be transformed into a simple path from to which is at least as short. Let the path be denoted . We proceed by induction on the number of pairs (with ) such that , which is countable. When there are zero such pairs, the path is already simple, and nothing needs to be done. Otherwise, we transform the path into another path with fewer such pairs but equal or lesser weight by removing the vertices from the path. The weight of the path therefore decreases by an amount equal to the weight of the cycle , which is nonnegative.
Corollary: Assuming our graphs have no cycles of negative weight, the restriction that the shortest paths be simple is immaterial. Therefore, we will assume in the foregoing discussion that our graphs have no cycles of negative weight, for the problem of finding shortest simple paths in graphs containing negative-weight cycles is NP-complete.
Corollary: In a finite connected graph, a shortest path always exists. (To prove this we simply use the fact that the graph has a finite number of simple paths, and only simple paths need be considered. So one of them must be the shortest.)
Three variations of the shortest path algorithm exist, and they are discussed in the following sections.
- In the single-pair shortest path problem, there is only one pair in the problem set. In other words the shortest path is desired between a single pair of vertices.
- In the single-source shortest paths problem, the problem set is of the form . One vertex, , is designated the source, and we wish to find the shortest paths from the source to all other vertices. (To solve the analogous single-destination shortest paths problem, we merely reverse the directions of all edges, which reduces it to single-source.)
- In the all-pairs shortest paths problem, the problem set is ; that is, we wish to know the shortest paths from every vertex to every other vertex.
All the shortest paths algorithms discussed in this article have the same basic approach. At their core, they compute not the shortest paths themselves, but the distances. Using information computed in order to compute the distances, one can easily then reconstruct the paths themselves. They begin with the knowledge that the distance from any vertex to itself is zero, and they overestimate all other distances they need. (By this it is meant that they find a number for each pair under consideration such that the distance from to is less than or equal to .) If , then the initial overestimate for the distance from to is the weight of the edge ; otherwise it is infinite. At some point, all overestimates will be refined, perhaps gradually, perhaps at once, so that once the algorithm has terminated, they are exactly the correct distances.
There are theoretically many ways to refine overestimates but a specific way, known as relaxation, is used in all the algorithms discussed in this article. Relaxation can take place when three conditions are met:
- The currently best overestimate for the distance from some vertex to some vertex is ;
- The currently best overestimate for the distance from to some vertex is ,
- The currently best overestimate for the distance from to is greater than . (This includes the case in which it is infinite.)
Relaxation refines the best overestimate for the distance from to by setting it to , which is better than its current value.
Theorem: When the distances from to all other vertices are all overestimated, but no relaxations are possible, then those distances are all known correctly. Contrapositively, if at there exists such that the distance from to is not correctly known, then relaxation must be possible somewhere in the graph.
Proof: By induction on the number of edges in some shortest path from to (which we can take to be simple). It is vacuously true when this is zero or one, because all paths of length zero or one were accounted for in the initial overestimates. Assume the path has at least two edges, and denote by the last vertex on the path before . If the distance from to or from to is not known, then by the inductive hypothesis, we are done. Otherwise, notice that the path contains two subpaths, one from to and one from to (the latter is trivial as it consists of a single edge), and that each of these must itself be a shortest path, otherwise we could replace it with a shorter path to obtain a shorter path from to , a contradiction. Now, as the distances from to and to are correctly known, and the correct distance from to is exactly the sum of the distances from to and to (as these values equal the weights of the aforementioned subpaths), and our overestimate of the distance from to is incorrect, it must be strictly greater than the sum of the distances from to and to . Hence can be relaxed.