Nexus Learn

Lecture 14: A* Search and Designing Heuristics

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

↓ Download slides (PowerPoint)

Recap of Lecture 13 and Homework Check

LAST LECTURE · 5 MIN

Today: UCS uses only g (optimal, slow). Greedy uses only h (fast, not optimal). What if we use both?

Today's Plan (50 minutes)

ROADMAP

0-5

Recap of Lecture 13 and homework check

S02

5-8

Plan and learning outcomes

S03-S04

8-20

Module 1: A* = g + h; trace on the road map; greedy fixed

S05-S08

20-28

Module 2: Why A* is optimal - admissible and consistent h

S09-S11

28-35

Module 3: Designing heuristics for the 8-puzzle

S12-S15

35-43

Class activity: A* from Khanewal to Lahore

S16

43-48

Common mistakes and recap quiz

S17-S18

48-50

Homework and next lecture

S19

Learning Outcomes (SLOs)

BY THE END OF THIS LECTURE YOU WILL BE ABLE TO

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

  • Course Learning Outcome(s) served
  • CLO3: Apply various techniques and algorithms of AI used in general problem solving, optimization problems, constraint satisfaction problems, and game programming.

Slide 5

MODULE 1

A* Search

f(n) = g(n) + h(n): cost paid so far + estimated cost still to go

A*: Count What You Paid and What Is Left

MODULE 1 · IDEA

g(n): paid so far

h(n): estimate to go

f(n) = g(n) + h(n)

Cost from the start to n. UCS uses only this - safe but blind.

Estimated cost from n to the goal. Greedy uses only this - fast but fooled.

Estimated cost of the whole trip through n. A* expands the smallest f.

Bus example: at Arifwala you have paid Rs 200 (g); the conductor says about Rs 700 more (h); the trip through Arifwala is about Rs 900 (f).

Same loop: best_first() from L13 with f = g + h. Goal test when a node is POPPED, not when it is generated.

A* Trace: Burewala to Lahore

MODULE 1 · TRACE

Lahore

Result: A*: 275 km after 4 cities expanded (UCS: 9). Pakpattan was checked because its f = 252 was the smallest.

Sahiwal

Khanewal

Multan

Mian Channu

Burewala

Arifwala

Pakpattan

Vehari

Mailsi

REAL OUTPUT: L14/ASTAR_TRACE.PY (G + H = F)

  • 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

A* Fixes the Greedy Trap

MODULE 1 · TRIP TO COLLEGE

REAL OUTPUT: L14/TOWN.PY

  • 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

Greedy (L13)

Home - Canal Road - College = 11 min.

A* (today)

Home - Bypass - Market - College = 9 min - the best.

Remember: College(11) sat in the frontier but was NOT popped while Bypass (8) and Market (8) were cheaper. Test the goal on POP.

Slide 9

MODULE 2

Why A* Is Optimal

The estimate must never be too high

Admissible Heuristics: Never Overestimate

MODULE 2 · ADMISSIBLE

REAL OUTPUT: L14/ADMISSIBLE.PY

  • 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

Admissible

h(n) ≤ h*(n) for every node: the estimate is never more than the true cheapest cost.

Overestimate = danger

h(Bypass) = 9 when the truth is 5: A* returned 11 min instead of 9.

Theorem: with an admissible h, A* (tree search) always finds an optimal solution.

Consistency and the Properties of A*

MODULE 2 · CONSISTENT

Consistent: h(n) ≤ c(n, n') + h(n') for every road. Example: h(Burewala) 223 ≤ 55 + h(Arifwala) 186 = 241.

Slide 12

MODULE 3

Designing Heuristics

A better heuristic = fewer nodes, same optimal answer

A* on the 8-Puzzle: h1 vs h2

MODULE 3 · EVIDENCE

REAL OUTPUT: L14/PUZZLE_COMPARE.PY (OPTIMAL MOVES, NODES EXPANDED)

  • 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

Same answer

Far fewer nodes

Dominance

UCS, A* h1 and A* h2 all find the optimal number of moves.

20-move board: 55,408 (UCS), 3,666 (h1), 282 (h2).

h2 ≥ h1 on every board and both are admissible, so h2 is never worse.

Where Do Heuristics Come From? Relaxed Problems

MODULE 3 · DESIGN

Why admissible?

Combine

Trade-off

Fewer rules can only make a problem easier, so its cost is never more than the real cost.

max(h1, h2, ...) of admissible heuristics is admissible and at least as good as each.

A better h saves nodes but must stay cheap to compute for every node.

UCS, Greedy and A* Side by Side

MODULE 3 · SUMMARY

REAL OUTPUT: L14/COMPARE_MAP.PY (KM / CITIES EXPANDED)

  • 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

Use: A* when you need the best answer and have a good admissible h (map apps, game path-finding, the 8-puzzle in Assignment 2).

Activity: A* from Khanewal to Lahore

CLASS ACTIVITY · PAIRS · 8 MIN

Pairs, 6 min + 2 min. Use the road map and the h table from Lecture 13 (S07). Write every frontier entry as g + h = f.

Is h(Mian Channu) = 227 admissible? (Its true cost to Lahore is 325 km.)

Continue A* until Lahore is POPPED. Write each expansion.

Check consistency on the road Khanewal-Mian Channu (55 km).

Lahore enters the frontier twice. With which f values, and which one is popped?

Expand Khanewal: write the frontier with g + h = f.

Give the final route and its km.

Common Mistakes

WATCH OUT

WRONG IDEA

CORRECT IDEA

✗ A* sorts the frontier by h only.

✓ A* uses f = g + h. f = h is greedy.

✗ Stop as soon as the goal is generated.

✓ Stop when the goal is POPPED: College(11) was not the best (9).

✗ Any heuristic keeps A* optimal.

✓ Only an admissible one: h(Bypass) = 9 gave 11 min.

✗ A bigger heuristic is always better.

✓ Bigger only while it stays admissible (and cheap).

✗ A* needs little memory.

✓ It keeps every node; IDA* and SMA* save memory.

Recap Quiz

CHECK YOUR UNDERSTANDING · 3 MIN

  • Q1. A* expands the node with the smallest:
  • (a) g(n) (b) h(n) (c) g(n) + h(n) (d) Depth
  • Q2. A heuristic is admissible if it:
  • (a) Never overestimates (b) Never underestimates (c) Is always 0 (d) Is exact
  • Q3. A* with h(n) = 0 for all n behaves like:
  • (a) DFS (b) Greedy search (c) Uniform-cost search (d) Hill climbing
  • Q4. On the 20-move 8-puzzle, A* with h2 expanded 282 nodes, h1 3,666. This is because h2:
  • (a) Overestimates (b) Dominates h1 (c) Is greedy (d) Ignores g

Homework and Next Lecture

BEFORE THE NEXT CLASS

  • Task 1: Run A* by hand from Vehari to Lahore. Write g + h = f for every frontier entry after each expansion, and the route with its km.
  • Task 2: Is h(n) = 2 x straight-line distance admissible on our map? Show one town where it fails.
  • Task 3: Run A* from Vehari with h = 0 for every town and compare its expansion order with UCS. What do you notice?
  • Assignment 2: Solve 8-puzzles with A* using h1 and h2; report moves and nodes expanded.

NEXT LECTURE

  • Lecture 15:
  • Hill Climbing and Beam Search

← All CS-511 material