Recap of Lecture 10 and Homework Check
LAST LECTURE · 5 MIN
Quiz 1: this week, Lectures 1-10, 10 MCQs in 15 minutes.
Today: the first real search algorithm - breadth-first search - on the same road map.
Today's Plan (50 minutes)
ROADMAP
0-5
Recap of Lecture 10 and homework check
S02
5-8
Plan and learning outcomes
S03-S04
8-16
Module 1: The idea of BFS and the algorithm
S05-S07
16-27
Module 2: BFS step by step on the road map
S08-S10
27-35
Module 3: Properties; 8-puzzle; uniform-cost search
S11-S14
35-43
Class activity: BFS on the campus map
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 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
- 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
The Idea of Breadth-First Search
Explore all places 1 road away, then 2 roads away, then 3 ...
BFS: Level by Level with a Queue
MODULE 1 · IDEA
Ripples in a pond
A queue (FIFO)
Fewest steps
Level 0, then all of level 1, then all of level 2 ...
Like the token queue at a NADRA office: first come, first served.
The first path found to the goal has the fewest actions (roads).
The BFS Algorithm
MODULE 1 · ALGORITHM
BFS IN PYTHON (SEARCHLIB.PY)
- def bfs(graph, start, goal):
- frontier = deque([[start]]) # queue of paths
- reached = {start}
- while frontier:
- path = frontier.popleft() # FRONT = oldest
- node = path[-1]
- for nxt in neighbours(node):
- if nxt in reached: continue
- reached.add(nxt)
- if nxt == goal: # early goal test
- return path + [nxt]
- frontier.append(path + [nxt]) # BACK
- return None
FIFO queue
popleft() takes the oldest node; append() adds at the back.
reached set
Never add a city twice - this is graph search (L10).
Early goal test
Check a child when it is added - saves a whole level of work.
Slide 8
MODULE 2
BFS on the Road Map
Burewala to Lahore, one level at a time
BFS Trace: Burewala to Lahore
MODULE 2 · TRACE
Lahore
Result: 3 roads - the fewest possible - but 475 km. Numbers on the map = order of expansion.
Sahiwal
Khanewal
Multan
Mian Channu
Burewala
Arifwala
Pakpattan
Vehari
Mailsi
REAL OUTPUT: L11/BFS_TRACE.PY
- 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
The BFS Search Tree, Level by Level
MODULE 2 · SEARCH TREE
Shallowest first
Ties
Parent pointers
No level-2 node is expanded before ALL level-1 nodes.
Two 3-road routes exist; BFS returns the one whose parent entered the queue first (Vehari).
Each node remembers its parent, so the path is read backwards from the goal.
Slide 11
MODULE 3
How Good Is BFS?
The four properties - and its biggest problem: memory
Properties of Breadth-First Search
MODULE 3 · PROPERTIES
Main weakness: memory. At depth 10 with b = 10 BFS must store about 10¹⁰ nodes (10 TB) - see L10.
BFS on the 8-Puzzle: Watch the Numbers Grow
MODULE 3 · EXPERIMENT
REAL OUTPUT: L11/BFS_PUZZLE.PY
- 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
Growth
Memory
15-puzzle
Depth 2: 3 expansions. Depth 14: 3,633. Depth 20: 39,278.
16,130 nodes waiting in the frontier at the same time.
About 10 trillion states - BFS runs out of memory.
Uniform-Cost Search: BFS for Different Step Costs
MODULE 3 · VARIANT
REAL OUTPUT: L11/UCS.PY
- 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
Activity: BFS on the Campus Map
CLASS ACTIVITY · PAIRS · 8 MIN
Pairs, 6 min + 2 min. Campus paths: Gate-Admin, Gate-Library, Admin-CS Dept, Admin-Cafe, Library-Cafe, Library-Ground, CS Dept-Lab, Cafe-Hostel, Ground-Hostel, Hostel-Lab. Neighbours are added in this order.
Draw the graph.
Which path does BFS return? How many steps?
Run BFS from Gate to Lab: write the queue after each expansion.
Is there another path with the same number of steps?
In which order are the places expanded?
Would BFS find the shortest path in METRES if paths had different lengths?
Common Mistakes
WATCH OUT
WRONG IDEA
CORRECT IDEA
✗ BFS uses a stack.
✓ BFS uses a FIFO queue: take from the front, add at the back.
✗ BFS always finds the shortest distance.
✓ BFS finds the fewest STEPS; for different step costs use UCS.
✗ BFS needs little memory.
✓ BFS stores every generated node: O(b^d) memory.
✗ Add a city every time it is seen.
✓ Keep a reached set; never add a city twice (graph search).
✗ UCS tests the goal when a node is generated.
✓ UCS tests when a node is EXPANDED, or it can miss a cheaper path.
Recap Quiz
CHECK YOUR UNDERSTANDING · 3 MIN
- Q1. The frontier of BFS is a:
- (a) Stack (LIFO) (b) Queue (FIFO) (c) Priority queue (d) Set
- Q2. BFS is optimal when:
- (a) Never (b) All step costs are equal (c) The graph has cycles (d) b is infinite
- Q3. Space complexity of BFS:
- (a) O(d) (b) O(bd) (c) O(b^d) (d) O(1)
- Q4. UCS expands the node with the lowest:
- (a) Depth (b) g(n) path cost (c) Heuristic h(n) (d) Number of children
Homework and Next Lecture
BEFORE THE NEXT CLASS
- Task 1: Run BFS by hand on the road map from Vehari to Sahiwal. Write the queue after each expansion and the path.
- Task 2: Run uniform-cost search from Vehari to Sahiwal. Write the expansion order with g(n) and the cheapest path.
- Task 3: Use searchlib.py (notes §2.2) to solve 1 2 3 / 4 _ 5 / 7 8 6 with BFS. How many moves and expansions?
- Read: Russell & Norvig 3.4.1-3.4.2; Luger 3.2.3.
NEXT LECTURE
- Lecture 12:
- Depth-First Search and Iterative Deepening