Part I · Chapter 2

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”.

Essentials

Heuristics: what makes one legal, what makes one good, where to get one, and exactly when A* is optimal.

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.

Intuition

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

Definition — heuristic

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:

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.

Figure 1 · Romania, with road costs and $h_{\text{SLD}}$ (straight-line km to Bucharest) at every city
71 75 151 140 118 111 70 75 120 146 80 99 97 138 101 211 90 85 98 86 142 92 87 Oradea h=380 Zerind h=374 Arad h=366 Timisoara h=329 Lugoj h=244 Mehadia h=241 Drobeta h=242 Craiova h=160 Sibiu h=253 Rimnicu Vilcea h=193 Fagaras h=176 Pitesti h=100 Bucharest h=0 Giurgiu h=77 Urziceni h=80 Hirsova h=151 Eforie h=161 Vaslui h=199 Iasi h=226 Neamt h=234
■ thick solid = the route Greedy takes (Arad → Sibiu → Fagaras → Bucharest, 450 km). ■ dashed = the genuinely optimal route (Arad → Sibiu → Rimnicu Vilcea → Pitesti → Bucharest, 418 km). Both start identically; they split at Sibiu, where Fagaras looks closer to Bucharest ($h=176$ vs $h=193$) and is in fact a worse move. Bucharest has $h=0$, because it is the goal.

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.

Common mistake

“$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.

Definition — Greedy (best-first) search

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 Sibiutrue remaining cost $h^*$
go to Fagaras176 ← smaller, so Greedy picks it99$99 + 211 = 310$
go to Rimnicu Vilcea19380$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”.

Verdict — Greedy search
Greedy search with $h(n) = 0$ for every $n$ behaves like which algorithm?
Why: if every node has priority 0, the priority queue is entirely ties, so behaviour is decided by the tie-break: pop the oldest and you get BFS, pop the newest and you get DFS. It is not UCS, because Greedy's priority is $h$ and never $g$ — with $h \equiv 0$, Greedy throws away the only information it had. Contrast this with A* with $h \equiv 0$, whose priority is $g + 0 = g$, and which therefore is exactly UCS (§11).

4. A*: combining UCS and Greedy

Put the two failures side by side.

Each throws away the half of the problem the other keeps. So keep both.

Definition — A* search

A\* search is the generic search algorithm with a priority-queue frontier ordered by $$f(n) \;=\; g(n) \;+\; h(n).$$

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.

Figure 2a · The state space graph. Edge labels are step costs; $h$ is printed under each state
1 3 2 8 1 1 1 Sad Geb c h=6 h=5 h=2 h=0 h=1 h=6 h=7
Edges (undirected, but a search never generates its own parent): $S\!-\!a=1$, $a\!-\!b=1$, $b\!-\!c=1$, $a\!-\!d=3$, $a\!-\!e=8$, $e\!-\!d=1$, $d\!-\!G=2$. The optimal solution is $S\!-\!a\!-\!d\!-\!G$ at cost $1+3+2 = 6$; the decoy is $S\!-\!a\!-\!e\!-\!d\!-\!G$ at cost $1+8+1+2 = 12$.

Expanding from $S$ without regenerating a node's own parent gives the slide's search tree:

Figure 2b · The search tree, with $g$ and $h$ on every node (the slide's tree, reproduced)
Sa bde cGd G g=0h=6 g=1h=5 g=2h=6 g=4h=2 g=9h=1 g=3h=7 g=6h=0 g=10h=2 g=12h=0 f=6 f=6 f=8 f=6 f=10 f=10 f=6 f=12 f=12 orange outline = the four nodes A* actually expands
$g$ is measured along the branch; $h$ is read off the state; $f=g+h$ is in orange on the left of each node. A* pops in increasing $f$ order and stops the first time it pops a goal.

The expansion order, pop by pop

#Popped (lowest $f$)Children generatedFrontier 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 passesreturn $S\!-\!a\!-\!d\!-\!G$, cost 6

Four expansions, and the answer is optimal. The other two on the identical graph:

AlgorithmPriorityPop orderNodes expandedPath returnedCost
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.

Common mistake — the tie at $d$

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$.

Guided problem 1Hand-run A* and list the pop order with $f$ values

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$
cost2125346
State$S$$A$$B$$C$$D$$G$
$h$746350
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

Key idea — the shape each one explores

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:

Figure 4 · “Is A* optimal?” — the counterexample from the slides
1 3 5 SAG h=7 h=6 h=0 true costs: $h^*(S)=4$, $h^*(A)=3$, $h^*(G)=0$ — both $h(S)$ and $h(A)$ are overestimates
The optimal plan is $S \to A \to G$ at cost $1+3 = 4$. A* returns $S \to G$ at cost 5. Nothing is wrong with A*; everything is wrong with $h(A) = 6$.

Exactly what A* does here

  1. 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}$.
  2. 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.

Key idea — the one-line diagnosis from the slide

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

Definition — admissible (optimistic) heuristic

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:

Example — the ones you already believe

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.

Theorem — A* tree search is optimal

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:

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

Figure 5 · The blocking argument — an ancestor $n$ of $A$ stands in front of $B$ in the queue
… already expanded nAB on the frontier, on an optimal path to $A$ optimal goal $g(A) = C^*$ suboptimal goal $g(B) > C^*$ rest of the optimal path 1. $f(n) \le f(A)$ 2. $f(A) < f(B)$ 3. $\Rightarrow f(n) < f(B)$, so $n$ pops before $B$
$n$ is any node on the frontier that lies on an optimal path to $A$ — possibly the start state, possibly $A$ itself. The argument shows $n$ always jumps the queue ahead of $B$, so $B$ can never be popped while any such $n$ is waiting. $B$ is blocked.

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:

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)$

Proof — step 1

$$ \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.

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)$

Proof — step 2

$$ \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$

Proof — step 3 and the conclusion

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$

Common mistake — three, actually
  1. 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.
  2. 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^*$.
  3. 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.
In step 1, where exactly is admissibility used?
Why: admissibility is precisely the statement $h(n) \le h^*(n)$, which is what licenses that one inequality. Everything else is definitions and the assumption that $B$ is suboptimal. That is why an inadmissible heuristic kills the theorem instantly: without that line, $f(n)$ can exceed $f(A)$, the relay in front of $B$ collapses, and Figure 4 happens. (Option B is true, but it is an consequence of admissibility at goals, used in the last line, not the inequality that does the work.)

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:

One word changed, and the optimality theorem broke:

Figure 6 · “A* Graph Search Gone Wrong” — an admissible $h$ that graph search still ruins
1 1 3 3 SACG h=2 h=4 h=1 h=0
Check admissibility first: $h^*(S)=5$ (via $A$), $h^*(A)=4$, $h^*(C)=3$, $h^*(G)=0$. Against $h = 2,4,1,0$: every one is $\le$ the truth, so $h$ is admissible. Tree search gets 5; graph search gets 6.

The two traces, side by side

#A* tree searchA* graph search (explored set)
1Frontier: 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.
2Pop 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.
3Pop 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.
4Pop S-A-C ($f=3$). Push S-A-C-G (5+0)=5.Frontier holds only S-C-G:6. Pop it — goal.
5Pop 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.

Key idea

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.

Poll from the slides — in Figure 6, which paths does A* graph search ever place on the frontier?
Why: $S$ goes on first; expanding it pushes 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.
A slip on the slide, so it does not confuse you

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.

Definition — consistent (monotone) heuristic

$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).

Analogy

$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

Proof — consistency $\Rightarrow$ $f$ is non-decreasing along any 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:

Consequence 2 — A* graph search is optimal when $h$ is consistent

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

Proof — every consistent heuristic is admissible

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.

Intuition — why the implication goes that way

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.

Guided problem 2Is this $h$ admissible? Is it consistent? Name the violating edge

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$
cost141127
State$S$$A$$B$$D$$G$
$h$43320

(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$43320
$h^*$43320
$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)$costOK?
$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 searchGraph search (explored set)
A*optimal if $h$ is admissibleoptimal if $h$ is consistent
UCSoptimal — the special case $h \equiv 0$, and $0 \le h^*$ alwaysoptimal — and $h \equiv 0$ is consistent, since $0 - 0 = 0 \le \text{cost}$ for every edge
Greedynever guaranteednever guaranteed

Four things to carry away:

  1. 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.
  2. The requirement gets stronger when you add the explored set. Graph search buys memory and time, and pays with a stronger obligation on $h$.
  3. 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.
  4. 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.
Example — why relaxation tends to hand you consistency for free

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:

Common mistake — comparing $f$ when you should compare $g$

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

Figure 7 · Cost contours — UCS expands circles around the start, A* expands ellipses stretched toward the goal
Start Goal Start Goal Uniform-cost: contours of $g$ A*: contours of $f = g + h$ expands equally in all “directions”; everything within radius $C^*$ of the start expands mainly toward the goal, but still hedges its bets sideways to guarantee optimality
These are not a cartoon — they are the exact level sets. UCS expands every node with $g \le C^*$: a ball around the start. A* expands every node with $f = g+h < C^*$; with a perfect Euclidean $h$ that set is $\{p : d(\text{start},p) + d(p,\text{goal}) \le C^*\}$, which is the textbook definition of an ellipse with foci at the start and the goal. The bolder outer curve in each panel is the $C^*$ contour, the boundary of what must be expanded.

The heuristic's quality is now a geometric parameter:

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:

The recipe — relaxation

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:

The 8-puzzle as a formal search problem
Figure 8 · The 8-puzzle: per-tile Manhattan distances summing to $h_{\text{MANHATTAN}} = 18$
Start state Goal state 724 56 831 312 23 223 small orange = that tile's Manhattan distance 12 345 678 → $h_{\text{TILES}} = 8$ $h_{\text{MAN}} = 18$ $3+1+2+2+2+3+3+2 = 18$  (tiles in order $1,2,\dots,8$)
Both boards are the standard Russell & Norvig pair. The blank does not contribute to either heuristic — it is not a tile, and counting it would break admissibility on some instances.

8-puzzle heuristic I — misplaced tiles

$h_{\text{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}}$

$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

AlgorithmAverage nodes expanded when the optimal path has…
…4 steps…8 steps…12 steps
UCS1126,300$3.6 \times 10^6$
A*TILES1339227
A*MANHATTAN122573

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.

Intuition — what “better” means quantitatively

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

Guided problem 3Design a heuristic by relaxation, for a problem you have not seen

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

Definition — dominance

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:

Why larger is better

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:

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

The max trick

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.

Proof

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.

Common mistake — averaging, or adding

$\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.

$h_1$ and $h_2$ are admissible. Which of these is guaranteed admissible and at least as informed as both?
Why: the max never exceeds $h^*$ (each argument is capped by $h^*$) and by definition sits above both inputs. The sum can shoot past $h^*$ and destroy optimality. The min is admissible but is dominated by both inputs — it is the worst legal combination, and it always makes A* do more work. The average is admissible but dominated by the max. When in doubt: push $h$ as high as you can without passing $h^*$.
Your A* graph search with an admissible-but-inconsistent $h$ returns a suboptimal path. Which single change is guaranteed to fix optimality without changing $h$?
Why: §8's theorem needs only admissibility — but it is a theorem about tree search. Removing the explored set restores exactly the hypotheses of that proof, at the cost of re-expanding repeated states (and risking non-termination on infinite graphs with cycles). Testing goals at generation makes things strictly worse, and is how you lose optimality even with a perfect heuristic. Tie-breaking changes which optimal answer you get, never whether you get one. Adding a constant to $h$ breaks $h(\text{goal}) = 0$ and can push $h$ above $h^*$, i.e. it can make things inadmissible. The other legal fix is to allow reopening closed nodes — but that is a change to the algorithm, and it is not in the slides' pseudocode.

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$.