Sub topic 2
The Single-Source Shortest Path Problem
This chapter assumes you know basic graph terms (vertices, directed and undirected edges), adjacency lists, Big-O notation, breadth-first search (BFS), and everyday Python. It defines the problem precisely. Later chapters solve it.
Paths and Their Weights
A path from vertex s to vertex v is a sequence of vertices s = v0, v1, ..., vk = v in which each consecutive pair is joined by an edge. In a weighted graph, the weight of a path is the sum of its edge weights:
w(p) = w(v0, v1) + w(v1, v2) + ... + w(vk-1, vk)
The shortest-path distance δ(s, v) is the smallest weight over all paths from s to v. If no path exists, δ(s, v) = ∞. A path whose weight equals δ(s, v) is a shortest path. A graph can contain several shortest paths with the same weight.
From A to D in this graph there are three routes:
| Path | Weight |
|---|---|
| A → B → D | 4 + 1 = 5 |
| A → C → D | 1 + 5 = 6 |
| A → C → B → D | 1 + 2 + 1 = 4 |
The route with the most edges is the cheapest. BFS minimizes the number of edges, so it would choose A → B → D (weight 5) and give the wrong answer. BFS works only when every edge has the same weight.
The Problem Statement
Single-source shortest paths (SSSP): given a weighted graph G = (V, E) and a source vertex s, compute δ(s, v) for every vertex v.
A full solution usually returns two things:
- a distance map that gives
δ(s, v)for each vertex, and - a predecessor map that records, for each vertex, the previous vertex on some shortest path. Together these entries form a shortest-path tree rooted at
s.
The same problem comes in several related forms:
| Variant | Question | Usual approach |
|---|---|---|
| Single-source | `s` to every vertex | Dijkstra, Bellman-Ford |
| Single-pair | `s` to one target `t` | SSSP with early stopping, A* |
| Single-destination | every vertex to `t` | SSSP on the reversed graph |
| All-pairs | every vertex to every vertex | Repeated SSSP, Floyd-Warshall |
No known algorithm solves single-pair asymptotically faster than single-source in the worst case. That is why SSSP is the core problem.
A Brute-Force Definition in Code
The following code computes δ straight from the definition by enumerating every simple path. It takes exponential time, so use it only to check answers on tiny graphs.
import math
graph = {
"A": [("B", 4), ("C", 1)],
"C": [("B", 2), ("D", 5)],
"B": [("D", 1)],
"D": [],
}
def simple_paths(graph, u, target, path=None):
if path is None:
path = [u]
if u == target:
yield list(path)
return
for v, _ in graph.get(u, []):
if v not in path:
path.append(v)
yield from simple_paths(graph, v, target, path)
path.pop()
def path_weight(graph, path):
return sum(dict(graph[a])[b] for a, b in zip(path, path[1:]))
def brute_force_distance(graph, s, t):
weights = [path_weight(graph, p) for p in simple_paths(graph, s, t)]
return min(weights, default=math.inf)
for p in simple_paths(graph, "A", "D"):
print(p, path_weight(graph, p))
print({v: brute_force_distance(graph, "A", v) for v in graph})
# {'A': 0, 'C': 1, 'B': 3, 'D': 4}Two Properties You Will Rely On
Optimal substructure. Every subpath of a shortest path is also a shortest path. A → C → B → D is shortest to D, so A → C → B must be shortest to B. If a cheaper route to B existed, you could swap it in and get a cheaper route to D. Dijkstra's algorithm builds on this fact: it extends known shortest paths one edge at a time.
Well-definedness. If a cycle with negative total weight can be reached from s, you can go around it forever and the "shortest" weight drops without bound. In that case δ is undefined. Dijkstra's algorithm avoids this case and a subtler one by requiring all edge weights to be non-negative. A later chapter shows what fails without that rule.
Exercise
In the graph above, change the weight of edge C → B from 2 to 4. What is δ(A, D) now, and which path achieves it?
Show answer
The paths now weigh A→B→D = 5, A→C→B→D = 1+4+1 = 6, and A→C→D = 6. So δ(A, D) = 5, achieved by A → B → D.
Exercise
Add a vertex E to the graph with no incoming edges. What should an SSSP solution from A report for E, and why can't optimal substructure give it a predecessor?
Show answer
δ(A, E) = ∞ because no path reaches E. E is not on any path from A, so it gets no predecessor and is not in the shortest-path tree.