A* SEARCH

Dijkstra with a compass, and what happens when the compass lies

L215 MINALGORITHMS
PREDICT

A* keeps an open set and always pops the node with the lowest f = g + h: cost so far plus a guess of the cost to go. It pops E and discovers the goal G through an 8-cost edge, giving g(G) = 11. The optimal path costs 9 and is not yet found. What should A* do?

A* is Dijkstra with a guess. For each node it tracks g, the cheapest cost found from the start, and h, an estimate of the cost still to go. It always expands the open node with the lowest f = g + h, and returns when it pops the goal.

If h never overestimates the true remaining cost, h is admissible and the returned path is optimal, provided the search reopens a closed node when a cheaper path to it turns up (as the sim does). A consistent h, one with h(u) ≤ w(u,v) + h(v) on every edge, never needs a reopen. This graph’s h is admissible but not consistent at S. A good h skips nodes that lead away from the goal. A bad h that overestimates can make a detour look cheap and hide the real shortest path.

▶ SIMULATION
NODES, W = 1
S: g=0 W·h=7 (h*=9) f=7 open
A: g=∞ W·h=4 (h*=7) f=∞
B: g=∞ W·h=6 (h*=8) f=∞
C: g=∞ W·h=2 (h*=4) f=∞
D: g=∞ W·h=4 (h*=4) f=∞
E: g=∞ W·h=1 (h*=8) f=∞
G: g=∞ W·h=0 (h*=0) f=∞
OPEN (by f)
S(f=7)
EXPANDED, IN ORDER
—
RESULT
path: —
cost: — · optimal 9
nodes expanded: 0
edgesS–A 2, S–B 5, S–E 3, A–C 3, A–D 4, B–D 4, C–G 4, D–G 4, E–G 8 (undirected)
last stepOPEN = {S}, f(S) = 7
✓ returned path is optimal (cost 9)✓ W·h(n) ≤ true cost to G for every node
SCHEDULE
01[CONFIG]admissible h, weight W from the slider
02[SEARCH]pop lowest f from OPEN (#1)
03[SEARCH]pop lowest f from OPEN (#2)
04[SEARCH]pop lowest f from OPEN (#3)
05[SEARCH]pop lowest f from OPEN (#4)
06[SEARCH]pop lowest f from OPEN (#5)
07[CONFIG]finish the search, run Dijkstra for comparison
Start of a*, f = g + w·h scenario. 7 steps.
A* (graph search with reopening)

f(n) = g(n) + W * h(n)       W = 1: plain A*
  g(n): cheapest cost found so far from start to n
  h(n): estimate of the cost from n to the goal

open = {start}; g[start] = 0
while open not empty:
  u = node in open with lowest f
  if u == goal: return path(u), g[u]   // only when POPPED
  move u from open to closed
  for (v, w) in neighbours(u):
    if g[u] + w < g[v]:
      g[v] = g[u] + w; prev[v] = u
      put v in open (reopen it if it was closed)

Admissible: h(n) <= h*(n), the true remaining cost.
  => the returned path is optimal.
Consistent: h(u) <= w(u,v) + h(v). Stronger; then
  no node ever needs reopening.
W > 1 (weighted A*): faster, cost <= W * optimal.
h = 0: A* is Dijkstra.

Watch the RESULT box and the invariant strip. With W = 1, A* returns cost 9 after 5 expansions while Dijkstra needs 7. With h(C) raised to 8, C looks expensive, A* takes S→A→D→G and returns 10, and both invariants go red.

TUNE IT NOT YET

Weighted A* multiplies h by W to dive toward the goal and expand fewer nodes. Find the largest W that still returns the optimal path on this graph.

GOAL: optimal at W, suboptimal at W + 0.5

FIX IT NOT YET

This A* returns as soon as it sees the goal. On the lesson graph it sees G from E first, through an 8-cost road, and returns 11. Make it return the optimal path. Every heuristic in the tests is admissible (it never overestimates); the random-graph ones are also consistent. Return null if the goal is unreachable.

Mental model:

f = g + h is a lower bound on any path through a node, as long as h never overestimates. A* pops nodes in order of that bound, so when the goal comes out of the queue nothing left can beat it. Stop at the pop, not the discovery. Inflate h, by a bad estimate or a weight W, and you buy speed with that bound: the answer is only guaranteed within a factor W of optimal (cost ≤ W · the optimum).

IN PRODUCTION

Game and robot pathfinding on grids and navmeshes, map routing (with landmark heuristics and precomputed hierarchies), puzzle solvers, planners. Weighted A* and its variants show up wherever a good-enough path now beats the best path later.

IN INTERVIEWS

“Implement A* on a grid” and “when is A* optimal?” Name admissibility, explain why the goal check belongs at pop time, give Manhattan distance as an admissible heuristic for 4-way grids, and say what weighted A* gives up.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: UNION-FIND →