Shortest-Path Basics
1Introduction
This lecture formulates shortest-path problems that minimize either the number of edges or the sum of edge weights. It then explains the applicability conditions for BFS, Dijkstra's algorithm, and methods that permit negative edges.
The lecture assumes the definitions of vertices, edges, walks, and paths, together with the layered structure of BFS.
data/lecture/information/graph/graph-basics.lecture.n.md data/lecture/information/algorithm/search/dfs-and-bfs-basics.lecture.n.md2Graphs and routes
Let be a finite graph. An edge of an undirected graph has no orientation, whereas a directed edge permits movement only from to .
An -to- walk is a vertex sequence in which an edge of joins every pair of consecutive vertices. In a directed graph, each edge orientation must also agree with the sequence. A walk with no repeated vertex is a path. Its number of edges is .
An edge-weight function is a function that assigns a real number to each edge. The cost of a walk is
For an undirected edge, denotes the weight of the undirected edge joining those two vertices. Minimizing the number of edges is equivalent to assigning weight to every edge.
3Distance and unreachability
Define the distance from to as the infimum of the costs of all -to- walks:
If is unreachable from , the candidate set is empty, and we define .
A closed walk is a walk whose first and last vertices coincide. A cycle is a closed walk with no repeated vertices other than its coincident endpoints. In a directed graph, every negative closed walk contains at least one negative cycle in its decomposition.
Suppose a negative cycle is reachable from and is reachable from that cycle. Repeating the cycle arbitrarily many times gives , and no walk attains a minimum cost. In an undirected graph, traversing a negative edge in both directions forms a negative closed walk. Thus the distance is whenever such an edge can occur on an -to- route.
Now suppose that no relevant negative cycle exists and that is reachable. If an arbitrary -to- walk repeats a vertex, the intervening closed subwalk has nonnegative cost and can be removed without increasing the cost. Repeating this operation produces a path. A finite graph has only finitely many -to- paths, so one of them attains the minimum cost. Such a path is called a shortest path from to .
4Choosing a method from the weights
4.1Equal edge weights
If all edge weights equal the same positive constant , the cost is times the number of edges. Thus minimizing cost is equivalent to minimizing the number of edges. BFS processes vertices in nondecreasing order of their edge counts from the source, so it computes the shortest distance to every reachable vertex. An unweighted graph is interpreted as the case .
4.2Nonnegative edge weights
If for every edge, Dijkstra's algorithm applies. It selects an unsettled vertex with minimum tentative distance and uses candidate routes through that vertex to update the tentative distances of adjacent vertices. Nonnegativity ensures that no later route can decrease a settled distance.
4.3Negative edges
A negative edge is an edge with . If even one negative edge exists, the justification for settling vertices solely by tentative distance in Dijkstra's algorithm no longer holds. A negative edge alone, however, does not imply that a shortest path fails to exist. If no relevant negative cycle exists, a method that permits negative edges, such as the Bellman--Ford algorithm, can be used.
data/lecture/information/algorithm/graph/dijkstra-algorithm-basics.lecture.n.md5Optimal substructure
Let be a shortest path from to . For every , the subpath is also a shortest path from to . This property is called the optimal substructure of shortest paths.
Indeed, suppose that a walk from to had lower cost than . Replacing that portion of with would construct a walk from to with lower cost than , contradicting the optimality of . The claim therefore holds. This property underlies shortest-path algorithms that combine optimal values of subproblems through relaxation.
6Summary
- A shortest-path problem must specify whether the graph is directed or undirected and distinguish walks from paths.
- The distance is when the target is unreachable and when a relevant negative cycle exists.
- BFS applies to equal weights, and Dijkstra's algorithm applies to nonnegative weights. Negative edges require a different method.
- Every subpath of a shortest path is itself a shortest path between its endpoints.