In graph theory, a shortest path is a path between two different nodes of a graph which has a minimum length with respect to an edge weight function . If the edges in the graph all have the weight 1, that is for all edges , then the shortest path is a - -path with the smallest possible number of edges between and .
In the literature, the problem is often referred to as the Shortest Path Problem .
The complexity depends largely on the type of weight function and on whether paths or edge trains are being considered. In edge trains, nodes and edges can be repeated, while paths do not use any nodes twice. There are three types of weight functions:
- Weight functions without negative weights;
- Conservative weight function: A weight function is said to be conservative for the graph if for all cycles of ;
- Weight functions with any weights.
The exact formulation of the problem is crucial in order to be able to answer the question of complexity.
- Without negative weights
- With Dijkstra's algorithm the problem can be solved in a running time of , where denotes the number of edges and the number of nodes in the graph. Note that the shortest paths are also the shortest edges. If all weights are genuinely positive, then the shortest paths match the shortest edges.
- With any weight and with edges
- If the graph contains a cycle in which the sum over the weights is strictly negative, then there are nodes that do not have a shortest line of edges. If there is an edge from to this cycle and an edge from this cycle to , then you can create an arbitrarily short edge from to by running through the cycle only sufficiently often. The Bellman-Ford algorithm can find a shortest edge train (if it exists) in a running time of or prove that it does not exist by finding a negative cycle. The decision problem as to whether there is a path of length can thus be solved in polynomial time.
- With arbitrary weights and with paths
- This variant of the problem is NP- hard. This can be proven, for example, by a reduction of the NP-heavy Hamiltonian path problem by setting all weights to −1 in the shortest path problem. Note that this construction contains negative cycles, and therefore the NP severity does not hold for conservative weight functions.
- Conservative weight function and with paths
- The Bellman-Ford algorithm can find a shortest path in a running time of .
The literature is mostly limited to non-negative weights or conservative weight function. With one of these additional requirements, every shortest path is automatically a shortest line of edges and that is why this distinction is often not made in the literature.
Variations of the problem
Aside from determining a shortest - path, there are a few other, but very similar problems:
Single-source shortest path (SSSP)
This variant of the shortest paths problem deals with the problem of how to compute the shortest paths between a given starting node and all other nodes of a graph. For nonnegative weight functions, the Dijkstra algorithm or the A * algorithm can be adapted in order to calculate the shortest paths to all nodes of the graph. On the other hand, the Bellman-Ford algorithm always calculates the shortest paths to all other nodes for any conservative weight functions.
Single-destination shortest path (SDSP)
The aim here is to determine the shortest path between an end node and all other nodes of the graph. This problem can be described as SSSP by reversing the edge directions.
All-pairs shortest path (APSP)
This variant of the problem is about determining the shortest paths between all pairs of nodes in a graph. Depending on the weight function, it is more efficient to solve the SSSP for each node one after the other or to use specialized methods such as the Floyd-Warshall algorithm or the min-plus matrix multiplication algorithm , which determine the shortest paths for all pairs at the same time.
In the graph on the right there is a shortest path between the nodes and the path that starts in and goes over to . The path costs are here . However, if you want to find a path from to , the direct path with costs from is not the shortest possible path, since the path from via to only has costs from .
Formulation as a linear program
A linear program can also be used to determine a shortest path . In this case, the path is interpreted as a flow with a flow value of 1 on the edges of the graph. The determination of the shortest path is then a special case of the min-cost-flow problem. The corresponding formulation is:
If there is a - path in the given graph, then the program has a feasible solution. However, the program is unlimited if the weight function is not conservative. In this case, the flow can be increased arbitrarily along a cycle with negative costs. Otherwise the problem has an optimal solution which corresponds to a vector with entries. The set then describes a shortest - path, the objective function value of the program corresponds to the length of the path.
It turns out that the dualization of the above linear program has an illustrative interpretation. The dual program is given by
A solution to the dual program is called a node potential . It is easy to see that the vector is also a solution for every solution , and one can choose at will. As a rule, the value of is set so that . The objective function is then given by .
If there is any path between and a node , the length of the path can be estimated as follows:
The potential of each node is therefore a lower bound for the length of a path. An optimal solution of the dual program can be found if one sets the potential of a node as the length of the shortest - path with respect to the objective function .
Algorithms that calculate a shortest path are often used in the calculation of travel routes. For example, the distance between two cities can be calculated. The cities are the nodes of the graph and the streets are the edges. Various algorithms are in the free Python - Library NetworkX implemented.
Shortest routes with constraints
A generalization of the problem is obtained if one only considers - paths which obey the additional inequality . There is another weight function and a real number.
The resulting constrained shortest path problem is then also NP-difficult for conservative or nonnegative objective functions, see HC Joksch (1966).
- Thomas H. Cormen, Charles E. Leiserson , Ronald L. Rivest , Clifford Stein: Introduction to Algorithms . 2nd Edition. MIT Press, Cambridge (Massachusetts) 2001, ISBN 0-262-03293-7 .
- Thomas H. Cormen, Charles E. Leiserson , Ronald L. Rivest , Clifford Stein: Algorithms - An Introduction . 2nd Edition. 2007. ISBN 978-3-486-58262-8
- HC Joksch (1966). The shortest route problem with constraints . J. Math. Anal. Appl. 14, pages 191-197