Recap of Lecture 11 and Homework Check
LAST LECTURE · 5 MIN
Today: BFS needs huge memory. Can we search with almost no memory? Yes - go deep first.
Today's Plan (50 minutes)
ROADMAP
0-5
Recap of Lecture 11 and homework check
S02
5-8
Plan and learning outcomes
S03-S04
8-15
Module 1: The idea of DFS and the algorithm
S05-S07
15-25
Module 2: DFS on the road map; properties
S08-S10
25-35
Module 3: Depth-limited search and iterative deepening
S11-S14
35-43
Class activity: DFS and IDDFS 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 12.1
Trace depth-first search with a LIFO stack and compare its order with BFS
→ CLO3 · C3 Apply
SLO 12.2
Explain why DFS uses little memory but is neither optimal nor always complete
→ CLO3 · C2 Understand
SLO 12.3
Apply depth-limited search and iterative deepening, and compare all uninformed methods
→ 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
Depth-First Search
Follow one road as far as it goes; when it ends, go back and try the next
DFS: Deepest First with a Stack
MODULE 1 · IDEA
Narrow streets
A stack (LIFO)
Backtracking
Follow one gali to its end; at a dead end, go back to the last turning.
Like a stack of plates: the last one put on is taken first.
Dead end? Pop the next node - a sibling higher up the tree. Same as Prolog (L09).
The DFS Algorithm
MODULE 1 · ALGORITHM
DFS IN PYTHON (SEARCHLIB.PY)
- def dfs(graph, start, goal):
- stack = [[start]] # stack of paths
- explored = set()
- while stack:
- path = stack.pop() # TOP = newest
- node = path[-1]
- if node in explored: continue
- if node == goal: return path
- explored.add(node)
- kids = [n for n in neighbours(node)
- if n not in explored]
- for n in reversed(kids): # first child on top
- stack.append(path + [n])
- return None
One change
pop() from the top of a list instead of popleft() from a queue.
Reversed push
So the FIRST neighbour ends up on top and is explored first.
Goal test on pop
A node is tested when it is taken off the stack.
Slide 8
MODULE 2
DFS on the Road Map
A trip from Burewala to Sahiwal - just 2 roads away
DFS Trace: Burewala to Sahiwal
MODULE 2 · TRACE
Lahore
Result: DFS: 4 roads, 645 km through Lahore. BFS: Burewala - Arifwala - Sahiwal, 2 roads, 105 km. DFS is not optimal.
Sahiwal
Khanewal
Multan
Mian Channu
Burewala
Arifwala
Pakpattan
Vehari
Mailsi
REAL OUTPUT: L12/DFS_TRACE.PY
- step expand children pushed stack (top ... bottom)
- 1 Burewala Vehari, Arifwala, Mian Channu Vehari, Arifwala, Mian Channu
- 2 Vehari Mailsi, Multan Mailsi, Multan, Arifwala, Mian Channu
- 3 Mailsi - (dead end) Multan, Arifwala, Mian Channu
- 4 Multan Khanewal, Lahore Khanewal, Lahore, Arifwala, Mian Channu
- 5 Khanewal Mian Channu Mian Channu, Lahore, Arifwala, Mian Channu
- 6 Mian Channu - (dead end) Lahore, Arifwala, Mian Channu
- 7 Lahore Sahiwal Sahiwal, Arifwala, Mian Channu
- Sahiwal popped -> goal.
- DFS path: Burewala -> Vehari -> Multan -> Lahore -> Sahiwal | 4 roads | 645 km | expanded: 7
- BFS path: Burewala -> Arifwala -> Sahiwal | 2 roads | 105 km | expanded: 3
Properties of Depth-First Search
MODULE 2 · PROPERTIES
REAL OUTPUT: L12/COMPARE.PY - 8-PUZZLE, BFS VS PLAIN DFS
- start BFS moves DFS moves DFS expanded
- 1 2 3 / 4 _ 6 / 7 5 8 2 49,284 135,586
- _ 2 3 / 1 4 5 / 7 8 6 4 22,646 23,538
- 8 1 3 / 4 _ 2 / 7 6 5 14 63,906 83,659
Look: DFS found a 49,284-move solution where 2 moves were enough.
Slide 11
MODULE 3
Limited Depth and Iterative Deepening
Keep DFS's small memory, get BFS's guarantees
Depth-Limited Search
MODULE 3 · DLS
REAL OUTPUT: L12/IDDFS.PY - DEPTH-LIMITED SEARCH, BUREWALA TO LAHORE
- Road map, Burewala to Lahore:
- limit 0: nodes 1 result: cutoff
- limit 1: nodes 4 result: cutoff
- limit 2: nodes 9 result: cutoff
- limit 3: nodes 6 result: Burewala -> Vehari -> Multan -> Lahore (475 km)
Depth limit l
Cutoff vs failure
Choosing l
Nodes at depth l get no children - DFS cannot dive forever.
Cutoff: the limit stopped the search (try a bigger l). Failure: no solution at all.
Too small: no answer. Too big: DFS problems return. 10 cities - at most 9 roads.
Iterative Deepening Search (IDDFS)
MODULE 3 · IDDFS
ITERATIVE DEEPENING
- for limit in 0, 1, 2, 3, ...:
- result = depth_limited_search(limit)
- if result != cutoff:
- return result
Best of both
- Memory like DFS: O(bd).
- Complete and optimal (equal costs) like BFS.
- Repeats top levels: only about 11% extra for b = 10.
REAL OUTPUT: L12/IDDFS.PY - 8-PUZZLE 8 1 3 / 4 _ 2 / 7 6 5
- 8-puzzle 8 1 3 / 4 _ 2 / 7 6 5:
- IDDFS found 14 moves at limit 14; nodes generated in all iterations: 24,028
- BFS: 14 moves; nodes expanded 3,633; largest frontier 2,327 | IDDFS stores at most 15 states on its path
Use it when: the search space is large and the solution depth is unknown - the preferred uninformed method.
Comparing the Uninformed Search Methods
MODULE 3 · SUMMARY
Key: * if b is finite. b branching factor, d depth of shallowest goal, m maximum depth, l depth limit.
Activity: DFS and IDDFS on the Campus Map
CLASS ACTIVITY · PAIRS · 8 MIN
Pairs, 6 min + 2 min. Same campus paths as L11 (Gate-Admin, Gate-Library, Admin-CS Dept, Admin-Cafe, Library-Cafe, Library-Ground, CS Dept-Lab, Cafe-Hostel, Ground-Hostel, Hostel-Lab). Goal: Hostel.
Run DFS from Gate to Hostel: write the stack after each expansion.
Run iterative deepening. At which limit is Hostel found, and by which path?
Which path does DFS return? How many steps?
Which method found the shorter path?
Run depth-limited search with limit 2. Result?
Which uses less memory, BFS or IDDFS? Why?
Common Mistakes
WATCH OUT
WRONG IDEA
CORRECT IDEA
✗ DFS uses a queue.
✓ DFS uses a LIFO stack: take from the top.
✗ DFS finds the shortest path.
✓ DFS returns the first goal it reaches: 645 km instead of 105 km.
✗ DFS is always complete.
✓ It can dive forever in infinite spaces or loops.
✗ IDDFS wastes a lot of time.
✓ Repeated top levels cost only about 11% extra (b = 10).
✗ Depth-limited search failing means no solution.
✓ 'Cutoff' means the limit was too small - try a bigger one.
Recap Quiz
CHECK YOUR UNDERSTANDING · 3 MIN
- Q1. The frontier of DFS is a:
- (a) Queue (FIFO) (b) Stack (LIFO) (c) Priority queue (d) Set
- Q2. Space complexity of DFS (tree search):
- (a) O(b^d) (b) O(bm) (c) O(1) (d) O(b^m)
- Q3. Iterative deepening runs depth-limited search with limits:
- (a) Only d (b) 0, 1, 2, ... until success (c) m, m-1, ... 0 (d) Random
- Q4. Which method is optimal for equal step costs AND uses linear memory?
- (a) BFS (b) DFS (c) IDDFS (d) DLS
Homework and Next Lecture
BEFORE THE NEXT CLASS
- Task 1: Run DFS by hand from Vehari to Sahiwal on the road map. Write the stack after each expansion and the path with its km.
- Task 2: Run iterative deepening from Vehari to Sahiwal: write the result of limits 0, 1, 2, 3.
- Task 3: For b = 10 and d = 5, count the nodes generated by BFS and by IDDFS. How many percent more does IDDFS generate?
- Prepare: Quiz 1 (L01-L10) this week. Assignment 2 (8-puzzle, best-first) is given in week 8.
NEXT LECTURE
- Lecture 13:
- Heuristics, Best-First and Greedy Search