Foundations to Frontier
Tests
A. Mathematical Foundations › A11 Graph Theory and Discrete Math

Paths, Connectivity and Shortest Paths

Can you get from here to there, and what is the cheapest way? This topic covers walks, paths and cycles, connected components (with union-find), and the shortest-path family: BFS for unweighted graphs, Dijkstra for non-negative weights, Bellman–Ford for negative weights and negative-cycle detection, Floyd–Warshall for all pairs, and A* with admissible heuristics. You will prove why Dijkstra and A* are correct, measure their costs, and plan a rover traverse across rocks and a crater rim, where A* finds the optimal route while expanding a quarter of the cells Dijkstra does.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Finding your way

A map app finds the quickest route among millions of roads in a blink. It doesn’t try every route. It explores outward from you, always extending the cheapest partial route found so far, and if it’s clever it leans toward the destination. A rover crossing Mars, a robot in a warehouse and a packet crossing the internet all solve the same problem: the shortest path in a graph.

Rover path planning: who explores least?
A*: expanded 199 cells · path cost 42.09 (36 cells)
Rocks are the solid high-contrast cells; the orange patch is a costly crater rim. BFS counts steps only (4-connected) and ignores terrain. Dijkstra expands in every direction in order of cost. A* adds an optimistic estimate of the remaining distance (the octile heuristic), so it heads for the goal and finds the same optimal cost while expanding far fewer cells. Weighted A* inflates the heuristic: faster still, at most ε times the optimal cost.
Why engineers care

Planetary rovers plan routes around rocks and slopes; warehouse robots, drones and game characters use A*. Network routing (OSPF uses Dijkstra), logistics, and the planning layer of almost every autonomous system rely on shortest-path algorithms. Choosing the right one can mean milliseconds instead of minutes on board.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A shortest-path algorithm finds…
  2. 2. A* is faster than plain Dijkstra because it…
  3. 3. If two parts of a map aren't connected, the path cost is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Definitions

  • A walk is a sequence of adjacent vertices; a path repeats no vertex; a cycle returns to its start.
  • A graph is connected if every pair has a path.
  • Components are the maximal connected pieces. Find them with BFS/DFS or union-find (near-constant time per operation).

BFS explores in layers: all vertices at distance 1, then 2, … In unweighted graphs, it gives fewest-edge paths in O(n+m)O(n + m).

Dijkstra’s algorithm

For non-negative weights: keep tentative distances, repeatedly finalize the closest unfinished vertex, and relax its edges (d(v)←min⁡(d(v),d(u)+wuv)d(v)\leftarrow\min(d(v), d(u) + w_{uv})). With a binary heap it runs in O((n+m)log⁡n)O((n + m)\log n) (Dijkstra, 1959).

📝 Quick check (Level 1)
0/3 answered
  1. 1. BFS finds shortest paths when…
  2. 2. Dijkstra requires weights that are…
  3. 3. Relaxing edge (u, v) means…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

The shortest-path toolbox

Algorithm Handles Cost
BFS unit weights O(n + m)
Dijkstra weights ≥ 0 O((n + m) log n)
Bellman–Ford negative weights, detects negative cycles O(nm)
Floyd–Warshall all pairs O(n³)
A* weights ≥ 0 plus an admissible heuristic ≤ Dijkstra, often far less

Order the frontier by f(v)=g(v)+h(v)f(v) = g(v) + h(v), where g is the cost so far and h estimates the cost to the goal. If h is admissible (never overestimates), A* returns an optimal path. If h is also consistent (h(u)≤wuv+h(v)h(u)\le w_{uv} + h(v)), no vertex needs re-expansion. On grids with step cost ≥ 1, the Euclidean or octile distance is admissible. Weighted A* uses g+εhg + \varepsilon h: fewer expansions, and the cost is at most ε×\varepsilon\times optimal.

Negative weights

Negative weights arise with rewards, energy regeneration or log-probability costs. Dijkstra’s “finalize the closest” logic breaks. Bellman–Ford relaxes all edges n − 1 times. A further improvement after that reveals a negative cycle, where “shortest” is undefined.

🐍 Exercise 1: the shortest-path family
Implement BFS, Dijkstra (heap), Bellman–Ford with negative-cycle detection, Floyd–Warshall and union-find. On a random weighted graph, check that they agree and that union-find matches reachability, then detect a planted negative cycle.
📝 Quick check (Level 2)
0/3 answered
  1. 1. An admissible heuristic…
  2. 2. Weighted A* with ε = 1.5 returns a path costing at most…
  3. 3. Bellman–Ford detects a negative cycle if…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Correctness of Dijkstra

Invariant: when u is finalized, d(u) is the true distance. Suppose not. Take the true shortest path to u, and let y be its first unfinalized vertex, with predecessor x finalized. Then d(y)≤δ(x)+wxy=δ(y)≤δ(u)<d(u)d(y)\le\delta(x) + w_{xy} = \delta(y)\le\delta(u) < d(u) (non-negative weights). So y would have been chosen before u. Contradiction. ∎

A* optimality with an admissible h

When the goal t is popped with f(t)=g(t)f(t) = g(t), every frontier node v has f(v)≥g(t)f(v)\ge g(t). Some frontier node lies on an optimal path with f(v)=g∗(v)+h(v)≤g∗(v)+δ(v,t)=C∗f(v) = g^*(v) + h(v)\le g^*(v) + \delta(v, t) = C^*. So g(t)≤C∗g(t)\le C^*. ∎

Bellman–Ford

A shortest path without negative cycles has at most n − 1 edges. After round k, all shortest paths using ≤ k edges are correct (induction). ∎

Exercise 3.1: Dijkstra on 0→1 (4), 0→2 (1), 2→1 (2), 1→3 (1), 2→3 (5)

Finalize 0 (0), then 2 (1). Relaxing gives d(1) = 3 and d(3) = 6. Finalize 1 (3), and relaxing gives d(3) = 4. Finalize 3 (4).

📝 Quick check (Level 3)
0/3 answered
  1. 1. Dijkstra's proof relies on…
  2. 2. A shortest simple path has at most how many edges?
  3. 3. d(3) in the exercise:

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Planning a rover traverse

A 60 × 60 grid map has 18% rock cells (impassable) and a crater rim that costs up to 3× to cross, with 8-connected moves. All three planners use the same cost model:

Planner Path cost Cells expanded
Dijkstra 93.5 2921
A* (octile heuristic) 93.5 761 (4× fewer)
weighted A* (ε = 2) 105, 12% worse 70

The heuristic is admissible because every cell costs at least 1. Mars rover planners have used related graph-search methods: GESTALT on Spirit and Opportunity evaluates arcs on a terrain grid (Biesiadecki and Maimone, 2006), and Field D* replans efficiently as new terrain is seen (Ferguson and Stentz, 2006).

🐍 Exercise 2 (research): A* for rover path planning
Generate a terrain-cost grid with rocks and a costly crater rim. Plan with Dijkstra, A* (octile heuristic) and weighted A*, and compare path cost and node expansions.

Shortest paths in AI, robotics and aerospace

  • Planetary rovers: GESTALT and AutoNav on MER, MSL and Mars 2020; Field D* and incremental replanning (D* Lite).
  • Networks: OSPF link-state routing (Dijkstra); delay-tolerant networking for deep-space relays (contact graph routing).
  • Robot motion planning: graph search over lattices and PRM/RRT roadmaps; optimal graph search in configuration space.
  • Learning: neural heuristics for A*; differentiable shortest paths in structured prediction.

References

  1. E. Dijkstra (1959). “A note on two problems in connexion with graphs.” Numerische Mathematik, 1.
  2. P. Hart, N. Nilsson, B. Raphael (1968). “A formal basis for the heuristic determination of minimum cost paths.” IEEE Trans. Systems Science and Cybernetics, 4(2).
  3. R. Bellman (1958). “On a routing problem.” Quarterly of Applied Mathematics, 16.
  4. T. Cormen, C. Leiserson, R. Rivest, C. Stein (2022). Introduction to Algorithms, 4th ed. MIT Press. Ch. 22–23.
  5. D. Ferguson, A. Stentz (2006). “Using interpolation to improve path planning: the Field D* algorithm.” J. Field Robotics, 23(2).
  6. J. Biesiadecki, M. Maimone (2006). “The Mars Exploration Rover surface mobility flight software: driving ambition.” IEEE Aerospace Conference.
  7. S. Koenig, M. Likhachev (2002). “D* Lite.” AAAI.
  8. S. LaValle (2006). Planning Algorithms. Cambridge. Ch. 2.