Chapter 6: Shortest Paths
Introduction
Learning Outcomes
Distinguish single-source, all-pairs and single-pair shortest-path problems; explain relaxation and the predecessor (π) array.
Run both algorithms step by step on an example graph, fill in the distance and π tables, and trace the Java code.
Show why Dijkstra fails on negative edges; detect negative cycles with Bellman-Ford and Floyd-Warshall.
Update the all-pairs D and Π matrices for k = 1…n; apply A*'s goal-directed search with f = g + h, and compare the algorithms.
Introduction
A Road Network Is a Graph
- Intersections are vertices, roads are edges, and road lengths or travel times are weights.
- The length of a path = the sum of its edge weights.
- A shortest path between two vertices is a path of minimum total weight; its length is written δ(s, v).
- Example: from Home to School, Home–1–2–4–School = 4 + 2 + 3 + 3 = 12; Home–1–4–School has fewer edges but costs 13.
Introduction
Kinds of Shortest-Path Problems
| Algorithm | What it finds | Restriction / note |
|---|---|---|
| Dijkstra | source to all vertices | edge weights must be nonnegative |
| Bellman-Ford | source to all vertices | handles negative edges, detects negative cycles; slower than Dijkstra |
| Floyd-Warshall | all pairs | handles negative edges; detects negative cycles on the diagonal |
| A* | start to goal | speeds up search with a heuristic estimate |
| BFS | source to all vertices | unweighted (or equal-weight) graphs only |
Introduction
The Basic Operation: Relaxation
Each vertex keeps two values:
d[v]: the shortest-path estimate so far (∞ at first, 0 at the source)π[v]: the vertex before v on that path (its predecessor)
RELAX(u, v, w):
if d[u] + w(u, v) < d[v]:
d[v] = d[u] + w(u, v)
π[v] = ud[u] + w = 5 + 3 = 8 < 9 → d[v] = 8, π[v] = u
Introduction
Properties of Shortest Paths
Every subpath of a shortest path is itself a shortest path. If s ⇝ u ⇝ v is shortest, so is s ⇝ u. Dynamic programming and greedy methods rest on this.
For every edge (u, v), δ(s, v) ≤ δ(s, u) + w(u, v). Relaxation fixes edges that violate it.
If a cycle of negative total weight is reachable from the source, each lap makes the path shorter: the shortest path is undefined (−∞).
Without negative cycles a shortest path has no cycle, so it has at most V − 1 edges. That is where Bellman-Ford's V − 1 passes come from.
Dijkstra
Dijkstra's Algorithm
- Finds shortest paths from a source vertex to every other vertex (single-source).
- Devised by Edsger W. Dijkstra in 1956 and published in 1959.
- Correct only on graphs whose edge weights are nonnegative.
- Greedy: at each step it picks the not-yet-settled vertex with the smallest d value and settles it.
- The neighbors of the chosen vertex are relaxed.
Dijkstra
Dijkstra: The Steps
Step 1: Choose the start vertex and set its distance to 0; every other vertex gets ∞.
Initialization: d[s] = 0, d[v] = ∞, π[v] = undefined, S = ∅.
Step 2: From the chosen vertex, compute distances to its not-yet-processed neighbors.
Candidate distance for neighbor v: d[u] + w(u, v).
Step 3: If a shorter path is found, update the neighbor's distance (relaxation).
If d[u] + w(u, v) < d[v], d[v] and π[v] change.
Step 4: Mark the processed vertex (add it to S) and select the unmarked vertex with the smallest distance.
The selection uses an array or a priority queue.
Step 5: Repeat steps 2–4 until every vertex is processed.
Unreachable vertices keep distance ∞.
Dijkstra
Dijkstra Example 1: Undirected Graph, Source 1
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | ∞ | – |
| 3 | ∞ | – |
| 4 | ∞ | – |
| 5 | ∞ | – |
| 6 | ∞ | – |
Initialization: source 1, d[1] = 0, all others ∞. No vertex is settled.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | ∞ | – |
| 3 | ∞ | – |
| 4 | ∞ | – |
| 5 | ∞ | – |
| 6 | ∞ | – |
Smallest d among unsettled vertices: 1 (d = 0); it is settled.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | ∞ | – |
| 4 | ∞ | – |
| 5 | ∞ | – |
| 6 | ∞ | – |
Edge (1, 2): d[1] + 7 = 7 < ∞ → d[2] = 7, π[2] = 1.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | ∞ | – |
| 5 | ∞ | – |
| 6 | ∞ | – |
Edge (1, 3): d[1] + 9 = 9 < ∞ → d[3] = 9, π[3] = 1.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | ∞ | – |
| 5 | ∞ | – |
| 6 | 14 | 1 |
Edge (1, 6): d[1] + 14 = 14 < ∞ → d[6] = 14, π[6] = 1.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | ∞ | – |
| 5 | ∞ | – |
| 6 | 14 | 1 |
Smallest d among unsettled vertices: 2 (d = 7); it is settled. Settled neighbors (1) are skipped.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | ∞ | – |
| 5 | ∞ | – |
| 6 | 14 | 1 |
Edge (2, 3): d[2] + 10 = 17 ≥ 9 → no change.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 22 | 2 |
| 5 | ∞ | – |
| 6 | 14 | 1 |
Edge (2, 4): d[2] + 15 = 22 < ∞ → d[4] = 22, π[4] = 2.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 22 | 2 |
| 5 | ∞ | – |
| 6 | 14 | 1 |
Smallest d among unsettled vertices: 3 (d = 9); it is settled. Settled neighbors (1, 2) are skipped.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 20 | 3 |
| 5 | ∞ | – |
| 6 | 14 | 1 |
Edge (3, 4): d[3] + 11 = 20 < 22 → d[4] = 20, π[4] = 3.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 20 | 3 |
| 5 | ∞ | – |
| 6 | 11 | 3 |
Edge (3, 6): d[3] + 2 = 11 < 14 → d[6] = 11, π[6] = 3.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 20 | 3 |
| 5 | ∞ | – |
| 6 | 11 | 3 |
Smallest d among unsettled vertices: 6 (d = 11); it is settled. Settled neighbors (1, 3) are skipped.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 20 | 3 |
| 5 | 20 | 6 |
| 6 | 11 | 3 |
Edge (6, 5): d[6] + 9 = 20 < ∞ → d[5] = 20, π[5] = 6.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 20 | 3 |
| 5 | 20 | 6 |
| 6 | 11 | 3 |
Smallest d among unsettled vertices: 4 (d = 20); it is settled. (4 and 5 tie at 20; the lower-numbered 4 is taken.) Settled neighbors (2, 3) are skipped.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 20 | 3 |
| 5 | 20 | 6 |
| 6 | 11 | 3 |
Edge (4, 5): d[4] + 6 = 26 ≥ 20 → no change.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 20 | 3 |
| 5 | 20 | 6 |
| 6 | 11 | 3 |
Smallest d among unsettled vertices: 5 (d = 20); it is settled. Settled neighbors (4, 6) are skipped.
| vertex | d | π |
|---|---|---|
| 1 | 0 | – |
| 2 | 7 | 1 |
| 3 | 9 | 1 |
| 4 | 20 | 3 |
| 5 | 20 | 6 |
| 6 | 11 | 3 |
Done: distances from 1 are 2:7, 3:9, 6:11, 4:20, 5:20. The green edges form the shortest-path tree (e.g. 1 → 3 → 6 → 5).
Dijkstra
Dijkstra Example 2: Directed Graph, Sets Q and S
Q
| d | u | π |
|---|---|---|
| 0 | 5 | / |
| ∞ | 1 | / |
| ∞ | 2 | / |
| ∞ | 3 | / |
| ∞ | 4 | / |
S
| u | d | π |
|---|---|---|
Initialization: source 5 has distance 0, all others ∞. Every vertex is in Q, S = ∅.
Q
| d | u | π |
|---|---|---|
| 2 | 4 | 5 |
| 4 | 2 | 5 |
| ∞ | 1 | / |
| ∞ | 3 | / |
S
| u | d | π |
|---|---|---|
| 5 | 0 | / |
Step 1: take 5 from Q and put it in S with d = 0. Relax: (5,2): ∞ → 4; (5,4): ∞ → 2.
Q
| d | u | π |
|---|---|---|
| 3 | 2 | 4 |
| 3 | 3 | 4 |
| ∞ | 1 | / |
S
| u | d | π |
|---|---|---|
| 5 | 0 | / |
| 4 | 2 | 5 |
Step 2: take 4 from Q and put it in S with d = 2. Relax: (4,2): 4 → 3; (4,3): ∞ → 3.
Q
| d | u | π |
|---|---|---|
| 3 | 3 | 4 |
| 6 | 1 | 2 |
S
| u | d | π |
|---|---|---|
| 5 | 0 | / |
| 4 | 2 | 5 |
| 2 | 3 | 4 |
Step 3: take 2 from Q and put it in S with d = 3. Relax: (2,1): ∞ → 6. (2 and 3 tie at 3; the lower-numbered 2 is taken.)
Q
| d | u | π |
|---|---|---|
| 6 | 1 | 2 |
S
| u | d | π |
|---|---|---|
| 5 | 0 | / |
| 4 | 2 | 5 |
| 2 | 3 | 4 |
| 3 | 3 | 4 |
Step 4: take 3 from Q and put it in S with d = 3. Relax: (3,4): 4 is already in S. No shorter path (no relaxation).
Q
| d | u | π |
|---|---|---|
S
| u | d | π |
|---|---|---|
| 5 | 0 | / |
| 4 | 2 | 5 |
| 2 | 3 | 4 |
| 3 | 3 | 4 |
| 1 | 6 | 2 |
Step 5: take 1 from Q and put it in S with d = 6. Relax: (1,3): 3 is already in S; (1,4): 4 is already in S. No shorter path (no relaxation).
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| d | 6 | 3 | 3 | 2 | 0 |
| π | 2 | 4 | 4 | 5 | / |
Final state: shortest paths from 5. Only the π edges (green) remain: 5→4→2→1 (6), 5→4→3 (3).
Dijkstra
Dijkstra: Pseudocode
DIJKSTRA(G, source):
dist = [ ]; visited = [ ]
pq = { }
for each v in V:
dist[v] = infinity
visited[v] = 0
dist[source] = 0
pq.add(source, 0)
while pq is not empty do:
u = pq.extractMin() // smallest distance
if visited[u] == 0:
visited[u] = 1
for each (u, v) in G[u]:
if dist[u] + weight(u, v) < dist[v]:
dist[v] = dist[u] + weight(u, v)
pq.add(v, dist[v])
return distvisited check skips it. To recover paths, relaxation also stores prev[v] = u.Dijkstra
Dijkstra (Priority Queue): Step Through the Code
void dijkstra(int src) { int[] dist = new int[V + 1]; int[] prev = new int[V + 1]; boolean[] visited = new boolean[V + 1]; Arrays.fill(dist, INF); dist[src] = 0; PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]); pq.add(new int[] { src, 0 }); while (!pq.isEmpty()) { int u = pq.poll()[0]; if (visited[u]) continue; // stale entry visited[u] = true; for (Edge e : adj.get(u)) { int v = e.to, w = e.weight; if (!visited[v] && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; prev[v] = u; pq.add(new int[] { v, dist[v] }); } } } }
Set up the arrays (vertices 1..5, index 0 unused); dist[5] = 0 and (5, 0) is added to the queue.
Poll the entry with the smallest distance, (5, 0): u = 5.
5 is settled: visited[5] = true.
Edge 5→2 (w = 4): 0 + 4 < ∞ → condition true.
dist[2] = 4, prev[2] = 5; add (2, 4) to the queue.
Edge 5→4 (w = 2): 0 + 2 < ∞ → condition true.
dist[4] = 2, prev[4] = 5; add (4, 2) to the queue.
Poll the entry with the smallest distance, (4, 2): u = 4.
4 is settled: visited[4] = true.
Edge 4→2 (w = 1): 2 + 1 < 4 → condition true.
dist[2] = 3, prev[2] = 4; add (2, 3) to the queue.
Edge 4→3 (w = 1): 2 + 1 < ∞ → condition true.
dist[3] = 3, prev[3] = 4; add (3, 3) to the queue.
Poll the entry with the smallest distance, (2, 3): u = 2.
2 is settled: visited[2] = true.
Edge 2→1 (w = 3): 3 + 3 < ∞ → condition true.
dist[1] = 6, prev[1] = 2; add (1, 6) to the queue.
Poll the entry with the smallest distance, (3, 3): u = 3.
3 is settled: visited[3] = true.
Edge 3→4 (w = 2): visited[4] = true → condition false.
Poll the entry with the smallest distance, (2, 4): u = 2.
visited[2] is already true: this is 2's stale entry (2, 4) → skipped with continue (lazy deletion).
Poll the entry with the smallest distance, (1, 6): u = 1.
1 is settled: visited[1] = true.
Edge 1→3 (w = 6): visited[3] = true → condition false.
Edge 1→4 (w = 3): visited[4] = true → condition false.
The queue is empty and the loop ends. dist = [6, 3, 3, 2, 0], prev = [2, 4, 4, 5, –].
Dijkstra
Path Reconstruction: The π (Predecessor) Array
Result of Example 2: π = [2, 4, 4, 5, /]. Walking back from target 1:
1 ← π[1] = 2 ← π[2] = 4 ← π[4] = 5 (source)
Reversed, the path is 5 → 4 → 2 → 1, of length 2 + 1 + 3 = 6 = d[1].
void printPath(int[] prev, int[] dist, int src, int target) { if (dist[target] == INF) { // unreachable System.out.print("no path"); return; } Deque<Integer> stack = new ArrayDeque<>(); for (int v = target; v != src; v = prev[v]) stack.push(v); // walk back stack.push(src); while (!stack.isEmpty()) System.out.print(stack.pop() + " "); }
A stack turns the vertices collected backward into the right order.
Dijkstra · Step through the code
Path Reconstruction: Tracing printPath(prev, dist, 5, 1)
void printPath(int[] prev, int[] dist, int src, int target) { if (dist[target] == INF) { // unreachable System.out.print("no path"); return; } Deque<Integer> stack = new ArrayDeque<>(); for (int v = target; v != src; v = prev[v]) stack.push(v); // walk back stack.push(src); while (!stack.isEmpty()) System.out.print(stack.pop() + " "); }
From Dijkstra Example 2 (source 5, target 1). d[1] = 6 ≠ INF: reachable. Start with an empty stack.
1 ≠ source → push it. Next, v = prev[1] = 2.
2 ≠ source → push it. Next, v = prev[2] = 4.
4 ≠ source → push it. Next, v = prev[4] = 5.
v = 5 = source: the loop ends. 5 is not on the stack yet.
Push the source 5: on top, with the target at the bottom.
Pop and print 5 (LIFO: last in, first out).
Pop and print 4 (LIFO: last in, first out).
Pop and print 2 (LIFO: last in, first out).
Pop and print 1. Path 5 → 4 → 2 → 1, length 2 + 1 + 3 = 6.
Dijkstra
Dijkstra: Running Time
The cost depends on how we select the vertex with the smallest distance:
| Selection structure | Select | Relax | Total | Best for |
|---|---|---|---|---|
| Array (linear scan) | V × O(V) | E × O(1) | O(V²) | dense (E ≈ V²) |
| Binary heap (priority queue) | O(log V) | O(log V) | O((V + E) log V) | sparse (E ≪ V²) |
| Fibonacci heap | O(log V) amortized | O(1) amortized | O(E + V log V) | theoretical best |
Dijkstra
Why Does Dijkstra Fail on Negative Edges?
- The correctness proof relies on this: once u is settled, any path leaving S has length at least d[u], because edges are nonnegative and a path can only grow.
- A negative edge breaks that assumption: a vertex processed later can open a shorter path to an already settled vertex.
- Adding a constant to every weight is no fix: paths with more edges are penalized more, so the shortest path changes.
- With negative edges, use Bellman-Ford.
A is settled; relaxation gives d[B] = 2, d[C] = 3.
Smallest d: B (2). Dijkstra settles B, assuming "no shorter path to B can exist".
C is processed: d[C] + (−2) = 1 < 2, but B is already in S. The result d[B] = 2 is wrong; the true δ(A, B) = 1 (A → C → B).
Bellman-Ford
The Bellman-Ford Algorithm
- Finds shortest paths from a source vertex to every other vertex (single-source).
- Named after Richard Bellman (1958) and Lester Ford Jr. (1956); E. F. Moore gave the same method in 1959.
- Handles negative edge weights.
- Detects negative cycles reachable from the source.
- Slower than Dijkstra: O(V · E).
Bellman-Ford
Bellman-Ford: The Steps
Step 1: Set the start vertex's distance to 0 and every other vertex's to ∞.
π[v] = undefined.
Step 2: Examine the edges one by one and update distances by relaxation; repeat this pass V − 1 times.
The edge order is fixed within a pass; it affects how many passes are needed, not the result.
Step 3: Once a vertex's distance improves, its neighbors' distances may improve in later checks.
Each pass pushes the improvement at least one edge further along the path.
Step 4: To check for negative cycles, examine every edge once more; if some edge still relaxes, a negative cycle is reachable from the source.
Without such a cycle no edge relaxes after V − 1 passes.
Bellman-Ford
Why V − 1 Passes? Complexity
- Without negative cycles every shortest path has at most V − 1 edges.
- Invariant: after pass i, d[v] = δ(s, v) for every v that has a shortest path with at most i edges.
- Pass i is sure to relax the path's i-th edge; V − 1 passes complete every path.
- Each pass examines E edges: O(V · E) time in total, O(V) extra memory.
| Graph | Time |
|---|---|
| Sparse, E ≈ V | O(V²) |
| Dense, E ≈ V² | O(V³) |
| Best case (early exit) | O(E) |
Bellman-Ford
Bellman-Ford Example 1: Relaxing Edge by Edge
Initialization: d[s] = 0, all others ∞. Each pass examines the edges in the order shown above.
Pass 1, edge (s, v₁): 0 + 9 = 9 < ∞ → d[v₁] = 9.
Pass 1, edge (s, v₂): 0 + 3 = 3 < ∞ → d[v₂] = 3.
Pass 1, edge (v₁, v₂): 9 + 6 = 15 ≥ 3 → no change.
Pass 1, edge (v₁, v₄): 9 + 2 = 11 < ∞ → d[v₄] = 11.
Pass 1, edge (v₂, v₁): 3 + 2 = 5 < 9 → d[v₁] = 5.
Pass 1, edge (v₂, v₃): 3 + 1 = 4 < ∞ → d[v₃] = 4.
Pass 1, edge (v₃, v₂): 4 + 2 = 6 ≥ 3 → no change.
Pass 1, edge (v₃, v₄): 4 + 2 = 6 < 11 → d[v₄] = 6.
Pass 2: none of the eight edges relaxes → early exit. Result: v₁ = 5, v₂ = 3, v₃ = 4, v₄ = 6 (with this order one pass was enough).
Bellman-Ford
Bellman-Ford Example 2: Negative Edges
A graph with negative edges, source S. Each pass examines the edges in the order shown above (back to front: the worst order).
Pass 1: only (S,A): ∞ → 3. The other edges still start at ∞.
Pass 2: only (A,B): ∞ → 2.
Pass 3: only (B,C): ∞ → 0.
Pass 4: only (C,D): ∞ → -2; (C,E): ∞ → 1.
Pass 5: only (E,D): -2 → -3. Going through E gives D a shorter path: 1 + (−4) = −3.
Check pass: no edge relaxes → no negative cycle (A→B→C→E→A = −1 −2 + 1 + 4 = +2). All V − 1 = 5 passes were needed.
Bellman-Ford
Bellman-Ford Example 3: The Graph from Dijkstra Example 2
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| d | ∞ | ∞ | ∞ | ∞ | 0 |
| π | / | / | / | / | / |
Initialization: source 5 has distance 0, all others ∞. Edge order: (1,3), (1,4), (2,1), (3,4), (4,2), (4,3), (5,2), (5,4).
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| d | ∞ | 4 | ∞ | 2 | 0 |
| π | / | 5 | / | 5 | / |
Pass 1: (5,2): ∞ → 4; (5,4): ∞ → 2.
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| d | 7 | 3 | 3 | 2 | 0 |
| π | 2 | 4 | 4 | 5 | / |
Pass 2: (2,1): ∞ → 7; (4,2): 4 → 3; (4,3): ∞ → 3. Edge (4,2) found a shorter path to 2.
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| d | 6 | 3 | 3 | 2 | 0 |
| π | 2 | 4 | 4 | 5 | / |
Pass 3: (2,1): 7 → 6. Since 2 improved in the previous pass, (2,1) relaxes again.
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| d | 6 | 3 | 3 | 2 | 0 |
| π | 2 | 4 | 4 | 5 | / |
Pass 4: no shorter path found (no edge relaxes).
Bellman-Ford
Checking for Negative Cycles (Pass V)
Try relaxing every edge one last time: if some edge still has d[v] > d[u] + w(u, v), a negative cycle is reachable from the source. For the result of Example 3:
| edge | d[v] | d[u] + w(u, v) | result |
|---|---|---|---|
| (1, 3) | d[3] = 3 | d[1] + w = 6 + 6 = 12 | 3 ≯ 12 ✓ |
| (1, 4) | d[4] = 2 | d[1] + w = 6 + 3 = 9 | 2 ≯ 9 ✓ |
| (2, 1) | d[1] = 6 | d[2] + w = 3 + 3 = 6 | 6 ≯ 6 ✓ |
| (3, 4) | d[4] = 2 | d[3] + w = 3 + 2 = 5 | 2 ≯ 5 ✓ |
| (4, 2) | d[2] = 3 | d[4] + w = 2 + 1 = 3 | 3 ≯ 3 ✓ |
| (4, 3) | d[3] = 3 | d[4] + w = 2 + 1 = 3 | 3 ≯ 3 ✓ |
| (5, 2) | d[2] = 3 | d[5] + w = 0 + 4 = 4 | 3 ≯ 4 ✓ |
| (5, 4) | d[4] = 2 | d[5] + w = 0 + 2 = 2 | 2 ≯ 2 ✓ |
Bellman-Ford
Detecting a Negative Cycle: Example
Cycle A → B → C → A: 1 + (−3) + 1 = −1. Edge order: (S,A), (A,B), (B,C), (C,A).
| pass | S | A | B | C |
|---|---|---|---|---|
| 1 | 0 | 1 | 3 | 0 |
| 2 | 0 | 0 | 2 | -1 |
| 3 | 0 | -1 | 1 | -2 |
- After V − 1 = 3 passes d = [0, −1, 1, −2]; the values on the cycle drop by 1 every pass.
- Check: for (A, B), d[A] + 1 = 0 < d[B] = 1 → a negative cycle exists.
- Vertices on the cycle, and those reachable from it, have shortest-path length −∞ (undefined).
Bellman-Ford
Bellman-Ford: Pseudocode
BELLMAN_FORD(G, source): dist = [ ] for each v in V: dist[v] = infinity dist[source] = 0 repeat for i = 1 to |V| - 1: for each (u, v, weight) in E: if dist[u] + weight < dist[v]: dist[v] = dist[u] + weight for each (u, v, weight) in E: if dist[u] + weight < dist[v]: error "Graph contains a negative-weight cycle" return dist
Bellman-Ford
Bellman-Ford: Step Through the Code
void bellmanFord(int src) { int[] dist = new int[V]; int[] prev = new int[V]; Arrays.fill(dist, INF); dist[src] = 0; for (int i = 1; i < V; i++) { for (Edge e : edges) { int u = e.from, v = e.to, w = e.weight; if (dist[u] != INF && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; prev[v] = u; } } } for (Edge e : edges) { // check int u = e.from, v = e.to, w = e.weight; if (dist[u] != INF && dist[u] + w < dist[v]) throw new IllegalStateException( "Negative cycle"); } }
Source 0. dist = [0, ∞, ∞, ∞]. Edge list: (2,1,−2), (0,1,4), (0,2,5), (1,3,3).
Pass 1, edge (2,1): dist[2] = ∞ → condition false, skip.
Pass 1, edge (0,1): 0 + (4) = 4 < ∞ → condition true.
dist[1] = 4, prev[1] = 0.
Pass 1, edge (0,2): 0 + (5) = 5 < ∞ → condition true.
dist[2] = 5, prev[2] = 0.
Pass 1, edge (1,3): 4 + (3) = 7 < ∞ → condition true.
dist[3] = 7, prev[3] = 1. Pass 1 is over: edge (2,1) was wasted because it was examined while 2 was still ∞.
Pass 2, edge (2,1): 5 + (-2) = 3 < 4 → condition true.
dist[1] = 3, prev[1] = 2.
Pass 2, edge (0,1): 0 + (4) = 4 ≥ 3 → no change.
Pass 2, edge (0,2): 0 + (5) = 5 ≥ 5 → no change.
Pass 2, edge (1,3): 3 + (3) = 6 < 7 → condition true.
dist[3] = 6, prev[3] = 1. Pass 2 is over: the negative edge brought 1 down to 3, and that brought 3 down to 6.
Pass 3, edge (2,1): 5 + (-2) = 3 ≥ 3 → no change.
Pass 3, edge (0,1): 0 + (4) = 4 ≥ 3 → no change.
Pass 3, edge (0,2): 0 + (5) = 5 ≥ 5 → no change.
Pass 3, edge (1,3): 3 + (3) = 6 ≥ 6 → no change.
Pass 3 changed nothing. Check pass (lines 16–20): no edge relaxes and no exception is thrown. Result [0, 3, 5, 6].
Bellman-Ford · Step through the code
Bellman-Ford: Tracing the Negative-Cycle Check
for (int i = 1; i < V; i++) { for (Edge e : edges) { int u = e.from, v = e.to, w = e.weight; if (dist[u] != INF && dist[u] + w < dist[v]) { dist[v] = dist[u] + w; prev[v] = u; } } } for (Edge e : edges) { // check int u = e.from, v = e.to, w = e.weight; if (dist[u] != INF && dist[u] + w < dist[v]) throw new IllegalStateException( "Negative cycle"); }
S → A (2) and the cycle A → B → C → A: 1 + (−3) + 1 = −1. dist = [S, A, B, C] = [0, ∞, ∞, ∞]; the edges SA, AB, BC, CA are relaxed in that order.
Pass 1, edge SA: 0 + (2) = 2 is smaller → dist[A] = 2.
Pass 1, edge AB: 2 + (1) = 3 is smaller → dist[B] = 3.
Pass 1, edge BC: 3 + (-3) = 0 is smaller → dist[C] = 0.
Pass 1, edge CA: 0 + (1) = 1 is smaller → dist[A] = 1. Going around the cycle brought A down from 2 to 1.
Pass 2 is over: three of the four edges relaxed again (B = 2, C = -1, A = 0). The values on the cycle drop by 1 every pass.
Pass 3 is over: three of the four edges relaxed again (B = 1, C = -2, A = -1). The values on the cycle drop by 1 every pass.
V − 1 = 3 passes are over; the check pass begins. Edge SA: 0 + 2 = 2 is not < -1, fine.
Check, edge AB: -1 + 1 = 0 < 1 → it still relaxes after V − 1 passes: a negative cycle exists, and the exception is thrown.
Floyd-Warshall
The Floyd-Warshall Algorithm
- Finds shortest paths between all pairs of vertices.
- Found independently by Bernard Roy (1959), Stephen Warshall (1962, transitive closure) and Robert Floyd (1962, shortest paths).
- Works with negative edge weights and detects negative cycles (in which case shortest paths are undefined).
- Uses dynamic programming, keeping the results in a V × V matrix D.
Floyd-Warshall
Floyd-Warshall: Steps and Recurrence
- Step 1: Fill D with the edge weights: D[i][i] = 0, and ∞ where there is no direct edge.
- Step 2: Take intermediate vertices k = 1, 2, …, n in turn; for every pair (i, j)…
- Step 3: Compute the length of the path through k, D[i][k] + D[k][j], and compare it with the current D[i][j].
- Step 4: If it is shorter, write the new value into the matrix (and update Π as well).
Either k is not on the path, or the path passes through k once. D(n) is the answer.
Floyd-Warshall
Floyd-Warshall: Complexity
- Time Θ(V³): three nested for loops, each running V times.
- the first (outermost) loop runs over all intermediate vertices (k),
- the second over all source vertices (i),
- the last over all target vertices (j).
- Memory Θ(V²): D can be updated in place; row and column k do not change during pass k.
| For all pairs | Time |
|---|---|
| Floyd-Warshall | O(V³) |
| V × Dijkstra (array) | O(V³) |
| V × Dijkstra (heap) | O(V(V + E) log V) |
| V × Bellman-Ford | O(V² E) |
Floyd-Warshall
Floyd-Warshall Example: The D and Π Matrices
D(0)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0 | ∞ | 6 | 3 | ∞ |
| 2 | 3 | 0 | ∞ | ∞ | ∞ |
| 3 | ∞ | ∞ | 0 | 2 | ∞ |
| 4 | ∞ | 1 | 1 | 0 | ∞ |
| 5 | ∞ | 4 | ∞ | 2 | 0 |
Π(0)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | / | / | 1 | 1 | / |
| 2 | 2 | / | / | / | / |
| 3 | / | / | / | 3 | / |
| 4 | / | 4 | 4 | / | / |
| 5 | / | 5 | / | 5 | / |
k = 0 (initialization): fill D with edge weights, 0 on the diagonal, ∞ where there is no edge. Π[i][j] = i if there is a direct edge, otherwise /.
D(1)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0 | ∞ | 6 | 3 | ∞ |
| 2 | 3 | 0 | 9 | 6 | ∞ |
| 3 | ∞ | ∞ | 0 | 2 | ∞ |
| 4 | ∞ | 1 | 1 | 0 | ∞ |
| 5 | ∞ | 4 | ∞ | 2 | 0 |
Π(1)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | / | / | 1 | 1 | / |
| 2 | 2 | / | 1 | 1 | / |
| 3 | / | / | / | 3 | / |
| 4 | / | 4 | 4 | / | / |
| 5 | / | 5 | / | 5 | / |
k = 1: via vertex 1 we find the paths 2 ⇝ 3 (3 + 6 = 9) and 2 ⇝ 4 (3 + 3 = 6).
D(2)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0 | ∞ | 6 | 3 | ∞ |
| 2 | 3 | 0 | 9 | 6 | ∞ |
| 3 | ∞ | ∞ | 0 | 2 | ∞ |
| 4 | 4 | 1 | 1 | 0 | ∞ |
| 5 | 7 | 4 | 13 | 2 | 0 |
Π(2)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | / | / | 1 | 1 | / |
| 2 | 2 | / | 1 | 1 | / |
| 3 | / | / | / | 3 | / |
| 4 | 2 | 4 | 4 | / | / |
| 5 | 2 | 5 | 1 | 5 | / |
k = 2: via vertex 2 we find 4 ⇝ 1 (1 + 3 = 4), 5 ⇝ 1 (4 + 3 = 7) and 5 ⇝ 3 (4 + 9 = 13).
D(3)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0 | ∞ | 6 | 3 | ∞ |
| 2 | 3 | 0 | 9 | 6 | ∞ |
| 3 | ∞ | ∞ | 0 | 2 | ∞ |
| 4 | 4 | 1 | 1 | 0 | ∞ |
| 5 | 7 | 4 | 13 | 2 | 0 |
Π(3)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | / | / | 1 | 1 | / |
| 2 | 2 | / | 1 | 1 | / |
| 3 | / | / | / | 3 | / |
| 4 | 2 | 4 | 4 | / | / |
| 5 | 2 | 5 | 1 | 5 | / |
k = 3: no shorter path via vertex 3 (3 has an edge only to 4, and no value improves).
D(4)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0 | 4 | 4 | 3 | ∞ |
| 2 | 3 | 0 | 7 | 6 | ∞ |
| 3 | 6 | 3 | 0 | 2 | ∞ |
| 4 | 4 | 1 | 1 | 0 | ∞ |
| 5 | 6 | 3 | 3 | 2 | 0 |
Π(4)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | / | 4 | 4 | 1 | / |
| 2 | 2 | / | 4 | 1 | / |
| 3 | 2 | 4 | / | 3 | / |
| 4 | 2 | 4 | 4 | / | / |
| 5 | 2 | 4 | 4 | 5 | / |
k = 4: via vertex 4, the paths 1⇝2, 1⇝3, 2⇝3, 3⇝1, 3⇝2, 5⇝1, 5⇝2, 5⇝3 get shorter (8 cells). E.g. 5⇝3: 2 + 1 = 3 < 13.
D(5)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0 | 4 | 4 | 3 | ∞ |
| 2 | 3 | 0 | 7 | 6 | ∞ |
| 3 | 6 | 3 | 0 | 2 | ∞ |
| 4 | 4 | 1 | 1 | 0 | ∞ |
| 5 | 6 | 3 | 3 | 2 | 0 |
Π(5)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | / | 4 | 4 | 1 | / |
| 2 | 2 | / | 4 | 1 | / |
| 3 | 2 | 4 | / | 3 | / |
| 4 | 2 | 4 | 4 | / | / |
| 5 | 2 | 4 | 4 | 5 | / |
k = 5: no shorter path via vertex 5 (no edge enters 5, so D[i][5] = ∞). The final matrices are ready.
Floyd-Warshall
Final State and Reading a Path
D (distances)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | 0 | 4 | 4 | 3 | ∞ |
| 2 | 3 | 0 | 7 | 6 | ∞ |
| 3 | 6 | 3 | 0 | 2 | ∞ |
| 4 | 4 | 1 | 1 | 0 | ∞ |
| 5 | 6 | 3 | 3 | 2 | 0 |
Π (predecessors)
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | / | 4 | 4 | 1 | / |
| 2 | 2 | / | 4 | 1 | / |
| 3 | 2 | 4 | / | 3 | / |
| 4 | 2 | 4 | 4 | / | / |
| 5 | 2 | 4 | 4 | 5 | / |
- Π[i][j]: the vertex just before j on a shortest path from i to j.
- 5 ⇝ 1: Π[5][1] = 2, Π[5][2] = 4, Π[5][4] = 5 → 5 → 4 → 2 → 1, D[5][1] = 6.
- 1 ⇝ 2: Π[1][2] = 4, Π[1][4] = 1 → 1 → 4 → 2, D[1][2] = 4.
- Column 5 is ∞: no vertex can reach 5.
- Row 5 matches the [6, 3, 3, 2, 0] that Dijkstra and Bellman-Ford found for source 5.
Floyd-Warshall
Floyd-Warshall: Pseudocode
1. Initialization
FLOYD_WARSHALL(G): dist = [ ][ ] for each u in V: for each v in V: if u == v: dist[u][v] = 0 else if (u, v) in E: dist[u][v] = weight(u, v) else: dist[u][v] = infinity
2. Three nested loops and the check
for each k in V: for each i in V: for each j in V: if dist[i][k] + dist[k][j] < dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j] for each i in V: if dist[i][i] < 0: error "Graph contains a negative-weight cycle" return dist
Floyd-Warshall
Floyd-Warshall: Java
void floydWarshall(int[][] d) { // V x V for (int k = 0; k < V; k++) // via for (int i = 0; i < V; i++) // from for (int j = 0; j < V; j++) // to if (d[i][k] != INF && d[k][j] != INF && d[i][k] + d[k][j] < d[i][j]) { d[i][j] = d[i][k] + d[k][j]; prev[i][j] = prev[k][j]; } for (int i = 0; i < V; i++) if (d[i][i] < 0) throw new IllegalStateException( "Negative cycle"); }
- The matrix is updated in place; no separate copy of D(k) is needed.
- The INF check prevents overflow when adding to
Integer.MAX_VALUE. prev[i][j] = prev[k][j]: on the new path, the vertex before j is the same as on the k ⇝ j path.- Negative cycle: some vertex has a negative distance to itself (D[i][i] < 0).
Floyd-Warshall · Step through the code
Floyd-Warshall: Step Through the Code
void floydWarshall(int[][] d) { // V x V for (int k = 0; k < V; k++) // via for (int i = 0; i < V; i++) // from for (int j = 0; j < V; j++) // to if (d[i][k] != INF && d[k][j] != INF && d[i][k] + d[k][j] < d[i][j]) { d[i][j] = d[i][k] + d[k][j]; prev[i][j] = prev[k][j]; } for (int i = 0; i < V; i++) if (d[i][i] < 0) throw new IllegalStateException( "Negative cycle"); }
Start: d holds the edge weights (0 on the diagonal, ∞ where there is no edge); prev[i][j] = i. The matrix on the right is d, the one below it prev.
k = 0 as intermediate: d[1][0] + d[0][3] = 8 + 7 = 15 < ∞ → d[1][3] = 15, prev[1][3] = prev[0][3] = 0.
k = 0 as intermediate: d[2][0] + d[0][1] = 5 + 3 = 8 < ∞ → d[2][1] = 8, prev[2][1] = prev[0][1] = 0.
k = 0 as intermediate: d[3][0] + d[0][1] = 2 + 3 = 5 < ∞ → d[3][1] = 5, prev[3][1] = prev[0][1] = 0.
k = 1 as intermediate: d[0][1] + d[1][2] = 3 + 2 = 5 < ∞ → d[0][2] = 5, prev[0][2] = prev[1][2] = 1.
k = 1 as intermediate: d[3][1] + d[1][2] = 5 + 2 = 7 < ∞ → d[3][2] = 7, prev[3][2] = prev[1][2] = 1.
k = 2 as intermediate: d[0][2] + d[2][3] = 5 + 1 = 6 < 7 → d[0][3] = 6, prev[0][3] = prev[2][3] = 2.
k = 2 as intermediate: d[1][2] + d[2][0] = 2 + 5 = 7 < 8 → d[1][0] = 7, prev[1][0] = prev[2][0] = 2.
k = 2 as intermediate: d[1][2] + d[2][3] = 2 + 1 = 3 < 15 → d[1][3] = 3, prev[1][3] = prev[2][3] = 2.
k = 3 as intermediate: d[1][3] + d[3][0] = 3 + 2 = 5 < 7 → d[1][0] = 5, prev[1][0] = prev[3][0] = 3.
k = 3 as intermediate: d[2][3] + d[3][0] = 1 + 2 = 3 < 5 → d[2][0] = 3, prev[2][0] = prev[3][0] = 3.
k = 3 as intermediate: d[2][3] + d[3][1] = 1 + 5 = 6 < 8 → d[2][1] = 6, prev[2][1] = prev[3][1] = 0.
All four passes are done. No negative value on the diagonal → no negative cycle. Sample path: prev[1][3] = 2, prev[1][2] = 1 → 1 → 2 → 3, d[1][3] = 3.
A* Search
The A* (A-Star) Search Algorithm
- A search algorithm that finds a shortest path between two points (start → goal).
- Developed in 1968 by Peter Hart, Nils Nilsson and Bertram Raphael.
- Combines Dijkstra's actual cost (uniform-cost search) with the goal estimate of greedy best-first search.
- It needs a good estimate function (a heuristic) to work well.
- Time and memory grow quickly as the number of vertices increases.
A* Search
How Does A* Work? f(n) = g(n) + h(n)
| Selection rule | Algorithm | Behavior |
|---|---|---|
| g(n) only | Dijkstra | spreads equally in all directions; correct but expands many vertices |
| h(n) only | Greedy best-first | rushes toward the goal; fast, but no shortest-path guarantee |
| g(n) + h(n) | A* | heads toward the goal; with a suitable h, guarantees a shortest path |
A* Search
A*: The Steps
1. Set g(start) = 0 and add it to the open list (a priority queue). All other vertices get g = ∞.
Open list: discovered but not yet expanded vertices.
2. Remove the vertex with the smallest f = g + h from the open list; put it on the closed list.
The rule is the sum f = g + h, not g or h alone.
3. If it is the goal, stop: rebuild the path from the predecessors.
The goal is final when removed, not when added.
4. Otherwise, for each neighbor v let g' = g(u) + w(u, v); if g' is smaller, update g, f and the predecessor and add v to the open list.
Dijkstra's relaxation. A closed v with a better g is reopened (never with a consistent h).
5. Go back to step 2. If the open list empties, there is no path.
The choice is from the whole open list, not just the last vertex's neighbors.
A* Search
Properties of a Heuristic
A* Search
A*: Running Time
- Worst-case time and memory in terms of the search tree O(bd):
- b: branching factor (average number of neighbors), d: depth of the goal (number of steps in the solution).
- On a finite graph with a consistent h, A* does at most O((V + E) log V) work, like Dijkstra.
- The better the heuristic, the fewer vertices are expanded: with a perfect estimate (h = h*) only the d vertices on the path are expanded.
- Its weak point is memory: the open and closed lists grow; variants such as IDA* and SMA* bound the memory.
A* Search
A* Example: s to t (h values next to the vertices)
Open list
| vertex | g | h | f |
|---|---|---|---|
| a | 1.5 | 4 | 5.5 |
| d | 2 | 4.5 | 6.5 |
Closed: s
Expand s: neighbors a (g = 1.5) and d (g = 2) are opened. f(a) = 1.5 + 4 = 5.5, f(d) = 2 + 4.5 = 6.5.
Open list
| vertex | g | h | f |
|---|---|---|---|
| b | 3.5 | 2 | 5.5 |
| d | 2 | 4.5 | 6.5 |
Closed: s, a
Smallest f: a (5.5). Expand a → b: g = 3.5, f(b) = 3.5 + 2 = 5.5.
Open list
| vertex | g | h | f |
|---|---|---|---|
| d | 2 | 4.5 | 6.5 |
| c | 6.5 | 4 | 10.5 |
Closed: s, a, b
Expand b (5.5 < 6.5) → c: g = 6.5, f(c) = 6.5 + 4 = 10.5. This path is getting expensive.
Open list
| vertex | g | h | f |
|---|---|---|---|
| e | 5 | 2 | 7 |
| c | 6.5 | 4 | 10.5 |
Closed: s, a, b, d
Now the smallest f is d (6.5). The search switches branches → e: g = 5, f(e) = 5 + 2 = 7.
Open list
| vertex | g | h | f |
|---|---|---|---|
| t | 7 | 0 | 7 |
| c | 6.5 | 4 | 10.5 |
Closed: s, a, b, d, e
Expand e → t: g = 5 + 2 = 7, f(t) = 7 + 0 = 7 < f(c) = 10.5.
Open list
| vertex | g | h | f |
|---|---|---|---|
| c | 6.5 | 4 | 10.5 |
Closed: s, a, b, d, e, t
t is removed from the open list: the goal. Shortest path s → d → e → t, cost 7. c was never expanded.
A* Search · Step through the code
A*: Step Through the Code
double aStar(int s, int t) { Arrays.fill(g, INF); g[s] = 0; Queue<Integer> open = new PriorityQueue<>( Comparator.comparingDouble(v -> g[v] + h[v])); open.add(s); while (!open.isEmpty()) { int u = open.poll(); // smallest f if (u == t) return g[t]; // goal for (Edge e : adj[u]) if (g[u] + e.w < g[e.v]) { g[e.v] = g[u] + e.w; parent[e.v] = u; open.remove(e.v); open.add(e.v); } } return INF; }
Undirected graph, goal t; h beside each vertex estimates the distance to t. g(s) = 0; open = {s} (f in parentheses).
Remove s, the smallest f (0 + 5 = 5). Not the goal; examine its neighbors.
g(a) = 0 + 1.5 = 1.5, f = 5.5; g(d) = 0 + 2 = 2, f = 6.5.
Remove a, the smallest f (1.5 + 4 = 5.5). Not the goal; examine its neighbors.
g(b) = 1.5 + 2 = 3.5, f = 5.5.
Remove b, the smallest f (3.5 + 2 = 5.5). Not the goal; examine its neighbors.
g(c) = 3.5 + 3 = 6.5, f = 10.5. Too large: d (f = 6.5) is next.
Remove d, the smallest f (2 + 4.5 = 6.5). Not the goal; examine its neighbors.
g(e) = 2 + 3 = 5, f = 7.
Remove e, the smallest f (5 + 2 = 7). Not the goal; examine its neighbors.
g(t) = 5 + 2 = 7, f = 7.
Smallest f: t (7), the goal → return g[t] = 7. Path s → d → e → t; c never expanded.
A* Search
A* Example: Around a Wall on a Grid
Expand A: 8 neighbors are opened. A straight step costs 10, a diagonal 14; h = octile distance (walls ignored). Smallest f = 42.
Expansion 2: the cell with the smallest f = 42 (orange frame) moves to the closed list; its neighbors are opened or updated.
Expansion 4: the cell with the smallest f = 48 (orange frame) moves to the closed list; its neighbors are opened or updated.
Expansion 7: the cell with the smallest f = 54 (orange frame) moves to the closed list; its neighbors are opened or updated. Cells below the wall, with f = 42…62, are tried first.
Expansion 9: the cell with the smallest f = 62 (orange frame) moves to the closed list; its neighbors are opened or updated.
Expansion 13: the cell with the smallest f = 68 (orange frame) moves to the closed list; its neighbors are opened or updated. The lower route is blocked: f has risen to 68, and the search spreads to both sides of the wall.
Expansion 17 (f = 68): the path around the right of the wall has reached B; B is in the open list with f = 68.
B is removed: the path goes up from A → diagonal up-right → diagonal up-left → three steps left over the wall, cost 68 (2 diagonals × 14 + 4 straight × 10). 18 cells were expanded.
Related Methods
BFS: Shortest Paths in an Unweighted Graph
- When all edges are equal (weight 1), the shortest path = the path with the fewest edges.
- Breadth-first search (BFS) visits vertices level by level, in order of distance from the source: d[v] = d[u] + 1.
- A FIFO queue replaces Dijkstra's priority queue: O(V + E).
- Example (graph at right): the shortest path 0 → 6 is 0 → 2 → 4 → 6 (3 edges).
- Bidirectional search: run BFS from both the source and the target and stop when the two waves meet; about 2·bd/2 vertices are expanded.
Numbers: BFS level (edge count). Green: the BFS tree.
Comparison
Comparing Shortest-Path Algorithms
| Algorithm | Problem | Negative edges | Negative cycle | Time | Approach |
|---|---|---|---|---|---|
| BFS | single source, unweighted | — | — | O(V + E) | level by level |
| Dijkstra | single source | no | no | O(V²) / O((V + E) log V) | greedy |
| Bellman-Ford | single source | yes | detects | O(V · E) | repeated relaxation (DP) |
| Floyd-Warshall | all pairs | yes | detects | Θ(V³) | dynamic programming |
| A* | single pair | no | no | worst case O(bd) | heuristic + greedy |
Summary
Summary
| Topic | Key points |
|---|---|
| Relaxation | if d[u] + w < d[v], update d[v] and π[v]; the operation all the algorithms share |
| Dijkstra | settle the vertex with the smallest d, relax its neighbors; weights ≥ 0; O((V + E) log V) |
| Bellman-Ford | relax every edge for V − 1 passes; an edge that relaxes in pass V = a negative cycle reachable from the source; O(V · E) |
| Floyd-Warshall | D[i][j] = min(D[i][j], D[i][k] + D[k][j]), k outermost; D[i][i] < 0 → negative cycle; Θ(V³) |
| A* | f = g + h; shortest path with an admissible h; h = 0 → Dijkstra |
| BFS | fewest-edge path in an unweighted graph; O(V + E) |
| Paths | follow the π (Π) pointers back from the target; shortest-path tree |
Summary
Common Mistakes
Integer.MAX_VALUE + w wraps around to a negative number and looks "shorter". That is why d[u] != INF is checked first.Summary
Worked Problems
Directed graph: A→B 4, A→C 2, C→B 1, B→D 5, C→D 8, C→E 10, D→E 3; source A.
- In what order does Dijkstra settle the vertices? What are the final d values?
- Write the shortest path from A to E using the π array.
- If the edges are examined in alphabetical order (A→B, A→C, B→D, C→B, C→D, C→E, D→E), how many relaxations happen in each pass of Bellman-Ford?
- What is the final Floyd-Warshall D matrix for the graph 1→2 (4), 2→3 (1), 1→3 (7), 3→1 (−2)? Is there a negative cycle?
- V = 1000, E = 5000, and there are negative edges. Which algorithm, and roughly how many operations?
Show answers
- A, C, B, D, E; d = A:0, B:3, C:2, D:8, E:11 (B improves via C, 2 + 1).
- π[E] = D, π[D] = B, π[B] = C, π[C] = A → A → C → B → D → E = 2 + 1 + 5 + 3 = 11.
- Pass 1: 5 (B=4, C=2, D=9, B=3, E=12); pass 2: 2 (D=8, E=11); passes 3–4: 0 → early exit is possible.
- D = [[0, 4, 5], [−1, 0, 1], [−2, 2, 0]]; the diagonal is 0 → the cycles 1→2→3→1 = +3 and 1→3→1 = +5, no negative cycle.
- Bellman-Ford: V · E = 1000 · 5000 = 5 · 10⁶ relaxation attempts (Dijkstra is unreliable with negative edges).
Summary
Exercises
- Find all distances in the graph of Dijkstra Example 1 with source 5.
dijkstra(src, target): write a version that stops when the target leaves the queue.- Extend Dijkstra to also count the number of shortest paths to each vertex.
- Add early exit to Bellman-Ford; what is the best-case running time?
- Adapt Floyd-Warshall to compute the transitive closure (Warshall) with a boolean matrix.
- Why is Manhattan distance admissible on a 4-direction grid but not on an 8-direction grid?
Answers / hints
- d = 1:20, 2:21, 3:11, 4:6, 5:0, 6:9 (e.g. 3: 5→6→3 = 9 + 2; 2: 5→4→2 = 6 + 15).
- Before line 13,
if (u == target) break;— when a vertex is added its value is not final yet. count[src] = 1; if d[u] + w < d[v],count[v] = count[u]; if equal,count[v] += count[u].- If the
changedflag stays false, leave the loop; best case O(E). t[i][j] = t[i][j] || (t[i][k] && t[k][j]); again k outermost, Θ(V³).- Each step lowers |Δx| + |Δy| by at most 1 → h ≤ h*. A diagonal step lowers it by 2 but costs 1 (or 1.4) → an overestimate.
Summary
References
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed., 2022 (Chapters 22–23).
- Sedgewick and Wayne, Algorithms, 4th ed., 2011 (Section 4.4: Shortest Paths).
- E. W. Dijkstra, "A Note on Two Problems in Connexion with Graphs", Numerische Mathematik 1, 1959.
- R. Bellman, "On a Routing Problem", Quarterly of Applied Mathematics 16, 1958.
- R. W. Floyd, "Algorithm 97: Shortest Path", Communications of the ACM 5(6), 1962; S. Warshall, "A Theorem on Boolean Matrices", Journal of the ACM 9(1), 1962.
- P. E. Hart, N. J. Nilsson and B. Raphael, "A Formal Basis for the Heuristic Determination of Minimum Cost Paths", IEEE Trans. SSC 4(2), 1968.