Recap of Lecture 12 and Homework Check
LAST LECTURE · 5 MIN
Today: All these searches were blind. UCS looked at 9 cities to reach Lahore. Can a sense of direction help?
Today's Plan (50 minutes)
ROADMAP
0-5
Recap of Lecture 12 and homework check
S02
5-8
Plan and learning outcomes
S03-S04
8-18
Module 1: Heuristics - straight-line distance, misplaced tiles, Manhattan
S05-S08
18-27
Module 2: Best-first search and the greedy trace
S09-S11
27-35
Module 3: Where greedy goes wrong; properties
S12-S14
35-43
Class activity: heuristics and a greedy trace
S15
43-48
Common mistakes and recap quiz
S16-S17
48-50
Homework and next lecture
S18
Learning Outcomes (SLOs)
BY THE END OF THIS LECTURE YOU WILL BE ABLE TO
SLO 13.1
Explain what a heuristic is and calculate h1 (misplaced tiles) and h2 (Manhattan) for an 8-puzzle board
→ CLO3 · C3 Apply
SLO 13.2
Trace greedy best-first search with a priority queue ordered by h(n)
→ CLO3 · C3 Apply
SLO 13.3
Explain why greedy search is fast but not optimal, and compare it with UCS
→ CLO3 · C2 Understand
- 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
Heuristics
A smart guess of how far the goal is
From Blind Search to Informed Search
MODULE 1 · IDEA
Uninformed (blind)
Informed (heuristic)
Road-sign example
BFS, UCS, DFS, IDDFS: no idea where the goal is. Explore in a fixed pattern.
Uses h(n): an estimate of the cost from n to the goal. Moves towards the goal.
Every town has a sign 'Lahore 186 km'. Pick the town whose sign shows the smallest number.
Heuristic for the Road Map: Straight-Line Distance
MODULE 1 · ROAD MAP
Lahore
Sahiwal
Khanewal
Multan
Mian Channu
Burewala
Arifwala
Pakpattan
Vehari
Mailsi
Key fact: a road is never shorter than the straight line - so h never overestimates.
Two Heuristics for the 8-Puzzle
MODULE 1 · 8-PUZZLE
REAL OUTPUT: L13/HEURISTICS.PY
- Board 8 1 3 / 4 _ 2 / 7 6 5 goal 1 2 3 / 4 5 6 / 7 8 _
- tile now(r,c) goal(r,c) misplaced distance
- 1 (0,1) (0,0) yes 1
- 2 (1,2) (0,1) yes 2
- 3 (0,2) (0,2) no 0
- 4 (1,0) (1,0) no 0
- 5 (2,2) (1,1) yes 2
- 6 (2,1) (1,2) yes 2
- 7 (2,0) (2,0) no 0
- 8 (0,0) (2,1) yes 3
- h1 (misplaced tiles) = 5 h2 (Manhattan) = 10 true moves needed = 14
h1 Misplaced tiles
Count tiles not in their goal square. Blank not counted. Here: 5.
h2 Manhattan distance
Add rows + columns each tile must travel. Here: 1+2+0+0+2+2+0+3 = 10.
Compare: h1 = 5 and h2 = 10, true moves = 14. Both underestimate; h2 is closer, so it guides better.
Slide 9
MODULE 2
Best-First Search
Always expand the node that looks best
Best-First Search: One Loop, Many Algorithms
MODULE 2 · FRAMEWORK
BEST-FIRST IN PYTHON (SIMPLIFIED FROM SEARCHLIB.PY)
- def best_first(graph, start, goal, f):
- frontier = [(f(0, start), 0, [start])] # heap
- closed = set()
- while frontier:
- _, g, path = heappop(frontier) # smallest f
- node = path[-1]
- if node in closed: continue
- if node == goal: return path, g
- closed.add(node)
- for nxt, km in graph[node]:
- g2 = g + km
- heappush(frontier,
- (f(g2, nxt), g2, path + [nxt]))
Priority queue: like a hospital emergency - the most serious case is seen first, not the first to arrive.
Greedy Best-First Trace: Burewala to Lahore
MODULE 2 · TRACE
Lahore
Result: Greedy: 3 cities expanded, 275 km. UCS (L11): 9 cities expanded for the same route.
Sahiwal
Khanewal
Multan
Mian Channu
Burewala
Arifwala
Pakpattan
Vehari
Mailsi
REAL OUTPUT: L13/GREEDY_TRACE.PY
- 1. expand Burewala (g=0) -> frontier: Arifwala(186), Mian Channu(227), Vehari(254)
- 2. expand Arifwala (g=55) -> frontier: Sahiwal(155), Pakpattan(162), Mian Channu(227), Vehari(254)
- 3. expand Sahiwal (g=105) -> frontier: Lahore(0), Pakpattan(162), Mian Channu(227), Vehari(254)
- 4. pop Lahore (g=275, f=0) - GOAL
- Greedy path: Burewala - Arifwala - Sahiwal - Lahore | 275 km | 3 cities expanded
- UCS (L11) path: Burewala - Arifwala - Sahiwal - Lahore | 275 km | 9 cities expanded
Slide 12
MODULE 3
Where Greedy Goes Wrong
Looking close is not the same as being cheap
Greedy on the 8-Puzzle: Fast but Long Solutions
MODULE 3 · EVIDENCE
REAL OUTPUT: L13/GREEDY_PUZZLE.PY (MOVES IN THE SOLUTION, NODES EXPANDED)
- start BFS/UCS greedy h1 greedy h2
- moves nodes moves nodes moves nodes
- _ 2 3 / 1 4 5 / 7 8 6 4 22 4 4 4 4
- 8 1 3 / 4 _ 2 / 7 6 5 14 5,960 26 221 36 332
- 7 2 4 / 5 _ 6 / 8 3 1 20 55,408 50 1,543 62 525
Fast
Not optimal
Why?
14-move board: 332 nodes instead of 5,960. 20-move board: 525 instead of 55,408.
Solutions of 36 and 62 moves where 14 and 20 were enough.
f = h only. Greedy never counts the moves it has already made (g).
Properties of Greedy Best-First Search
MODULE 3 · PROPERTIES
Activity: Heuristics and a Greedy Trace
CLASS ACTIVITY · PAIRS · 8 MIN
Pairs, 6 min + 2 min. Trip to college (minutes): Home-Canal Road 1, Home-Bypass 4, Canal Road-College 10, Bypass-Market 2, Market-College 3. Estimates h: Home 4, Canal Road 3, Bypass 4, Market 2, College 0.
8-puzzle board 1 2 3 / 7 6 _ / 5 4 8: calculate h1 (misplaced tiles).
Which route does greedy return? How many minutes?
Same board: calculate h2 (Manhattan distance).
Find the fastest route by hand. How many minutes?
Run greedy best-first from Home to College: write the frontier (with h) after each expansion.
Why was greedy fooled?
Common Mistakes
WATCH OUT
WRONG IDEA
CORRECT IDEA
✗ A heuristic gives the exact distance.
✓ It is an estimate (h1 = 5 when the true answer is 14).
✗ Greedy best-first finds the shortest path.
✓ No: 36 moves where 14 were enough.
✗ Count the blank in misplaced tiles.
✓ Never count the blank - only tiles 1 to 8.
✗ Small h(n) means the path so far was cheap.
✓ h looks forward only; the cost so far is g(n).
✗ Best-first search is one algorithm.
✓ It is a family: f = g (UCS), f = h (greedy), f = g + h (A*).
Recap Quiz
CHECK YOUR UNDERSTANDING · 3 MIN
- Q1. A heuristic h(n) estimates the cost:
- (a) From start to n (b) From n to the goal (c) Of one step (d) Of the whole tree
- Q2. Greedy best-first orders the frontier by:
- (a) g(n) (b) h(n) (c) g(n) + h(n) (d) Depth
- Q3. For 8 1 3 / 4 _ 2 / 7 6 5, h1 (misplaced tiles) is:
- (a) 3 (b) 5 (c) 10 (d) 14
- Q4. Greedy best-first search is:
- (a) Optimal (b) Fast but not optimal (c) Always slower than BFS (d) A FIFO search
Homework and Next Lecture
BEFORE THE NEXT CLASS
- Task 1: Run greedy best-first by hand from Vehari to Lahore (h table, S07). Write the frontier after each expansion and the route with its km.
- Task 2: For the board 7 2 4 / 5 _ 6 / 8 3 1 calculate h1 and h2.
- Task 3: On that board greedy (h2) gave 62 moves after 525 nodes; UCS gave 20 moves after 55,408 nodes. Explain both numbers in 3-4 lines.
- Assignment 2: 8-puzzle with best-first search - see the assignment sheet; bring questions to the next class.
NEXT LECTURE
- Lecture 14:
- A* Search and Designing Heuristics