Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Solutions · 9. Shortest paths on terrain: breadth-first search, Dijkstra and A*

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 9. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. Across every directed 4-neighbor and 8-neighbor edge (u→v)(u \to v) of make_topographic_terrain(56, 56, seed=42), verify heuristic consistency h(u)≤w(u→v)+h(v)h(u) \leq w(u \to v) + h(v) and h(goal)=0h(\mathrm{goal}) = 0, and verify that f(uk)=g(uk)+h(uk)f(u_k) = g(u_k) + h(u_k) is monotonically non-decreasing along admissible A∗A^*'s expansion order.


Exercise 1: Exhaustive edge-consistency and monotone f-contour verification
===========================================================================
4-neighbor consistency verified on 12,320 directed edges | min(w + h(v) - h(u)) = +0.000e+00
8-neighbor consistency verified on 24,420 directed edges | min(w + h(v) - h(u)) = +1.445e-04
Monotone non-decreasing f-sequence verified across all 1440 A* pops (min df = +2.503e-05)

Explanation. By the norm triangle inequality ∥u−g∥−∥v−g∥≤∥u−v∥=ℓ(u,v)\|\mathbf{u} - \mathbf{g}\| - \|\mathbf{v} - \mathbf{g}\| \leq \|\mathbf{u} - \mathbf{v}\| = \ell(u, v) and C(v)≥cmin⁡C(v) \geq c_{\min}, we have h(u)−h(v)≤cmin⁡ ℓ(u,v)≤ℓ(u,v) C(v)=w(u→v)h(u) - h(v) \leq c_{\min}\,\ell(u,v) \leq \ell(u,v)\,C(v) = w(u \to v). Consistency forces f(v)=g(u)+w(u→v)+h(v)≥g(u)+h(u)=f(u)f(v) = g(u) + w(u\to v) + h(v) \geq g(u) + h(u) = f(u), guaranteeing that no closed vertex can ever be reached later with a smaller gg-cost.


Exercise 2

Problem. Verify ValueError guards on grid_shortest_path_search, then demonstrate quantitatively (a) how replacing cmin⁡c_{\min} with cmeanc_{\mathrm{mean}} or cmax⁡c_{\max} in h(u)h(u) breaks admissibility and returns a suboptimal path, and (b) how pairing a Manhattan heuristic ∣Δr∣+∣Δc∣|\Delta r| + |\Delta c| with 8-neighbor diagonal moves returns a strictly suboptimal route.


Exercise 2: Failure-mode audit — inadmissible c_max scaling and Manhattan-on-diagonals bug
==========================================================================================
(a) Inadmissible c_max scaling : True Optimal = 107.35 vs. c_max Bug = 140.31 (+32.95 penalty)
(b) Manhattan on 8-neighbor    : True Optimal = 14.8284 vs. Manhattan-Heuristic Bug = 15.2244 (missed diagonal bypass)

Explanation. On 8-neighbor grids, moving diagonally by (±1,±1)(\pm 1, \pm 1) decreases Manhattan distance ∣Δr∣+∣Δc∣|\Delta r| + |\Delta c| by 2cmin⁡2 c_{\min} while costing as little as 2 cmin⁡≈1.414 cmin⁡\sqrt{2}\, c_{\min} \approx 1.414\, c_{\min}. That 41.4%41.4\% heuristic overestimate penalizes off-axis diagonal detours around mud/ridges, causing A∗A^* to pop the goal prematurely along a suboptimal axis-aligned route.


Exercise 3

Problem. Implement multi_waypoint_terrain_planner(cost_grid, depot, waypoints) combining pairwise 8-neighbor admissible A∗A^* shortest paths with exact Held–Karp subset DP (O(K22K)\mathcal{O}(K^2 2^K)) to route an autonomous rover from depot through K=6K = 6 geological targets and back to depot. Verify exact cost agreement against all 6!=7206! = 720 permutations and compare against greedy Nearest-Neighbor.


Exercise 3: Implement it yourself — multi-waypoint rover terrain tour (A* + Held-Karp DP)
=========================================================================================
Optimal Rover Tour Order  : [0, 4, 2, 6, 3, 5, 1, 0] (142 terrain cells)
Held-Karp Exact Tour Cost : 283.4500 (720-permutation oracle = 283.4500)
Greedy Nearest-Neighbor   : 318.5014 (Held-Karp saves 35.05 energy units)
<Figure size 770x517 with 1 Axes>

Figure S9.1. Rover tour from the depot (star) through six targets on the synthetic terrain. The Held–Karp order with A* legs costs 283.4.

Summary. The problem splits into two parts: pairwise A∗A^* searches reduce the 56×5656 \times 56 terrain to a 7×77 \times 7 table of exact energies between the depot and the targets, and Held–Karp dynamic programming orders the visits. The tour cost, 283.45, equals the best of all 720 permutations and is 35.05 units below the nearest-neighbour tour.