Learning Outcomes
SLO | After this lecture you will be able to... | CLO | Bloom |
|---|---|---|---|
SLO 11.1 | Trace breadth-first search, writing the frontier (FIFO queue) and explored set at each step | CLO3 | C3 Apply |
SLO 11.2 | Implement BFS in Python for a graph and for the 8-puzzle | CLO3 | C3 Apply |
SLO 11.3 | Evaluate BFS with the four properties and explain when uniform-cost search is needed | CLO3 | C2 Understand |
1. The Idea
§1.1 Level by level
Slide L11-S06
Breadth-first search (BFS) expands the shallowest unexpanded node first: all nodes 1 step from the start, then all nodes 2 steps away, and so on - like ripples in a pond. Its frontier is a FIFO queue (first in, first out), like the token queue at a NADRA office: new children join the back, the oldest node is taken from the front. Because it goes level by level, the first path it finds to the goal has the fewest steps.
§1.2 The algorithm
Slide L11-S07
"""Shared search code for L11-L14 (plain Python). Graph search with a pluggable frontier, plus the 8-puzzle.""" import heapq, os, sys from collections import deque sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "l10")) from roadmap import GRAPH, ROADS # the L10 road map (approximate km) def path_cost(path): km = {(a, b): d for a, b, d in ROADS}; km.update({(b, a): d for a, b, d in ROADS}) return sum(km[p] for p in zip(path, path[1:])) def bfs(graph, start, goal, trace=False): """Breadth-first GRAPH search; goal test when a node is generated (early test).""" if start == goal: return [start], 0, [] frontier, reached, steps, expanded = deque([[start]]), {start}, [], 0 while frontier: path = frontier.popleft(); node = path[-1]; expanded += 1 new = [] for nxt in (n for n, _ in graph[node]): if nxt in reached: continue reached.add(nxt); child = path + [nxt]; new.append(nxt) if nxt == goal: steps.append((node, new, [p[-1] for p in frontier])) return child, expanded, steps frontier.append(child) steps.append((node, new, [p[-1] for p in frontier])) return None, expanded, steps |
- reached: a set of states already added, so no state enters the queue twice (graph search).
- Early goal test: BFS checks a child when it is generated; this saves a whole level of work.
2. BFS on the Road Map
§2.1 Step-by-step trace
Slide L11-S09
The L10 road map (approximate km). Real output:
step expand new children frontier (front ... back) 1 Burewala Vehari, Arifwala, Mian Channu Vehari, Arifwala, Mian Channu 2 Vehari Mailsi, Multan Arifwala, Mian Channu, Mailsi, Multan 3 Arifwala Pakpattan, Sahiwal Mian Channu, Mailsi, Multan, Pakpattan, Sahiwal 4 Mian Channu Khanewal Mailsi, Multan, Pakpattan, Sahiwal, Khanewal 5 Mailsi - Multan, Pakpattan, Sahiwal, Khanewal 6 Multan Lahore Pakpattan, Sahiwal, Khanewal Lahore generated -> stop. BFS path: Burewala -> Vehari -> Multan -> Lahore | 3 roads | 475 km | nodes expanded: 6 |
§2.2 The search tree by levels
Slide L11-S10
Level | Nodes | Parent |
|---|---|---|
0 | Burewala | - |
1 | Vehari, Arifwala, Mian Channu | Burewala |
2 | Mailsi, Multan | Pakpattan, Sahiwal | Khanewal | Vehari | Arifwala | Mian Channu |
3 | Lahore (goal) | Multan |
BFS returned Burewala - Vehari - Multan - Lahore: 3 roads (the fewest) but 475 km. Burewala - Arifwala - Sahiwal - Lahore also has 3 roads (275 km) but Vehari was ahead of Arifwala in the queue.
3. How Good Is BFS?
§3.1 The four properties
Slide L11-S12
Property | BFS | Reason |
|---|---|---|
Complete | Yes, if b is finite | Every level is checked |
Optimal | Only if all step costs are equal | It minimises steps, not cost |
Time | O(b^d) | b + b² + ... + b^d nodes generated |
Space | O(b^d) | Every generated node is kept - the main weakness |
§3.2 BFS on the 8-puzzle
Slide L11-S13
1 2 3 / 4 _ 6 / 7 5 8 depth 2 expanded 3 largest frontier 5 moves: Down Right 1 2 3 / 4 _ 5 / 7 8 6 depth 2 expanded 5 largest frontier 7 moves: Right Down _ 2 3 / 1 4 5 / 7 8 6 depth 4 expanded 11 largest frontier 11 moves: Down Right Right Down 8 1 3 / 4 _ 2 / 7 6 5 depth 14 expanded 3,633 largest frontier 2,327 7 2 4 / 5 _ 6 / 8 3 1 depth 20 expanded 39,278 largest frontier 16,130 |
The number of expanded nodes grows very fast with the solution depth (3 at depth 2, 39,278 at depth 20), and the frontier holds 16,130 nodes at once. The 15-puzzle (about 10 trillion states) is out of reach for BFS.
§3.3 Uniform-cost search
Slide L11-S14
Uniform-cost search (UCS) expands the node with the lowest path cost g(n), using a priority queue (Python heapq). It tests the goal when a node is expanded. It is complete and optimal for positive step costs; with equal step costs it behaves like BFS.
BFS: Burewala -> Vehari -> Multan -> Lahore (3 roads, 475 km) UCS: Burewala -> Arifwala -> Sahiwal -> Lahore (3 roads, 275 km) UCS expansion order (city, g = km so far): Burewala 0, Vehari 35, Mian Channu 50, Arifwala 55, Mailsi 80, Pakpattan 90, Sahiwal 105, Khanewal 105, Multan 135, Lahore 275 |
4. Common Mistakes
Wrong idea | Correct idea |
|---|---|
BFS uses a stack. | BFS uses a FIFO queue. |
BFS finds the shortest distance. | BFS finds the fewest steps; UCS finds the lowest cost. |
BFS needs little memory. | BFS stores O(b^d) nodes. |
UCS tests the goal when generating. | UCS tests when expanding. |
Word Bank
Word | Simple meaning | Example |
|---|---|---|
FIFO queue | First in, first out | Token queue at a bank |
Shallowest node | The node with the fewest steps from the start | Level-1 cities |
Early goal test | Test a node when it is generated | Lahore found while expanding Multan |
Uniform-cost search | Expand the cheapest path first | 275 km route |
Priority queue | Always gives the smallest item first | heapq |
Self-Check Questions
- Why does BFS use a FIFO queue? (see §1.1)
- Trace BFS from Burewala to Lahore. (see §2.1)
- Why did BFS return the 475 km route? (see §2.2)
- Write the four properties of BFS. (see §3.1)
- Why is memory the main problem of BFS? (see §3.2)
- When is UCS better than BFS? (see §3.3)
Reading: Russell & Norvig (4e) 3.4.1-3.4.2; Luger (6e) 3.2.3.