The rickshaw-puller's compass
Ask a rickshaw-puller in Old Dhaka to reach Sadarghat. No map, no GPS, but he never turns away from the river.
UCS last week expanded every city cheaper than the answer, in all directions, including Sylhet, 190 km the wrong way. It knew how far it had come, not how far it had left.
Today: add an estimate of the distance left, h(n), and decide which city to expand using it.
Best-first search: one skeleton, three orders
- A heuristic h(n) estimates the cost of the cheapest path from n to a goal. It uses knowledge about the problem; h(goal) = 0.
- Best-first search is last week's UCS skeleton with the priority queue ordered by an evaluation function f(n):
| Strategy | f(n) | Reads as |
|---|---|---|
| Uniform-cost | g(n) | cheapest so far |
| Greedy best-first | h(n) | looks closest to the goal |
| A* | g(n) + h(n) | estimated cost of the best solution through n |
Notation
g(n): cost from the start to n (known).
h(n): estimated cost from n to the goal (guessed).
h*(n): true cheapest cost from n to the goal (unknown during search).
A* was published in 1968 by Hart, Nilsson and Raphael at SRI, for Shakey, the first mobile robot that planned its own moves. It is older than the Apollo moon landing.
Greedy best-first: fast, short-sighted
- Always expand the node with the smallest h. On our map it walks straight down the coast: DHK, CUM, FEN, CTG, COX, 5 expansions (UCS needed 10).
- Not optimal: it ignores g, so a node that looks close but was expensive to reach still wins.
- Not complete as tree search: it can oscillate between two nodes that both look close (a dead end next to the goal). Graph search in a finite space fixes that.
- Worst case O(bm) time and space, but a good h cuts this dramatically.
"Closest to the goal" is not "on the best path". A river, a cantonment wall or a one-way street can make the nearest-looking point a trap.
When would you happily use greedy search anyway?
Argue it in pairs for two minutes. There may be more than one defensible answer.
Live lab: greedy vs A* on the road to Cox's Bazar
Frontier
priority queue: pop lowest f = g + hExplored
in the order expandedinitStart: frontier = [Dhaka (f = 0 + 300 = 300)]
Your turn · 1: where greedy goes wrong
Edge costs on the lines, h(n) in the tags. Run greedy best-first and then A* (graph search) from S to G. Give the expansion order and the path for each.
Work it out first.
Press Solve when the class has an answer.
A*: cost so far + estimate to go
- Exactly UCS with the queue on f instead of g. Goal test on pop (as in UCS: a goal on the frontier may still be beaten).
- If h is good, A* expands few nodes off the best path. If h = 0, A* is UCS. If h = h*, A* walks straight down the optimal path.
- Two conditions on h make A* optimal: admissibility (tree search) and consistency (graph search). Next slides.
A* in one picture: contours
UCS spreads in circles of equal g around the start. A* spreads in ellipse-like contours of equal f, stretched towards the goal. A better h makes them thinner.
The frontier entry for the goal has f = g (because h = 0). A* stops when that is the smallest f on the frontier, i.e. when nothing else could possibly lead somewhere cheaper.
Worked example: A* by hand
Given
Last week's map with h = straight-line km to Cox's Bazar (tags). Start Dhaka, goal Cox's Bazar. Graph search.
Find
The frontier (with f = g + h) after each expansion, the expansion order and the route.
Method: A* graph search (Russell & Norvig); f = g + h
Solution
Predict the answer first. Then reveal one step at a time.
When can we trust A*? Admissible and consistent
Admissible: never overestimates
Straight-line distance ≤ any road distance. Tree-search A* with an admissible h is optimal.
Consistent (monotone): the triangle inequality
Consistent ⇒ admissible, and f never decreases along a path, so graph-search A* is optimal and never needs to re-open a closed node.
Why admissible ⇒ optimal (tree search)
Let C* be the optimal cost and G₂ a worse goal on the frontier: f(G₂) = g(G₂) > C*. Some node n of the optimal path is also on the frontier, and
So n (and the rest of the optimal path) is popped before G₂ can be. ∎
Check consistency edge by edge: Feni–Chattogram: 180 ≤ 95 + 120 = 215 ✓. Bandarban–Cox's: 85 ≤ 100 ✓. Every edge of our map passes.
Your turn · 2: admissible is not always enough
(a) Is h admissible? (b) Is it consistent? (c) Run A* graph search (never re-expand an explored node). Is the answer optimal? Explain.
Work it out first.
Press Solve when the class has an answer.
Live lab: path-finding on a grid
Critical thinking · how wrong can a heuristic be?
Your boss multiplies the straight-line heuristic by 1.5 'to make the route planner faster'. What do you gain, and what can you lose?
Argue it in pairs for two minutes. There may be more than one defensible answer.
Two admissible heuristics h₁ and h₂ give different numbers for the same node. Which should A* use?
Argue it in pairs for two minutes. There may be more than one defensible answer.
A*'s weakness: memory
| Question | Greedy | A* |
|---|---|---|
| Complete? | graph search in finite spaces | yes (finite b, step costs ≥ ε > 0) |
| Optimal? | no | yes (admissible: tree; consistent: graph) |
| Time | O(bm), often far less | exponential in the error of h |
| Space | O(bm) | keeps every generated node |
A* usually runs out of memory long before it runs out of time: it stores the whole frontier and explored set.
Memory-bounded relatives (names to know)
IDA*: iterative deepening on f instead of depth (W3's IDS idea). RBFS and SMA*: forget the worst nodes when memory is full, re-create them if needed.
Real world: A* everywhere you move
When you click a destination in a strategy game, units follow paths from A* on a grid or a navigation mesh. Engines add tricks (precomputed waypoints, path smoothing), but the core is the lab you just played with.
Map apps route over millions of road segments. Plain straight-line A* is too slow there; engines use A* with much better precomputed heuristics (landmarks and the triangle inequality, "ALT") or hierarchies of shortcuts. Same admissibility idea, smarter h.
Autonomous delivery robots carry medicines and samples along hospital corridors on an occupancy grid, re-planning with A*-family planners (D* Lite) when a trolley blocks the way.
A Pathao rider's app uses travel time as cost and straight-line km ÷ 60 km/h as h. During Dhaka traffic, is this h admissible?
Argue it in pairs for two minutes. There may be more than one defensible answer.
Pop quiz
Question 1 / 5
Greedy best-first search orders its frontier by…
Three things to remember
Best-first search sorts by f: UCS f = g, greedy f = h (fast, not optimal), A* f = g + h.
Admissible (h ≤ h*) ⇒ tree-search A* optimal. Consistent (h(n) ≤ c + h(n')) ⇒ graph-search A* optimal, f never decreases.
Better h ⇒ fewer expansions. Inflate h (weighted A*) for speed with a bounded loss. A*'s real limit is memory (IDA*).
A landslide closes the Chattogram–Cox's Bazar road. Re-run A* (same h) by hand from Dhaka. Which route, what cost, how many expansions? Is h still consistent?
Work it out first.
Press Solve when the class has an answer.