Heuristics, Greedy search & A*
Invent a heuristic for a problem you have never seen, hand-trace Greedy and A*, say exactly when A* is optimal and when it silently is not — and prove the blocking argument the slides leave as “TODO”.
Heuristics: what makes one legal, what makes one good, where to get one, and exactly when A* is optimal.
- Heuristic $h(n)$ — estimates the cost from $n$'s state to a nearest goal. Problem-specific, and you write it. $h^*(n)$ is the true remaining cost.
- Greedy orders the frontier by $h$ alone: fast, incomplete as tree search, never optimal. A* orders by $f(n) = g(n) + h(n)$ — the estimated total cost of the best solution through $n$. UCS is A* with $h \equiv 0$.
- Goal-test on pop, never on generation.
- Admissible: $0 \le h(n) \le h^*(n)$ for every node — optimistic, never overestimates. Forces $h(\text{goal}) = 0$.
- Consistent: $h(A) - h(C) \le \text{cost}(A \to C)$ for every edge, plus $h = 0$ at goals. A triangle inequality. Consistent $\Rightarrow$ admissible; the converse is false.
- A* tree search is optimal iff $h$ is admissible — by blocking: an ancestor $n$ of the optimal goal $A$ is always on the frontier with $f(n) \le f(A) < f(B)$, so a suboptimal goal $B$ can never pop first.
- A* graph search needs consistency. Consistency $\Rightarrow$ $f$ never decreases along a path $\Rightarrow$ the first pop of a state is its cheapest $\Rightarrow$ closing it is safe. With a merely admissible $h$, graph search can return a suboptimal path (§9).
- Relaxation builds heuristics: delete a rule and solve the easier problem exactly. Admissible, and usually consistent, by construction. 8-puzzle: $h_{\text{TILES}} = 8$ (tiles teleport), $h_{\text{MANHATTAN}} = 18$ (tiles pass through each other).
- Dominance: $h_a \ge h_c$ everywhere means $h_a$ does no more work. Larger is better while still $\le h^*$. If neither dominates, take $\max(h_a, h_b)$ — admissible, and dominates both. Sums are not admissible; averages are dominated by the max.
- Shapes: UCS expands circles of $g \le C^*$; A* expands ellipses of $f < C^*$ with foci at start and goal. The sideways bulge is the price of the optimality proof.
Everything below is the explanation and the practice. If you are short on time, read this box, then go to the problems.
This page assumes Chapter 1: the frontier, nodes, $g(n)$ and the explored set. UCS is complete and optimal and has exactly one flaw — it has never heard of the goal. This chapter gives search a nose.
1. What UCS is missing
The professor's framing, taken literally: Donuts ASAP! Uniform-cost search's plan for reaching a specific shop is to walk one block in every direction, then two blocks in every direction, until a ring happens to contain it. Provably correct, and wasteful in a fixable way.
UCS's contours are circles around the start. If the goal is 3 km east, UCS explores 3 km west first, because nothing in its interface to the world — $\text{Actions}$, $\text{Result}$, $c$, $\text{IsGoal}$ — ever mentions where the goal is. We hand it a fifth function whose only job is to whisper “warmer / colder”.
Informed search is not free: you have to write the heuristic.
2. What a heuristic is
A heuristic is a function $$h : \text{States} \to \R_{\ge 0}$$ that estimates how close a state is to a goal. It is designed for a particular search problem — there is no universal heuristic, the same way there is no universal state space.
Read the notation carefully: $h(n)$ is written as a function of a node but it only ever looks at that node's state — not how you got there, not $g(n)$, not the future. A cheap guess from the state alone.
The two the course gives, both for pathing problems:
- Manhattan distance between $(x_1,y_1)$ and $(x_2,y_2)$: $|x_1-x_2| + |y_1-y_2|$. The distance if you may only move along grid lines — the right estimate for a Pacman maze, where diagonal moves do not exist.
- Euclidean distance: $\sqrt{(x_1-x_2)^2 + (y_1-y_2)^2}$. The as-the-crow-flies distance — the right estimate when you can move in any direction, or when you are on a road map and want the straight-line distance between two towns.
The slide's triangle: a state 10 across and 5 up from the goal is $10+5=15$ away in Manhattan terms and $\sqrt{100+25} \approx 11.2$ in Euclidean terms. Same quantity, different movement rules.
The running example: straight-line distance to Bucharest
The running example is the Romania road map: drive from Arad to Bucharest, states are cities, actions are roads, step costs are kilometres. The heuristic $h_{\text{SLD}}(n)$ is the straight-line distance from city $n$ to Bucharest — a ruler on the map, ignoring that roads exist. No search needed, and never an overestimate, because no road is shorter than the straight line. That last clause becomes admissible in §7.
In that figure the number on a road is its real length, the thing $g$ accumulates; the number under a city is $h_{\text{SLD}}$, the guess about everything ahead. One is measured, one is imagined — and the idea of this page is that you add them anyway.
The effect of a heuristic
In the slide's words: guide search towards the goal instead of all over the place. UCS in Arad ranks Zerind (75 km) ahead of Sibiu (140 km), and Zerind is the one unambiguously wrong direction; Sibiu's $h=253$ against Zerind's $374$ says “this way is 120 km more promising”, so the search leans east before spending a kilometre finding out.
“$h$ is the distance to the goal.” No — $h$ is an estimate of the distance to the goal, and the entire theory below exists because it can be wrong. The true distance has its own name, $h^*(n)$, and if you could compute $h^*$ cheaply you would not need to search at all: you would just walk downhill on $h^*$ and arrive. (A* with $h = h^*$ expands only the nodes on an optimal path. See §15.)
3. Greedy best-first search
The most obvious way to use a heuristic is also the most reckless one.
Greedy search is the generic search algorithm with a priority-queue frontier ordered by $h(n)$ alone. At every step it expands the node that looks closest to the goal, and it never once consults how much that node already cost to reach.
In other words: take UCS's pseudocode and replace the priority $g(n)$ with $h(n)$. That is the entire algorithm.
Case 1 — where Greedy wins big
Arad → Bucharest. Greedy expands Arad, sees $\{$Zerind 374, Sibiu 253, Timisoara 329$\}$ and pops Sibiu; from Sibiu, $\{$Arad 366, Oradea 380, Fagaras 176, Rimnicu Vilcea 193$\}$ and pops Fagaras; from Fagaras it pops Bucharest, $h=0$. Three expansions, zero wasted nodes, for a usable 450 km route — where UCS expands Zerind, Timisoara, Sibiu, Rimnicu Vilcea, Fagaras, Pitesti, Craiova and more. When you need a route rather than the route, an excellent trade.
Case 2 — where Greedy returns a worse path
But 450 km is not optimal — 418 km exists through Rimnicu Vilcea and Pitesti, and Greedy lost it at Sibiu:
| At Sibiu, the choice | $h$ (what Greedy sees) | road cost from Sibiu | true remaining cost $h^*$ |
|---|---|---|---|
| go to Fagaras | 176 ← smaller, so Greedy picks it | 99 | $99 + 211 = 310$ |
| go to Rimnicu Vilcea | 193 | 80 | $80 + 97 + 101 = 278$ |
Fagaras is closer in a straight line and further by road: its only exit is a single 211 km haul. The estimate was not even bad — it ranked two options in the wrong order, which is all it takes.
Case 3 — where Greedy thrashes, or never terminates
Now put Greedy in Iasi and ask for Fagaras. Neamt is geometrically closer, so Greedy drives to Neamt — a dead end whose only road returns to Iasi. In graph search it backs out, expands Vaslui and arrives, having wasted the detour. In tree search it goes Iasi → Neamt → Iasi → Neamt → … forever. That is the technical meaning of “Greedy is not complete”.
- Complete? No as tree search — it can loop between two states forever (Iasi ↔ Neamt). Yes as graph search on a finite state space: the explored set stops repeats, so it must run out of new states.
- Optimal? No, in either form, for any interesting $h$. It chooses by $h$ alone, so it has no representation of path cost at all. Arad → Bucharest returns 450 when 418 exists.
- Worst case is the same as DFS — $O(b^m)$ time and space, where $b$ is the branching factor and $m$ the maximum depth — because a bad heuristic can send it down one wrong branch forever.
- Best case is spectacular: with a perfect heuristic it walks straight to the goal in $d$ expansions.
4. A*: combining UCS and Greedy
Put the two failures side by side.
- UCS orders by backward cost $g(n)$ — the money already spent reaching $n$. A fact, blind to the future.
- Greedy orders by forward cost $h(n)$ — the money still to spend. A guess, blind to the past.
Each throws away the half of the problem the other keeps. So keep both.
A\* search is the generic search algorithm with a priority-queue frontier ordered by $$f(n) \;=\; g(n) \;+\; h(n).$$
- $g(n)$ — the cost of the path from the start state to $n$ along the branch we actually took. Known exactly.
- $h(n)$ — our estimate of the cheapest remaining cost from $n$'s state to a goal. A guess.
- $f(n)$ — therefore an estimate of the total cost of the best complete solution that goes through $n$. That sentence is the whole algorithm: A* always works on the plan that currently looks cheapest end to end.
The slide's worked example, in full
The “Combining UCS and Greedy” graph (originally Teg Grenager's): seven states, $S$ through $G$, with $h$ printed under each.
Expanding from $S$ without regenerating a node's own parent gives the slide's search tree:
The expansion order, pop by pop
| # | Popped (lowest $f$) | Children generated | Frontier after, as $\langle$node$,f\rangle$ |
|---|---|---|---|
| 1 | $S$, $f = 0+6 = 6$ | $a$ with $g=1,\,h=5$ | $\langle a,6\rangle$ |
| 2 | $a$, $f = 1+5 = 6$ | $b\,(g{=}2,h{=}6)$, $d\,(g{=}4,h{=}2)$, $e\,(g{=}9,h{=}1)$ | $\langle d,6\rangle,\ \langle b,8\rangle,\ \langle e,10\rangle$ |
| 3 | $d$, $f = 4+2 = 6$ | $G$ with $g=6,\,h=0$ | $\langle G,6\rangle,\ \langle b,8\rangle,\ \langle e,10\rangle$ |
| 4 | $G$, $f = 6+0 = 6$ | — goal test passes | return $S\!-\!a\!-\!d\!-\!G$, cost 6 |
Four expansions, and the answer is optimal. The other two on the identical graph:
| Algorithm | Priority | Pop order | Nodes expanded | Path returned | Cost |
|---|---|---|---|---|---|
| UCS | $g$ | $S_0, a_1, b_2, c_3, d_4, G_6$ | 6 | $S\!-\!a\!-\!d\!-\!G$ | 6 ✔ optimal |
| Greedy | $h$ | $S_6, a_5, e_1, d_2, G_0$ | 5 | $S\!-\!a\!-\!e\!-\!d\!-\!G$ | 12 ✘ |
| A* | $g+h$ | $S_6, a_6, d_6, G_6$ | 4 | $S\!-\!a\!-\!d\!-\!G$ | 6 ✔ optimal |
The middle row is the clearest picture of greedy failure in the course. After expanding $a$, Greedy holds $b$ at $h=6$, $d$ at $h=2$ and $e$ at $h=1$, and pops $e$: to buy a one-unit improvement in the estimate it paid eight units of real cost (the $a\!-\!e$ edge) and never noticed, because 8 appears nowhere in its priority. UCS, seeing only $g$, wanders into $b$ and $c$ first — cheap, useless.
After Greedy pops $e$, its frontier holds two nodes for state $d$, both with $h = 2$: the good one ($g=4$) and the terrible one ($g=10$). They tie on Greedy's only criterion, so the returned cost is 6 or 12 depending on an implementation detail. That uncertainty is what “no optimality guarantee” feels like. A* has no such ambiguity: $f = 6$ versus $f = 12$.
Run A* tree search (goal-test on pop, never regenerate a node's parent) on this graph. Give, in order, every node you pop, with its $g$, $h$ and $f$. Then give the solution and its cost. Finally: is this $h$ admissible?
| Edge | $S\!-\!A$ | $S\!-\!B$ | $A\!-\!C$ | $B\!-\!C$ | $B\!-\!D$ | $C\!-\!G$ | $D\!-\!G$ |
|---|---|---|---|---|---|---|---|
| cost | 2 | 1 | 2 | 5 | 3 | 4 | 6 |
| State | $S$ | $A$ | $B$ | $C$ | $D$ | $G$ |
|---|---|---|---|---|---|---|
| $h$ | 7 | 4 | 6 | 3 | 5 | 0 |
Step 1 — Set up: what does the frontier hold at the start?
One node: $S$ with $g=0$, $h=7$, so $f = 0 + 7 = 7$. Do not goal-test it on insertion; A* tests on pop. (Testing on insertion is the classic way to break A* — you would return the first goal found rather than the first goal proved cheapest.)
Step 2 — The first two pops
Pop 1: $S$, $g=0$, $h=7$, $f=7$. Not a goal. Children:
- $A$: $g = 0+2 = 2$, $h = 4$, $f = 6$.
- $B$: $g = 0+1 = 1$, $h = 6$, $f = 7$.
Frontier: $\langle A,6\rangle, \langle B,7\rangle$. Note that $B$ is the cheaper child ($g=1$ vs $2$) — UCS would take it — and A* still prefers $A$, because $A$'s whole projected journey is cheaper.
Pop 2: $A$, $g=2$, $h=4$, $f=6$. Not a goal. Only child (excluding its parent $S$): $C$ with $g = 2+2 = 4$, $h = 3$, $f = 7$.
Frontier: $\langle B,7\rangle, \langle C,7\rangle$ — a genuine tie at $f=7$.
Step 3 — Break the tie and finish
Ties at equal $f$ may be broken any way; a standard rule is “prefer the smaller $h$”, i.e. prefer nodes that look closer to done, which here means $C$ ($h=3$) over $B$ ($h=6$). Take that rule.
Pop 3: $C$, $g=4$, $h=3$, $f=7$. Not a goal. Children: $G$ with $g = 4+4 = 8$, $h=0$, $f = 8$; and $B$ with $g = 4+5 = 9$, $h=6$, $f=15$ (a second, dreadful node for state $B$ — tree search keeps it).
Frontier: $\langle B,7\rangle, \langle G,8\rangle, \langle B,15\rangle$.
Pop 4: $B$, $g=1$, $h=6$, $f=7$. Not a goal — and notice A* refuses to return the goal node sitting right there at $f=8$, because something with $f=7$ is still pending. Children: $C$ with $g = 1+5 = 6$, $h=3$, $f=9$; $D$ with $g = 1+3 = 4$, $h=5$, $f=9$.
Frontier: $\langle G,8\rangle, \langle C,9\rangle, \langle D,9\rangle, \langle B,15\rangle$.
Pop 5: $G$, $g=8$, $h=0$, $f=8$. Goal test passes → return.
Answer & the takeaway
Pop order: $S_{f=7},\ A_{f=6},\ C_{f=7},\ B_{f=7},\ G_{f=8}$. Solution $S\!-\!A\!-\!C\!-\!G$, cost $2+2+4 = 8$.
Check by brute force: $S\!-\!A\!-\!C\!-\!G = 8$; $S\!-\!B\!-\!C\!-\!G = 1{+}5{+}4 = 10$; $S\!-\!B\!-\!D\!-\!G = 1{+}3{+}6 = 10$. So 8 is optimal. ✔
Is $h$ admissible? Compute the true costs: $h^*(S)=8$, $h^*(A)=6$, $h^*(B)=9$, $h^*(C)=4$, $h^*(D)=6$, $h^*(G)=0$. Compare with $h$: $7\le 8$ ✔, $4\le 6$ ✔, $6\le 9$ ✔, $3\le 4$ ✔, $5\le 6$ ✔, $0\le 0$ ✔. Admissible — which is exactly why the answer came out optimal.
Takeaway: the $f$ values along the pop sequence were $7, 6, 7, 7, 8$ — they went down once, from $S$'s 7 to $A$'s 6. That dip is legal for an admissible heuristic but it is a symptom: this $h$ is admissible and not consistent (check the edge $S\!\to\!A$: $h(S) - h(A) = 7-4 = 3 > 2 = \text{cost}$). Keep that in your pocket until §10.
5. Watch all three run on the same maze
On a large maze with Manhattan distance as $h$: UCS's expanded region is a diamond centred on the start (a Manhattan-metric circle); A*'s is that diamond squashed along the line to the goal; Greedy's is a narrow finger reaching straight for the goal — economical, with no guarantee that the finger points down the shortest corridor.
6. Why A* can still be wrong: a bad heuristic
A* is not magic. The slide's three-node graph:
Exactly what A* does here
- Pop $S$, $f = 0+7 = 7$. Not a goal. Children: $A$ with $g=1$, $h=6$, $f(A) = \mathbf{7}$; $G$ by the direct road with $g=5$, $h=0$, $f(G) = \mathbf{5}$.
- Frontier $\langle G,5\rangle, \langle A,7\rangle$: the minimum is $G$. Pop it, goal test passes, return $S\!-\!G$ at cost 5. $A$ is never expanded, so A* never discovers that going through $A$ costs 4.
$f(A) = 7$ claims “any plan through $A$ costs about 7”, and that claim is a lie — the best plan through $A$ costs 4. The lie came from $h(A) = 6$ against a true $h^*(A) = 3$. A* trusted it, concluded $A$ was a bad neighbourhood, and stopped looking.
Estimated goal cost > actual future cost. When $h$ overestimates, A* becomes pessimistic about a region and prunes it away — and pruning without proof is exactly how you lose the optimum. So: we need estimates to be less than actual costs.
The asymmetry matters. Underestimating is harmless to correctness: A* looks somewhere it need not have, wastes time, discovers the truth. Overestimating is fatal: A* never looks, so it never discovers.
7. Admissibility
A heuristic $h$ is admissible if for every node $n$ $$0 \;\le\; h(n) \;\le\; h^*(n),$$ where $h^*(n)$ is the true cost of a cheapest path from $n$ to a nearest goal (and $h^*(n) = \infty$ if no goal is reachable from $n$).
Both inequalities carry weight. The right one says never overestimate. The left one says never hand the search a negative estimate, which would make $f$ look cheaper than a path that already exists.
The course's word for this is optimistic: an admissible heuristic always says “only about twenty minutes away” — sometimes right, never pessimistic. $h \equiv 0$ is the most optimistic of all, and admissible for every problem, which is why UCS is a special case of A*. A* only ever discards a plan by saying “its $f$ is too big”; if every $f$ underestimates the truth, a plan whose $f$ is too big has a truth that is bigger still, so discarding it was safe. That is the licence to trust A*'s dismissals.
Two sanity conditions fall out of the definition; check them reflexively:
- $h(\text{goal}) = 0$. At a goal, $h^*=0$, so $0 \le h \le 0$ forces $h = 0$. Every proof below uses this line.
- Admissibility is a claim about every state, not most states. One overestimating node is enough to destroy optimality, as Figure 4 shows with exactly one guilty node.
- Straight-line distance on a road map: admissible, because a road between two towns cannot be shorter than the segment joining them.
- Manhattan distance in a 4-connected grid with unit step costs: admissible, because every move changes exactly one coordinate by exactly 1, so you need at least $|\Delta x| + |\Delta y|$ moves — and walls only make it worse.
- Euclidean distance in that grid: still admissible (it is $\le$ Manhattan), but weaker — it underestimates by more, so it prunes less (§15).
- Manhattan distance when diagonal moves cost 1: not admissible. One diagonal move covers 2 of Manhattan distance at a cost of 1, so the estimate can be double the truth. Re-check admissibility whenever you change the action set.
From the slides: “coming up with admissible heuristics is most of what's involved in using A* in practice.” The algorithm is twenty lines; the heuristic is where the work is (§14).
8. The optimality of A* tree search — the full blocking argument
The slides state the theorem, draw the picture, and write “TODO” where the three steps go. Here are the three steps, with every justification spelled out.
If $h$ is admissible, then A* tree search (goal test on pop, frontier ordered by $f = g+h$) returns an optimal solution.
Setup, exactly as on the slide. Assume:
- $A$ is an optimal goal node — a node whose state is a goal, reached along a cheapest possible path, so $g(A) = C^*$, the optimal solution cost;
- $B$ is a suboptimal goal node — its state is a goal too, but $g(B) > C^*$;
- $h$ is admissible.
Claim: $A$ will be chosen for expansion (popped off the frontier) before $B$.
Why the claim suffices: A* returns the first goal node it pops, so if every optimal goal beats every suboptimal goal to the front of the queue, the first goal popped cannot be suboptimal.
The picture: blocking
Why there is always such an $n$
The slide compresses this into one parenthesis — “(maybe the start state; maybe $A$ itself!)” — and the whole proof rests on it. Fix an optimal path $S = n_0 \to n_1 \to \dots \to n_k = A$. I claim that at every moment before $A$ is popped, at least one $n_i$ on that path is sitting on the frontier. Induct on time:
- Initially the frontier is $\{S\} = \{n_0\}$, and $n_0$ is on the path. ✔
- Inductively, suppose some $n_i$ is on the frontier. The only way it leaves the frontier is by being popped and expanded. But expanding $n_i$ generates all its children, one of which is $n_{i+1}$ — which is therefore pushed onto the frontier at that very moment. So the property is restored immediately, with $i$ increased by 1. ✔
- The index can only climb to $k$, at which point the frontier node is $A$ — and we have stopped, because popping $A$ is the event we were waiting for. ✔
So the frontier always holds an unexpanded “front” of the optimal path: the frontier separates explored from unexplored, and you cannot get from $S$ to $A$ without crossing it.
Step 1: $f(n) \le f(A)$
$$ \begin{aligned} f(n) &= g(n) + h(n) &&\text{definition of the $f$-cost} \\ &\le g(n) + h^*(n) &&\text{admissibility of $h$: } h(n) \le h^*(n) \\ &= g(A) &&\text{$n$ lies on an optimal path to $A$} \\ &= f(A) &&\text{$h = 0$ at a goal, so } f(A) = g(A) + 0 \end{aligned} $$
Line by line.
- Line 1 is just the definition; nothing is assumed.
- Line 2 replaces the guess $h(n)$ by the truth $h^*(n)$. The inequality points this way — and only this way — because $h$ is admissible. This is the only place admissibility is used, and it is the hinge of the whole theorem.
- Line 3 is the subtle one. $g(n)$ is the cost from $S$ to $n$ along the optimal path we fixed, and $h^*(n)$ is the cheapest cost from $n$ onward. Since $n$ sits on an optimal path to $A$, the cheapest completion from $n$ is the rest of that very path, so $g(n) + h^*(n)$ is the cost of a complete optimal path: $g(n) + h^*(n) = C^* = g(A)$. (If $n$ were an arbitrary node this line would fail — it is why we insisted $n$ be on the optimal path.)
- Line 4 uses $h(A) = 0$, which we proved is forced by admissibility at any goal node.
In words: the $f$-value of a node on an optimal path never exceeds the cost of that optimal path. An optimistic estimate cannot inflate an honest partial plan past its own true total.
Step 2: $f(A) < f(B)$
$$ \begin{aligned} f(A) &= g(A) + h(A) = g(A) &&\text{definition of $f$; } h = 0 \text{ at a goal} \\ f(B) &= g(B) + h(B) = g(B) &&\text{identically, since $B$ is a goal node too} \\ g(A) &< g(B) &&\text{$B$ is suboptimal and $A$ is optimal} \\ \therefore\quad f(A) &< f(B) \end{aligned} $$
Notice there is no heuristic left in this step at all: at goal nodes $f$ collapses to $g$, so comparing two goals by $f$ is comparing two solutions by their real cost. The inequality is strict because “suboptimal” means strictly more expensive than the optimum. (If $g(B) = g(A)$ then $B$ is also optimal, and returning it would be perfectly fine — the theorem only promises optimal cost, not a unique path.)
Step 3: therefore $n$ — and then $A$ — is expanded before $B$
Chain steps 1 and 2: $$f(n) \;\le\; f(A) \;<\; f(B) \qquad \Longrightarrow \qquad f(n) < f(B).$$
A* always pops the frontier node of minimum $f$. While both $n$ and $B$ sit on the frontier, $n$'s key is strictly smaller, so $B$ cannot be selected: some strictly better node is always available. $n$ is expanded first.
Now iterate. Expanding $n$ puts the next node of the optimal path, $n_{i+1}$, on the frontier — and $n_{i+1}$ is also an ancestor of $A$ on the optimal path, so the identical argument applies to it. One after another, all ancestors of $A$ are expanded before $B$. The chain is finite and ends at $A$ itself, so $A$ is expanded before $B$.
$B$ was an arbitrary suboptimal goal node, so no suboptimal goal can be popped while any optimal goal's path is still pending. Hence the first goal node A* pops has cost $C^*$. A* tree search is optimal. $\blacksquare$
- Goal-testing on insertion. If you return a goal the moment you generate it, the blocking argument evaporates: $B$ never had to win the queue. Test on pop. In Figure 4's counterexample and in Guided problem 1 this is the difference between right and wrong answers.
- Assuming $n$ can be any node. Step 1's third line needs $n$ on an optimal path to $A$. For a general node, $g(n) + h^*(n)$ is the cost of the best path through $n$, which may be worse than $C^*$.
- Thinking this proves anything about graph search. It does not — the proof never mentions an explored set, and §9 shows an admissible heuristic that breaks graph search on a four-node example.
9. A* graph search, and why admissibility is not enough
Everything above was tree search: no explored set, states re-generated freely. Real implementations use graph search — keep an explored set, never expand a state twice — or a cycle drowns you:
- A* tree search = the tree search algorithm from Chapter 1, frontier = priority queue keyed by $f(n) = g(n)+h(n)$.
- A* graph search = the UCS graph search algorithm from Chapter 1, frontier = priority queue keyed by $f(n) = g(n)+h(n)$.
One word changed, and the optimality theorem broke:
The two traces, side by side
| # | A* tree search | A* graph search (explored set) |
|---|---|---|
| 1 | Frontier: S (0+2)=2. Pop $S$. Push S-A (1+4)=5, S-C (3+1)=4. | Same. Explored $=\{S\}$. Frontier: S-A:5, S-C:4. |
| 2 | Pop S-C ($f=4$). Push S-C-G (6+0)=6. | Pop S-C ($f=4$). Explored $=\{S,C\}$ — $C$ is now closed forever. Push S-C-G:6. |
| 3 | Pop S-A ($f=5$). Push S-A-C (2+1)=3. Nothing stops a second node for state $C$. | Pop S-A ($f=5$). Its child is $C$ with $g=2$ — but $C \in$ explored, so the child is discarded. |
| 4 | Pop S-A-C ($f=3$). Push S-A-C-G (5+0)=5. | Frontier holds only S-C-G:6. Pop it — goal. |
| 5 | Pop S-A-C-G ($f=5$) — goal. Return $S\!-\!A\!-\!C\!-\!G$, cost 5. ✔ | Return $S\!-\!C\!-\!G$, cost 6. ✘ suboptimal |
The slide's summary of the bug, worth memorising: “$C$ (specifically $S\!-\!C$) is no longer on the frontier, so it can't be replaced with the better $S\!-\!A\!-\!C$ option.”
Why this is not the same bug as Chapter 1's
The replacement rule only works on nodes still on the frontier. Once a state is popped into the explored set, graph search treats it as settled — and for UCS that belief is true: UCS pops in increasing $g$, so a state's first pop carries its minimum possible $g$. A* pops by $f$, which mixes in a guess. Here A* popped $C$ with $g=3$ because $h(C)=1$ made that route look finished ($f=4$), while the genuinely better route to $C$ hid behind $A$, whose $h(A)=4$ made it look expensive ($f=5$). The heuristic told A* to settle $C$ too early.
Admissibility guarantees that A* pops goal nodes in a safe order. It says nothing about the order in which A* pops ordinary nodes. Graph search's closed set is a bet that the first pop of every state is its best pop — and that bet needs a stronger property than admissibility. The property is called consistency.
beyond syllabus Two other repairs: reopening a closed state when it is rediscovered with smaller $g$, or running tree search and eating the duplicates. The course fixes the heuristic instead.
S-A ($f=5$) and S-C ($f=4$); popping S-C closes state $C$ and pushes S-C-G ($f=6$); popping S-A generates $C$ again with $g=2$, but $C$ is in the explored set so that node is never created — which is why S-A-C (and therefore S-A-C-G) never appears. Options B and D describe tree search, which does build S-A-C and finishes with the optimal S-A-C-G at cost 5.The slide's frontier column reads S: 0+2 = 2, S-A: 1+4 = 5, S-C: 2+1 = 3, S-C-G: 6+0 = 6. The third line mixes the $g$ of the good route to $C$ ($g=2$, through $A$) with the label S-C; the node actually on the frontier is the direct route, $g = 3$, $f = 4$. Nothing downstream changes — either way $C$ is popped second and the answer is still 6 — but write $f(\texttt{S-C}) = 4$ in your own work.
10. Consistency (the triangle inequality)
Admissibility constrains $h$ at each node relative to the goal. Consistency constrains $h$ across each edge, so that the estimate cannot drop faster than you can actually travel.
$h$ is consistent if for every node $A$ and every successor $C$ of $A$, $$h(A) - h(C) \;\le\; \text{cost}(A \to C), \qquad\text{equivalently}\qquad h(A) \;\le\; \text{cost}(A, C) + h(C),$$ and $h(g) = 0$ at every goal $g$.
In the slide's words: the “heuristic step cost” must be $\le$ the actual cost, for each step. The second form is a triangle inequality — “kind of”, as the slide says — with the three sides being $A \to$ goal (estimated), $A \to C$ (real), $C \to$ goal (estimated).
$h$ is a sign at each junction reading “Bucharest: about 190 km”. Consistency says the signs must be mutually believable: drive 80 km, then read “about 60 km”, and the pair is lying — together they claim 130 km of progress for 80 km of driving. The constraint is one-sided: signs may increase freely, since driving 80 km away from the goal may legally raise the estimate by 80.
Consequence 1 — $f$ never decreases along a path
Let $C$ be a successor of $A$ on some path, so that $g(C) = g(A) + \text{cost}(A,C)$ (the definition of $g$ along a branch). Then $$ \begin{aligned} f(C) &= g(C) + h(C) &&\text{definition of $f$}\\ &= g(A) + \text{cost}(A,C) + h(C) &&\text{definition of $g$ along the branch}\\ &\ge g(A) + h(A) &&\text{consistency: } \text{cost}(A,C) + h(C) \ge h(A)\\ &= f(A). &&\text{definition of $f$} \end{aligned} $$ So $f(C) \ge f(A)$: as you walk down any branch of the search tree, $f$ is monotone non-decreasing. (Hence the other name for consistency: monotonicity.) $\blacksquare$
Why that matters: if $f$ never decreases along a path, the $f$ of the node A* pops never decreases over time either. A* sweeps outward in bands of increasing $f$, exactly as UCS does in bands of increasing $g$, so the first time a state is popped its $g$ is already optimal — anything arriving later arrives with an $f$ at least as large. That is the bet the explored set makes:
With consistency, closing a state is safe: the route by which a state was first popped is a cheapest route to it, so the discard in row 3 of the Figure 6 trace can never throw away an improvement.
Sanity-check Figure 6 against the definition: the edge $A \to C$ has $h(A) - h(C) = 4 - 1 = 3$, while $\text{cost}(A,C) = 1$. Since $3 > 1$, that edge violates consistency — and it is exactly the edge whose rediscovery got discarded. The counterexample was not bad luck; it was built on the single inconsistent edge.
Consistency implies admissibility
Take any node $n$ and let $n = m_0 \to m_1 \to \dots \to m_k = \gamma$ be a cheapest path from $n$ to a nearest goal $\gamma$, so that by definition $$h^*(n) = \sum_{i=0}^{k-1} \text{cost}(m_i, m_{i+1}).$$ Induct on $k$, the number of steps on that path.
Base case $k = 0$. Then $n$ is itself a goal, $h^*(n) = 0$, and consistency requires $h(n) = 0$. So $h(n) \le h^*(n)$. ✔
Inductive step. Suppose the claim holds for every node whose cheapest path to a goal has $k-1$ steps. The suffix $m_1 \to \dots \to m_k$ is a cheapest path from $m_1$ to a goal (a suffix of a cheapest path is a cheapest path — otherwise splice in the cheaper suffix and beat $h^*(n)$), and it has $k-1$ steps, so the hypothesis gives $h(m_1) \le h^*(m_1)$. Then $$ \begin{aligned} h(n) &\le \text{cost}(n, m_1) + h(m_1) &&\text{consistency across the first edge}\\ &\le \text{cost}(n, m_1) + h^*(m_1) &&\text{inductive hypothesis}\\ &= h^*(n). &&\text{the path is cheapest, so its cost splits this way} \end{aligned} $$ ✔ By induction, $h(n) \le h^*(n)$ for every $n$; together with $h \ge 0$ that is admissibility. $\blacksquare$
The converse is false, and you have already seen the witness: Figure 6's $h = (2,4,1,0)$ is admissible and inconsistent. So consistent is strictly stronger than admissible.
Consistency is a local promise (“I never shrink faster than you can walk”); admissibility a global one (“I never exceed the true remaining distance”). Chaining the local promise all the way to the goal produces the global one. The converse fails because a global bound says nothing about how wildly the estimate may bounce between neighbours.
Directed graph, start $S$, single goal $G$:
| Edge | $S\!\to\!A$ | $S\!\to\!B$ | $A\!\to\!D$ | $B\!\to\!D$ | $D\!\to\!G$ | $B\!\to\!G$ |
|---|---|---|---|---|---|---|
| cost | 1 | 4 | 1 | 1 | 2 | 7 |
| State | $S$ | $A$ | $B$ | $D$ | $G$ |
|---|---|---|---|---|---|
| $h$ | 4 | 3 | 3 | 2 | 0 |
(a) Compute $h^*$ for every state and decide admissibility. (b) Check consistency on every edge. (c) If it is inconsistent, does A* graph search still return the optimal answer here?
Step 1 — Compute $h^*$ by working backwards from $G$
$h^*(G) = 0$. $h^*(D) = 2$ (only edge $D\!\to\!G$). $h^*(A) = 1 + h^*(D) = 3$. $h^*(B) = \min(1 + h^*(D),\ 7) = \min(3, 7) = 3$. $h^*(S) = \min(1 + h^*(A),\ 4 + h^*(B)) = \min(4, 7) = 4$.
Always do this from the goal backwards; computing $h^*$ forwards from $S$ is a different (and harder) question.
Step 2 — Admissibility: compare $h$ with $h^*$ state by state
| $S$ | $A$ | $B$ | $D$ | $G$ | |
|---|---|---|---|---|---|
| $h$ | 4 | 3 | 3 | 2 | 0 |
| $h^*$ | 4 | 3 | 3 | 2 | 0 |
| $h \le h^*$? | ✔ | ✔ | ✔ | ✔ | ✔ |
Admissible — in fact $h = h^*$ exactly, the perfect heuristic. Hold that thought, because it makes the next step surprising.
Step 3 — Consistency: test $h(X) - h(Y) \le \text{cost}(X \to Y)$ on every edge
| Edge | $h(X)-h(Y)$ | cost | OK? |
|---|---|---|---|
| $S\!\to\!A$ | $4-3 = 1$ | 1 | ✔ (equality is allowed) |
| $S\!\to\!B$ | $4-3 = 1$ | 4 | ✔ |
| $A\!\to\!D$ | $3-2 = 1$ | 1 | ✔ |
| $B\!\to\!D$ | $3-2 = 1$ | 1 | ✔ |
| $D\!\to\!G$ | $2-0 = 2$ | 2 | ✔ |
| $B\!\to\!G$ | $3-0 = 3$ | 7 | ✔ |
All six pass, so this one is consistent. That is no accident: $h^*$ itself is always consistent, because $h^*(X) \le \text{cost}(X,Y) + h^*(Y)$ is just the statement “the best route from $X$ is no worse than the route that goes via $Y$”.
Step 4 — Now break it, and find the violating edge
Change a single number: let $h(A) = 1$ instead of 3 (everything else unchanged). Admissibility survives — $1 \le 3$ ✔, and every other state is untouched. Consistency does not:
- $S \to A$: $h(S) - h(A) = 4 - 1 = 3$ but $\text{cost} = 1$. $3 > 1$ → violated.
- $A \to D$: $h(A) - h(D) = 1 - 2 = -1 \le 1$ ✔ (a drop of $-1$ means the estimate went up, always legal).
The violating edge is $S \to A$. The recipe for finding one: for each edge, compute $h(\text{tail}) - h(\text{head}) - \text{cost}$; any strictly positive value names a violation. You are looking for a place where the estimate collapses faster than the road allows — here the estimate falls by 3 over a 1-cost road.
Answer & the takeaway
(a) Admissible (with room to spare after the change). (b) Consistent as given; after setting $h(A)=1$, inconsistent, violating edge $S \to A$. (c) Yes — A* graph search still gets the optimal answer on this particular graph, even with the inconsistent $h(A) = 1$: run it and you pop $S(f{=}4)$, $A(f{=}2)$, $D(f{=}4)$, $G(f{=}4)$ and return $S\!-\!A\!-\!D\!-\!G$ at cost 4, which is optimal.
That is the real lesson. Inconsistency does not guarantee failure; it merely withdraws the guarantee of success. Optimality theorems are promises about all inputs. “It worked on my graph” is not a counterexample to anything, and it is not a proof of anything either.
11. The optimality summary you must memorise
| Tree search | Graph search (explored set) | |
|---|---|---|
| A* | optimal if $h$ is admissible | optimal if $h$ is consistent |
| UCS | optimal — the special case $h \equiv 0$, and $0 \le h^*$ always | optimal — and $h \equiv 0$ is consistent, since $0 - 0 = 0 \le \text{cost}$ for every edge |
| Greedy | never guaranteed | never guaranteed |
Four things to carry away:
- UCS is not a different algorithm. It is A* with the laziest heuristic; every A* theorem specialises to UCS by substituting $h = 0$, including Chapter 1's optimality proof — the blocking argument with the $h$'s crossed out.
- The requirement gets stronger when you add the explored set. Graph search buys memory and time, and pays with a stronger obligation on $h$.
- Consistency implies admissibility (§10), so the graph-search row implies the tree-search row: a consistent $h$ is safe everywhere, an admissible $h$ only in a tree.
- And the slides' reassurance: “in general, most natural admissible heuristics tend to be consistent, especially if from relaxed problems.” An observation, not a theorem — and the reason nobody worries much in practice.
Suppose $h(n)$ is the exact optimal cost of $n$ in a relaxed problem (same states, same or cheaper edges, possibly extra edges). There $h$ is a true optimal-cost function, so it obeys its own triangle inequality: $h(A) \le \text{cost}_{\text{relaxed}}(A,C) + h(C)$. Every real edge is also a relaxed edge with $\text{cost}_{\text{relaxed}} \le \text{cost}$, so $h(A) \le \text{cost}(A,C) + h(C)$ — consistency, in one line. Both 8-puzzle heuristics below are of this form.
12. The two algorithms, written out
Both blocks from the slides. Read them together: the only textual difference is the priority-queue key.
function UNIFORM-COST-SEARCH(problem) returns a solution, or failure
initialize the explored set to be empty
initialize the frontier as a priority queue using g(n) as the priority
add initial state of problem to frontier with priority g(S) = 0
loop do
if the frontier is empty then
return failure
choose a node and remove it from the frontier
if the node contains a goal state then
return the corresponding solution
add the node state to the explored set
for each resulting child from node
if the child state is not already in the frontier or explored set then
add child to the frontier
else if the child is already in the frontier with higher g(n) then
replace that frontier node with child
function A-STAR-SEARCH(problem) returns a solution, or failure
initialize the explored set to be empty
initialize the frontier as a priority queue using f(n) = g(n) + h(n) as the priority
add initial state of problem to frontier with priority f(S) = 0 + h(S)
loop do
if the frontier is empty then
return failure
choose a node and remove it from the frontier
if the node contains a goal state then
return the corresponding solution
add the node state to the explored set
for each resulting child from node
if the child state is not already in the frontier or explored set then
add child to the frontier
else if the child is already in the frontier with higher f(n) then
replace that frontier node with child
Five lines worth annotating — each is an exam question waiting to happen:
choose a node and remove it from the frontier— “choose” means the minimum-priority node. The only place the priority is consulted, which is why swapping the key is the only edit needed.if the node contains a goal state then returnsits after the pop and before the expansion. Move it into the child loop and both algorithms lose optimality.add the node state to the explored setmakes these graph search. Delete it and its membership check and you have tree search, which is what §8 proves about.if the child state is not already in the frontier or explored set— state, not node. Two nodes with the same state are what we are policing.else if the child is already in the frontier with higher f(n) then replace— the repair branch, and it handles only the frontier case. There is deliberately no branch for “already explored with higher $f$”, and that missing branch is the bug in Figure 6. Replacement must rewrite the parent pointer too, or the recovered path will not match the recovered cost.
In the A* version the replacement test is written on $f$. For two nodes with the same state that is equivalent to testing $g$, since they share $h$: $f_1 > f_2 \iff g_1 > g_2$. Both phrasings are correct here — but only because the states match. Never compare $f$ across different states to decide which path is cheaper.
13. UCS vs A* contours
The heuristic's quality is now a geometric parameter:
- $h \equiv 0$: minor axis as long as major — the ellipse is the circle, and A* is UCS.
- A weak but admissible $h$: a fat ellipse. Fewer nodes than UCS, still a lot.
- A strong admissible $h$: a thin cigar hugging the straight line between start and goal.
- $h = h^*$: the ellipse collapses onto the segment — A* expands only nodes on optimal paths. The best any heuristic can do.
The slide's word hedges matters: A* does not expand only the straight line, and must not — the sideways bulge is the set of alternatives it has to rule out before it can honestly claim optimality. Greedy skips the bulge, which is why it is faster and why it can be wrong. The bulge is the price of the proof.
14. Creating heuristics: relaxed problems
Where do you get a heuristic? The slides' recipe:
Admissible heuristics are usually the exact solution costs of relaxed problems, where you delete constraints so that new actions become available.
Why it always works: any solution to the real problem is still a legal solution to the relaxed problem (you removed rules, you did not add any), so the relaxed optimum can only be cheaper. Therefore $$h(n) \;=\; \text{optimal cost from } n \text{ in the relaxed problem} \;\le\; h^*(n),$$ which is admissibility, obtained for free — you never have to check it case by case. And by the argument in §11, such an $h$ is normally consistent too.
The Romania heuristic is exactly this: relax “stay on roads” to “drive straight across fields”, and the relaxed optimum is the straight-line distance — 366 km from Arad, Pythagoras instead of search. Relax just enough that the easier problem becomes trivial while still capturing something real about the difficulty.
Example: the 8-puzzle
The slide's five questions about the 8-puzzle, answered:
- States: an arrangement of the eight numbered tiles plus the blank in nine cells — a permutation of $\{1,\dots,8,\square\}$.
- How many? $9! = 362{,}880$, but only half — 181,440 — are reachable from any given start, because a legal move changes tile-permutation parity in lockstep with the blank's displacement: the space splits into two components that never communicate. Pry out two tiles and swap them and the puzzle becomes unsolvable.
- Actions: move the blank up, down, left or right. Defining actions on the blank rather than the tiles gives a branching factor of at most 4 instead of 8 for the same state space — a free win.
- How many actions from the start? It depends only on where the blank is: corner 2, edge 3, centre 4. The standard start has the blank in the centre, so 4. (Average over the space is about 3.)
- Step costs: $c = 1$ per move, so path cost = number of moves, and $h^*$ is measured in moves — as every heuristic below must be.
8-puzzle heuristic I — misplaced tiles
$h_{\text{TILES}}(n) = $ the number of tiles that are not in their goal position (never counting the blank).
On the start state above, every one of the eight tiles is somewhere it should not be, so $h_{\text{TILES}}(\text{start}) = 8$.
Why is it admissible? Each move slides exactly one tile into a new cell. So every misplaced tile requires at least one move of its own before the puzzle can be solved, and no single move can fix two tiles at once. Therefore the number of remaining moves is at least the number of misplaced tiles: $h_{\text{TILES}} \le h^*$. ✔
Which relaxation? Delete both rules about where a tile may go: a tile may be teleported to its goal square in one move. The optimal solution to that fantasy puzzle is one teleport per misplaced tile = 8, and that relaxed cost is $h_{\text{TILES}}$ — which is why admissibility never had to be argued case by case.
8-puzzle heuristic II — total Manhattan distance
Now relax only one rule — the one that says a tile may move only into the blank — and keep the rule that a tile moves one square at a time, orthogonally. The slide's phrasing: any tile could slide any direction at any time, ignoring other tiles.
$h_{\text{MANHATTAN}}(n) = \sum_{\text{tiles } t} \big( |r_t - r_t^{\text{goal}}| + |c_t - c_t^{\text{goal}}| \big)$ — the total Manhattan distance of every tile from where it belongs.
On the start state, tile by tile in numeric order: $3+1+2+2+2+3+3+2 = \mathbf{18}$, exactly the slide's “$h(\text{start}) = 3+1+2+\dots = 18$”.
Why is it admissible? A tile at Manhattan distance $d$ from its home needs at least $d$ moves of its own, because one move changes one of its coordinates by one. Summing over tiles, and using again that each move moves only one tile, gives $h_{\text{MANHATTAN}} \le h^*$. ✔ Note both facts are needed: “$\ge d$ moves per tile” and “no move helps two tiles”. Drop the second and you could double-count.
And $h_{\text{MANHATTAN}} \ge h_{\text{TILES}}$ always, because a misplaced tile contributes at least 1 to the sum and a correctly placed one contributes 0 — which is dominance, §15.
The statistics table, and what it is really saying
| Algorithm | Average nodes expanded when the optimal path has… | ||
|---|---|---|---|
| …4 steps | …8 steps | …12 steps | |
| UCS | 112 | 6,300 | $3.6 \times 10^6$ |
| A*TILES | 13 | 39 | 227 |
| A*MANHATTAN | 12 | 25 | 73 |
Statistics from Andrew Moore, as given on the slide.
Along the UCS row: $112 \to 6{,}300 \to 3{,}600{,}000$ — every 4 extra steps of depth multiplies the work by roughly 56, then 570, which is what $O(b^d)$ feels like. At depth 12, 3.6 million nodes is twenty times the entire reachable state space of 181,440, so these are tree-search counts, re-expanding states over and over. Along the A*MANHATTAN row: $12 \to 25 \to 73$ — a factor of 49,000 less work than UCS at depth 12, purely because somebody decided which rule to delete.
Down the depth-12 column: $3.6\times10^6 \to 227 \to 73$ — the same algorithm with $h = 0$, then $h_{\text{TILES}}$, then $h_{\text{MANHATTAN}}$. The table is not a comparison of algorithms; it is a measurement of heuristic quality. A better heuristic does not trim a constant factor — it lowers the effective branching factor, which changes the exponent, which is why the gaps widen with depth.
If A* expands $N$ nodes for a depth-$d$ solution, the effective branching factor $b^*$ solves $N \approx (b^*)^d$. At depth 12: UCS $b^* \approx 3.6$, $h_{\text{TILES}} \approx 1.57$, $h_{\text{MANHATTAN}} \approx 1.42$. Getting $b^*$ near 1 is the game. beyond syllabus
Problem. Pacman is in a maze with walls. There are $k$ dots left. A state is (Pacman's position, which dots remain). Actions are N/S/E/W into a non-wall square, each costing 1; a dot is eaten automatically by stepping on it. The goal test is “no dots remain”. Design an admissible heuristic, argue why it is admissible, and say which relaxation it comes from. Then find a better one.
Step 1 — First, list the constraints you could delete
Relaxation is a menu, so write the menu down. The real problem says: (i) you must move one square at a time; (ii) you cannot pass through walls; (iii) you must visit every remaining dot; (iv) you can only be in one place at a time.
Each rule you delete makes a different, easier problem — and its exact cost is a candidate heuristic. Deleting nothing gives $h = h^*$ (perfect, unaffordable). Deleting everything gives $h = 0$ (free, useless). The craft is in between.
Step 2 — Candidate A: delete rule (iii) down to “eat one dot”
Relaxation: you only have to reach the farthest remaining dot; the others vanish on their own.
$h_A = \max_{\text{dots } d} \text{mazeDistance}(\text{pacman}, d)$.
Admissible? Yes. Any real plan must at some point step onto that farthest dot, and getting there costs at least its maze distance. So $h_A \le h^*$. ✔
Use maze distance (a small BFS that respects walls), not Manhattan — it is larger, still admissible, and therefore better (§15). If you cannot afford the BFS, Manhattan distance is a legal, weaker fallback.
A near-miss worth naming: using the distance to the nearest dot is also admissible but much weaker; using the sum over all dots is not admissible at all — one walk can collect many dots, so the sum wildly overestimates, and A* will happily return garbage.
Step 3 — Candidate B: delete rules (i) and (ii)
Relaxation: Pacman teleports to any square he likes, but still must land on each dot once.
$h_B = $ the number of remaining dots, $k$.
Admissible? Yes: each move eats at most one dot (you land on one square), so at least $k$ moves remain. ✔ It costs $O(1)$ to compute if you keep a counter in the state.
Notice $h_A$ and $h_B$ measure different things — distance versus quantity — and neither dominates the other. One dot far away: $h_A$ is big, $h_B = 1$. Fifty dots all adjacent: $h_B = 50$, $h_A$ is small. This is the exact situation §15 tells you what to do about.
Answer & the takeaway
Answer: $h(n) = \max\big(h_A(n),\, h_B(n)\big)$ — the larger of “maze distance to the farthest remaining dot” and “number of remaining dots”. It is admissible because the max of admissible heuristics is admissible (§15 proves it), and it dominates both of its parts, so A* with it expands no more nodes than with either alone.
beyond syllabus Stronger still: relax rule (iv) only partially and take $h = \text{(distance to the nearest dot)} + \text{(weight of a minimum spanning tree over the remaining dots, using maze distances)}$. Still admissible — any tour that visits all dots contains a spanning connected subgraph of them — and much tighter. This is the standard answer to the Berkeley “eat all the food” project.
The takeaway, and the procedure to use in an exam: (1) write the problem's rules as a list; (2) cross one out; (3) ask “can I now compute the optimal cost in closed form or with a trivial algorithm?”; (4) if yes, that cost is your heuristic and it is admissible by construction; (5) if two candidates disagree about which situations are hard, take the max.
15. Dominance, and combining heuristics
For two heuristics, $h_a$ dominates $h_c$ — written $h_a \ge h_c$ — if $$\forall n:\quad h_a(n) \;\ge\; h_c(n).$$ Read it as: $h_a$ is everywhere at least as well informed as $h_c$. (The $\forall n$ is doing real work: being bigger on most nodes is not dominance and buys no guarantee.)
The slide's summary: roughly speaking, larger is better — as long as both are admissible. Why, precisely:
With a consistent heuristic, A* must expand every node with $f(n) < C^*$, i.e. every node with $g(n) + h(n) < C^*$, and may skip the rest. Now suppose $h_a \ge h_c$, both admissible. If a node satisfies $g(n) + h_a(n) < C^*$ then it also satisfies $g(n) + h_c(n) < C^*$, since $h_c \le h_a$. So $$\{\text{nodes A*}_{h_a} \text{ must expand}\} \;\subseteq\; \{\text{nodes A*}_{h_c} \text{ must expand}\}.$$ The better-informed search's workload is a subset of the worse-informed one's. Larger admissible $h$ ⟹ more nodes pruned ⟹ never more work (up to tie-breaking). $\blacksquare$
And it does not cost correctness, because admissibility caps how large $h$ may get: the ceiling is $h^*$. “Larger is better” only holds beneath that ceiling; above it, larger means wrong, which is Figure 4.
The two extremes named in the slides:
- The zero heuristic is pretty bad. A* with $h=0$ is uniform-cost search ($g + 0 = g$): complete, optimal, dominated by every other admissible heuristic, so it does the most work any admissible A* can. The floor.
- The exact heuristic is pretty good, but too expensive. $h = h^*$ dominates everything admissible and expands only nodes on optimal paths — but computing $h^*(n)$ means solving the problem from $n$. The ceiling, unreachable by construction.
Every useful heuristic lives between the two, and the trade is always the same: closer to $h^*$ prunes more nodes but costs more per node. A heuristic that halves the node count and triples the per-node cost is a loss.
When neither dominates: take the max
Given admissible $h_a$ and $h_b$ where neither dominates the other, define $$h(n) \;=\; \max\big(h_a(n),\, h_b(n)\big).$$ The max of admissible heuristics is admissible, and it dominates both.
Admissible. Fix any node $n$. The value $\max(h_a(n), h_b(n))$ equals either $h_a(n)$ or $h_b(n)$ — whichever is larger — and each of those is $\le h^*(n)$ by the admissibility of its own heuristic. So the max is $\le h^*(n)$. Non-negativity is immediate, since the max of two non-negative numbers is non-negative. Hence $0 \le h(n) \le h^*(n)$. ✔
Dominates both. By definition of the maximum, $\max(h_a(n), h_b(n)) \ge h_a(n)$ and $\ge h_b(n)$, for every $n$. That is exactly $h \ge h_a$ and $h \ge h_b$. ✔ $\blacksquare$
beyond syllabus The max of two consistent heuristics is consistent too: if the max at $A$ is attained by $h_a$, then $h(A) = h_a(A) \le \text{cost}(A,C) + h_a(C) \le \text{cost}(A,C) + h(C)$, and symmetrically for $h_b$. So you may combine consistent heuristics freely without losing graph-search optimality.
$\tfrac12(h_a + h_b)$ is admissible but pointless: it is dominated by the max, so it does strictly more work for the same guarantee. And $h_a + h_b$ is not admissible in general — two admissible estimates added together can easily exceed $h^*$ (take $h_a = h_b = h^*$; the sum is $2h^*$). Max is the only combinator that is both safe and never worse than its inputs.
16. Recap
The Essentials box at the top is the recap. Take the two checks above cold — and if any line in the box is not yet obvious, that is the section to reread.
Next: Chapter 3 keeps the tree but takes away the assumption that you control every move. With an opponent picking half the branches, “cheapest path” stops meaning anything and we need minimax — and pruning returns, sharper, as $\alpha$-$\beta$.