Learning Outcomes
SLO | After this lecture you will be able to... | CLO | Bloom |
|---|---|---|---|
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 |
1. Heuristics
§1.1 From blind search to informed search
Slide L13-S06
The searches of Lectures 10-12 (BFS, UCS, DFS, IDDFS) are uninformed (blind): they know the states, actions, step costs and goal test, but nothing about where the goal is. An informed (heuristic) search also uses a heuristic function h(n) = the ESTIMATED cost of the cheapest path from node n to a goal. A heuristic (a rule of thumb, a smart guess) must be cheap to calculate; it does not have to be exact. Always h(goal) = 0.
Symbol | Meaning | Example (Arifwala, Burewala to Lahore) |
|---|---|---|
g(n) | Cost already paid from the start to n | 55 km |
h(n) | Estimated cost from n to the goal | 186 km straight line |
f(n) | Evaluation function used to pick the next node | f = h for greedy |
§1.2 Straight-line distance for the road map
Slide L13-S07
For route finding, h_SLD(n) = straight-line (air) distance from town n to Lahore. A road can never be shorter than the straight line, so this heuristic never overestimates the real cost.
Town | Lahore | Sahiwal | Pakpattan | Arifwala | Burewala | Mian Channu | Vehari | Khanewal | Mailsi | Multan |
|---|---|---|---|---|---|---|---|---|---|---|
h (km) | 0 | 155 | 162 | 186 | 223 | 227 | 254 | 270 | 285 | 314 |
§1.3 Two heuristics for the 8-puzzle
Slide L13-S08
- h1 - misplaced tiles: the number of tiles (1 to 8) not in their goal square. The blank is not counted.
- h2 - Manhattan distance: for every tile, the number of rows plus the number of columns between its square and its goal square, added together. Tiles move only up, down, left and right - like walking along the streets of a planned colony.
def h_misplaced(s): """h1: number of tiles not in their goal square (the blank is not counted).""" return sum(1 for i, ch in enumerate(s) if ch != "0" and ch != GOAL[i]) def h_manhattan(s): """h2: sum over tiles of |row - goal row| + |col - goal col|.""" tot = 0 for i, ch in enumerate(s): if ch == "0": continue j = GOAL.index(ch); tot += abs(i // 3 - j // 3) + abs(i % 3 - j % 3) return tot |
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 |
Both values are below the true cost (14 moves): every misplaced tile needs at least one move, and at least as many moves as its Manhattan distance. h2 is closer to the truth, so it is the better guide.
2. Best-First Search
§2.1 One loop, many algorithms
Slide L13-S10
Best-first search keeps the frontier in a priority queue (Python heapq) and always expands the node with the smallest value of an evaluation function f(n) - like a hospital emergency, where the most serious patient is seen first. The goal test is done when a node is popped.
f(n) | Algorithm | Lecture |
|---|---|---|
g(n) | Uniform-cost search | L11 |
h(n) | Greedy best-first search | L13 |
g(n) + h(n) | A* search | L14 |
def best_first(graph, start, goal, f): """Generic best-first GRAPH search. f(g, node) gives the priority; goal test when a node is EXPANDED. Returns path, cost, steps = [(node, g, f, frontier [(city, g, f)] after expansion)].""" frontier, best, steps, closed = [(f(0, start), 0, [start])], {start: 0}, [], set() while frontier: fv, g, path = heapq.heappop(frontier); node = path[-1] if node in closed: continue if node == goal: steps.append((node, g, fv, None)); return path, g, steps closed.add(node) for nxt, km in graph[node]: g2 = g + km if nxt not in closed and g2 < best.get(nxt, 1e18): best[nxt] = g2; heapq.heappush(frontier, (f(g2, nxt), g2, path + [nxt])) live = {} for fv2, g2, p in sorted(frontier): if p[-1] not in closed and p[-1] not in live: live[p[-1]] = (g2, fv2) steps.append((node, g, fv, [(c, gv, fv2) for c, (gv, fv2) in live.items()])) return None, None, steps def greedy(graph, start, goal, h): return best_first(graph, start, goal, lambda g, n: h[n]) def astar(graph, start, goal, h): return best_first(graph, start, goal, lambda g, n: g + h[n]) |
§2.2 Greedy trace: Burewala to Lahore
Slide L13-S11
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 |
Greedy expanded only 3 cities and found the best route (275 km). UCS expanded 9 cities for the same route. Here greedy was lucky; section 3 shows that it is not always right.
3. Where Greedy Goes Wrong
§3.1 Greedy on the 8-puzzle
Slide L13-S13
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 |
Greedy is very fast (332 nodes instead of 5,960) but its solutions are long (36 moves where 14 were enough). It looks only forward (h) and never counts the cost already paid (g). A better heuristic does not cure this; adding g does (A*, Lecture 14).
§3.2 Properties
Slide L13-S14
Property | Greedy best-first | Reason |
|---|---|---|
Complete | Yes in finite spaces (graph search); no in tree search | Can go back and forth between two close-looking states |
Optimal | No | Ignores g(n) |
Time | O(b^m) worst case | A good h makes it very fast in practice |
Space | O(b^m) worst case | Keeps all generated nodes |
4. Worked Exercise: Trip to College
Roads (minutes): Home-Canal Road 1, Home-Bypass 4, Canal Road-College 10, Bypass-Market 2, Market-College 3. h: Home 4, Canal Road 3, Bypass 4, Market 2, College 0. (Class activity, slide L13-S15.) Try it yourself first, then check:
1. expand Home (g=0) -> frontier: Canal Road(3), Bypass(4) 2. expand Canal Road (g=1) -> frontier: College(0), Bypass(4) 3. pop College (g=11, f=0) - GOAL Greedy: Home - Canal Road - College = 11 min Best route (UCS): Home - Bypass - Market - College = 9 min |
Greedy chose Canal Road because it LOOKED close (h = 3), but its road to College takes 10 minutes. Keep this example: in Lecture 14, A* finds the 9-minute route.
5. Common Mistakes
Wrong idea | Correct idea |
|---|---|
A heuristic gives the exact distance. | It is an estimate (h1 = 5, true 14). |
Greedy finds the shortest path. | No: 36 moves where 14 were enough. |
Count the blank in h1. | Only tiles 1 to 8. |
Small h means the path so far was cheap. | h looks forward; cost so far is g. |
Best-first is one algorithm. | A family: f = g, f = h, f = g + h. |
Word Bank
Word | Simple meaning | Example |
|---|---|---|
Heuristic | A smart guess / rule of thumb | Road sign 'Lahore 186 km' |
Informed search | Search that uses h(n) | Greedy, A* |
Straight-line distance | Distance 'as the crow flies' | Arifwala-Lahore 186 km |
Manhattan distance | Rows + columns to travel | Tile 8: 2 rows + 1 column = 3 |
Priority queue | Queue where the smallest value leaves first | Hospital emergency |
Greedy | Always grab what looks best now | Canal Road in the activity |
Self-Check Questions
- What is the difference between g(n) and h(n)? (see §1.1)
- Why does straight-line distance never overestimate? (see §1.2)
- Calculate h1 and h2 for 7 2 4 / 5 _ 6 / 8 3 1. (see §1.3)
- Which f(n) gives UCS, greedy and A*? (see §2.1)
- Trace greedy from Burewala to Lahore. (see §2.2)
- Why is greedy not optimal? Give numbers. (see §3.1)
Reading: Russell & Norvig (4e) 3.5.1, 3.6.1-3.6.2; Luger (6e) 4.1-4.2.