Nexus Learn

Lecture 14: A* Search and Designing Heuristics

Lecture Notes CS-511 Artificial Intelligence 7 min read Updated 8 Nov 2026

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. A* Search
  3. 2. Why A* Is Optimal
  4. 3. Designing Heuristics
  5. 4. Common Mistakes
  6. Word Bank
  7. Self-Check Questions

Learning Outcomes

SLO

After this lecture you will be able to...

CLO

Bloom

SLO 14.1

Trace A* search using f(n) = g(n) + h(n) and a priority queue

CLO3

C3 Apply

SLO 14.2

Explain admissible and consistent heuristics and why they make A* optimal

CLO3

C2 Understand

SLO 14.3

Compare 8-puzzle heuristics by nodes expanded and design a heuristic from a relaxed problem

CLO3

C3 Apply

§1.1 The idea: f(n) = g(n) + h(n)

Slide L14-S06

A* (A-star) is best-first search with f(n) = g(n) + h(n): the cost already paid to reach n plus the estimated cost still to go, i.e. the estimated cost of the whole trip through n. Example: on a bus from Burewala to Lahore you have paid Rs 200 at Arifwala (g) and expect about Rs 700 more (h), so the trip through Arifwala costs about Rs 900 (f). A* uses the same loop as greedy search (best_first in searchlib.py); only f changes. The goal test is done when a node is POPPED.

Algorithm

f(n)

Optimal?

Guided?

Uniform-cost (L11)

g(n)

Yes

No (blind)

Greedy best-first (L13)

h(n)

No

Yes

A* (L14)

g(n) + h(n)

Yes (admissible h)

Yes

§1.2 Trace: Burewala to Lahore

Slide L14-S07

1. expand Burewala (g=0) -> frontier: Arifwala(55+186=241), Mian Channu(50+227=277), Vehari(35+254=289)

2. expand Arifwala (g=55) -> frontier: Pakpattan(90+162=252), Sahiwal(105+155=260), Mian Channu(50+227=277), Vehari(35+254=289)

3. expand Pakpattan (g=90) -> frontier: Sahiwal(105+155=260), Mian Channu(50+227=277), Vehari(35+254=289)

4. expand Sahiwal (g=105) -> frontier: Lahore(275+0=275), Mian Channu(50+227=277), Vehari(35+254=289)

5. pop Lahore (g=275, f=275) - GOAL

A* path: Burewala - Arifwala - Sahiwal - Lahore | 275 km | 4 cities expanded

Each frontier entry shows g + h = f. Pakpattan (f = 252) is expanded before Sahiwal (260) because its straight-line distance made it look promising; it is a dead end. Lahore enters with f = 275, which is smaller than every other f, so it is popped. A* expanded 4 cities; UCS expanded 9 for the same 275 km route.

§1.3 A* fixes the greedy trap

Slide L14-S08

1. expand Home (g=0) -> frontier: Canal Road(1+3=4), Bypass(4+4=8)

2. expand Canal Road (g=1) -> frontier: Bypass(4+4=8), College(11+0=11)

3. expand Bypass (g=4) -> frontier: Market(6+2=8), College(11+0=11)

4. expand Market (g=6) -> frontier: College(9+0=9)

5. pop College (g=9, f=9) - GOAL

A*: Home - Bypass - Market - College = 9 min

With h(Bypass) = 9 (overestimate, true 5): Home - Canal Road - College = 11 min

After Canal Road, College is in the frontier with f = 11, but Bypass (8) and Market (8) are cheaper, so A* keeps going and finds the 9-minute route. If the goal were tested when it is first generated, A* would wrongly return 11 minutes.

2. Why A* Is Optimal

§2.1 Admissible heuristics

Slide L14-S10

A heuristic is ADMISSIBLE if h(n) ≤ h*(n) for every node n, where h*(n) is the true cheapest cost from n to the goal. It never overestimates (it is optimistic). Theorem: A* tree search with an admissible heuristic always returns an optimal solution. Straight-line distance and the 8-puzzle heuristics h1 and h2 are admissible.

town h true h* admissible consistent

Sahiwal 155 170 yes yes

Pakpattan 162 255 yes yes

Arifwala 186 220 yes yes

Burewala 223 275 yes yes

Mian Channu 227 325 yes yes

Vehari 254 310 yes yes

Khanewal 270 380 yes yes

Mailsi 285 355 yes yes

Multan 314 340 yes yes

If h overestimates, A* can be fooled: with h(Bypass) = 9 (true cost 5) A* returned the 11-minute route (§1.3).

§2.2 Consistent heuristics and properties

Slide L14-S11

A heuristic is CONSISTENT (monotone) if h(n) ≤ c(n, n') + h(n') for every action from n to n' - a triangle rule. Example: h(Burewala) = 223 ≤ 55 + h(Arifwala) = 55 + 186 = 241. Every consistent heuristic is admissible. With a consistent h the value of f never decreases along a path, so the first time A* pops a node it has found the cheapest route to it; graph search with a closed set is then optimal.

Property

A* with a consistent (or admissible) h

Complete

Yes (positive step costs, finite b)

Optimal

Yes

Optimally efficient

No other optimal algorithm using the same h expands fewer nodes

Time and space

Exponential in the worst case; all nodes kept in memory

Memory-saving versions

IDA* (iterative deepening A*), SMA*

3. Designing Heuristics

§3.1 A* on the 8-puzzle: h1 vs h2

Slide L14-S13

def puzzle_search(start, h=None, kind="astar", max_nodes=500000):

"""Best-first search on the 8-puzzle. kind = 'astar' (f = g + h), 'greedy' (f = h) or 'ucs' (f = g).

Returns moves list, nodes expanded, largest frontier."""

pri = {"astar": lambda g, s: g + h(s), "greedy": lambda g, s: h(s), "ucs": lambda g, s: g}[kind]

tie = 0

frontier, parent, gbest, closed, expanded, biggest = [(pri(0, start), 0, tie, start)], {start: None}, {start: 0}, set(), 0, 1

while frontier:

_, g, _, s = heapq.heappop(frontier)

if s in closed: continue

if s == GOAL:

plan = []

while parent[s]: s, a = parent[s]; plan.append(a)

return plan[::-1], expanded, biggest

closed.add(s); expanded += 1

for name, n in moves(s):

if n in closed: continue

if g + 1 < gbest.get(n, 1e18):

gbest[n] = g + 1; parent[n] = (s, name); tie += 1

heapq.heappush(frontier, (pri(g + 1, n), g + 1, tie, n))

biggest = max(biggest, len(frontier))

if expanded >= max_nodes: return None, expanded, biggest

return None, expanded, biggest

start moves UCS nodes A* h1 A* h2

_ 2 3 / 1 4 5 / 7 8 6 4 22 4 4

8 1 3 / 4 _ 2 / 7 6 5 14 5,960 301 76

7 2 4 / 5 _ 6 / 8 3 1 20 55,408 3,666 282

8 6 7 / 2 5 4 / 3 _ 1 31 181,438 143,848 21,197

All three searches find the same optimal number of moves, but A* with h2 expands far fewer nodes (282 instead of 55,408 on the 20-move board). h2 DOMINATES h1: h2(n) ≥ h1(n) for every board, and both are admissible, so A* with h2 never expands more nodes. Rule: use the largest admissible heuristic that is cheap to compute.

§3.2 Relaxed problems

Slide L14-S14

A relaxed problem has fewer rules than the real one. Its exact solution cost is an admissible heuristic for the real problem, because removing rules can only make the problem easier.

Real rule

Relaxed rule

Heuristic

Tile moves to an adjacent blank square

Tile can jump to any square

h1: misplaced tiles

Tile moves to an adjacent blank square

Tile can slide to any adjacent square

h2: Manhattan distance

Car must follow roads

Fly in a straight line

Straight-line distance

If several admissible heuristics are available, h(n) = max(h1(n), h2(n), ...) is admissible and dominates each of them.

§3.3 UCS, greedy and A* compared

Slide L14-S15

from UCS km/exp greedy km/exp A* km/exp

Burewala 275/9 275/3 275/4

Vehari 310/9 310/4 310/5

Khanewal 380/9 380/5 380/7

Mailsi 355/9 355/5 355/6

km found / cities expanded before Lahore was popped. Greedy expands the fewest here, but it is not guaranteed to be optimal (Lecture 13). A* is both optimal and well guided.

4. Common Mistakes

Wrong idea

Correct idea

A* sorts by h only.

A* uses f = g + h.

Stop when the goal is generated.

Stop when the goal is popped.

Any heuristic keeps A* optimal.

Only an admissible one.

A bigger heuristic is always better.

Only while it stays admissible and cheap.

A* needs little memory.

It keeps every node.

Word Bank

Word

Simple meaning

Example

Admissible

Never overestimates the true cost

186 ≤ 220 at Arifwala

Consistent (monotone)

Obeys the triangle rule on every road

223 ≤ 55 + 186

Dominate

Always at least as large

h2 ≥ h1

Relaxed problem

The same problem with fewer rules

Tiles that can fly

Optimally efficient

No optimal rival expands fewer nodes

A* with the same h

IDA*

Iterative deepening A*, small memory

Limits on f instead of depth

Self-Check Questions

  1. Write f(n) for UCS, greedy and A*. (see §1.1)
  2. Trace A* from Vehari to Lahore. (see §1.2)
  3. Why must the goal be tested when popped? (see §1.3)
  4. Define admissible; is 2 x straight-line distance admissible? (see §2.1)
  5. Define consistent and check it on one road. (see §2.2)
  6. Why is h2 better than h1? Where does h2 come from? (see §3.1-3.2)

Reading: Russell & Norvig (4e) 3.5.2-3.5.5, 3.6; Luger (6e) 4.2-4.3.

← All CS-511 material