Recap of Lecture 9 and Homework Check
LAST LECTURE · 5 MIN
Today: Prolog searched by going deep first. Is that the best way? First we must say what a search problem IS.
Today's Plan (50 minutes)
ROADMAP
0-5
Recap of Lecture 9 and homework check
S02
5-8
Plan and learning outcomes
S03-S04
8-18
Module 1: Formulating problems as search
S05-S08
18-28
Module 2: State spaces, search trees, the search loop
S09-S11
28-35
Module 3: Four properties of search methods
S12-S15
35-43
Class activity: farmer, dog, goat and fodder
S16
43-48
Common mistakes and recap quiz
S17-S18
48-50
Homework and next lecture
S19
Learning Outcomes (SLOs)
BY THE END OF THIS LECTURE YOU WILL BE ABLE TO
SLO 10.1
Formulate a problem as a search problem: initial state, actions, transition model, goal test, path cost
→ CLO3 · C3 Apply
SLO 10.2
Differentiate a state space from a search tree, and tree search from graph search
→ CLO3 · C2 Understand
SLO 10.3
Explain completeness, optimality, time and space complexity using b, d and m
→ 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
Problems as Search
Many AI problems are: find a sequence of actions that reaches a goal
The Problem-Solving Agent
MODULE 1 · AGENT
Goal
Problem
Search
Execute
Decide what you want: be in Lahore.
Choose states and actions: cities and roads. Leave out details (abstraction).
Try action sequences in the head, find one that reaches the goal.
Carry out the plan, one action at a time.
Five Components of a Search Problem
MODULE 1 · FORMULATION
Lahore
Sahiwal
Khanewal
Mian Channu
Multan
Arifwala
Burewala
Pakpattan
Vehari
Mailsi
Optimal: the amber route Burewala-Arifwala-Sahiwal-Lahore = 275 km is the cheapest solution.
Formulating Two Classic Problems
MODULE 1 · FORMULATION
REAL OUTPUT: L10/STATES.PY
- 8-puzzle: reachable states from the goal: 181440 (= 9!/2 = 181440 )
- 8-puzzle: average branching factor: 2.67
- Water jugs: reachable states: 14
- Water jugs: shortest plan to get 2 litres: fill 4 -> (4, 0) | pour 4->3 -> (1, 3) | empty 3 -> (1, 0) | pour 4->3 -> (0, 1) | fill 4 -> (4, 1) | pour 4->3 -> (2, 3)
Slide 9
MODULE 2
State Spaces and Search Trees
The map is the state space; the search builds a tree on top of it
Search Trees, Nodes and the Frontier
MODULE 2 · SEARCH TREE
Key idea: every search method uses the same loop - they differ ONLY in which frontier node they expand next.
The Search Loop: Tree Search vs Graph Search
MODULE 2 · ALGORITHM
GENERAL SEARCH (GRAPH SEARCH)
REAL OUTPUT: L10/SEARCH_DEMO.PY (FIFO FRONTIER)
- frontier = [initial state]
- explored = { } # graph search
- loop:
- if frontier is empty: return FAIL
- node = choose and remove a node
- if goal_test(node): return path
- add node.state to explored
- for each child of node:
- if child not explored and
- not in frontier:
- add child to frontier
- Shortest road route for comparison: Burewala -> Arifwala -> Sahiwal -> Lahore (275 km)
- Tree search (no explored set)
- path : Burewala -> Vehari -> Multan -> Lahore (475 km)
- nodes expanded: 18 nodes generated: 46 largest frontier: 28
- Graph search (explored set)
- path : Burewala -> Vehari -> Multan -> Lahore (475 km)
- nodes expanded: 9 nodes generated: 12 largest frontier: 5
Notice: graph search did half the work. Neither found the 275 km route - fewest roads is not fewest km.
Slide 12
MODULE 3
Measuring a Search Method
Four questions we ask about every algorithm in Lectures 11-16
Four Properties of Search Methods
MODULE 3 · PROPERTIES
b - branching factor
d - depth of goal
m - maximum depth
Maximum children of a node. Burewala: 3. 8-puzzle: 2 to 4.
Steps to the shallowest goal. Burewala to Lahore: 3 roads.
Longest path in the tree; infinite if there are cycles.
Why Search Is Hard: Exponential Growth
MODULE 3 · COMPLEXITY
REAL OUTPUT: L10/GROWTH.PY (B = 10)
Lesson 1
- depth d nodes (b^d) time memory
- 2 100 0.0001 seconds 100 KB
- 4 10,000 0.0100 seconds 10 MB
- 6 1,000,000 1 seconds 1 GB
- 8 100,000,000 1.7 minutes 100 GB
- 10 10,000,000,000 2.8 hours 10 TB
- 12 1,000,000,000,000 11.6 days 1,000 TB
Memory runs out before time: depth 10 needs 10 TB.
Lesson 2
Blind search cannot go deep - we need heuristics (L13-L14).
Assumptions: 1 million nodes per second, 1 KB per node. Chess: b is about 35 - full search is impossible.
The Search Methods Ahead
MODULE 3 · ROADMAP
Assignment 2 (week 8): 8-puzzle with best-first search - uses today's formulation (S08).
Activity: Farmer, Dog, Goat and Fodder
CLASS ACTIVITY · PAIRS · 8 MIN
Pairs, 6 min + 2 min. A farmer must take a dog, a goat and fodder (chara) across a canal. The boat holds the farmer + ONE item. Left alone, the dog eats the goat and the goat eats the fodder.
Choose a state description.
What is the path cost?
Write the initial state and the goal state.
Which states are illegal? How many legal states are there?
List the possible actions.
Find a solution. How many crossings?
Common Mistakes
WATCH OUT
WRONG IDEA
CORRECT IDEA
✗ The search tree and the state space are the same.
✓ The map is the state space; the tree is built by the search and can repeat states.
✗ A solution is always optimal.
✓ Optimal = lowest path cost. S11 found 475 km; 275 km existed.
✗ Complete means fast.
✓ Complete means it finds a solution if one exists - it may be slow.
✗ b is the number of states.
✓ b is the branching factor: children per node.
✗ Graph search and tree search do the same work.
✓ Graph search remembers explored states and skips repeats.
Recap Quiz
CHECK YOUR UNDERSTANDING · 3 MIN
- Q1. Which is NOT one of the five components of a search problem?
- (a) Initial state (b) Goal test (c) Heuristic (d) Path cost
- Q2. A search method that always finds the cheapest solution is:
- (a) Complete (b) Optimal (c) Fast (d) Informed
- Q3. The frontier is the set of:
- (a) Goal states (b) Explored states (c) Generated nodes not yet expanded (d) All states
- Q4. With b = 10, nodes at depth 6 are about:
- (a) 60 (b) 10,000 (c) 1,000,000 (d) 10^10
Homework and Next Lecture
BEFORE THE NEXT CLASS
- Task 1: Formulate the 8-puzzle with start 1 2 3 / 4 _ 6 / 7 5 8: write all five components and the states after each possible first move.
- Task 2: On the S07 map, draw the search tree from Burewala to depth 2 (tree search, repeats allowed). How many nodes are at depth 2?
- Task 3: A tree has b = 3. How many nodes are at depth 5? How many in the whole tree up to depth 5?
- Read: Russell & Norvig 3.1-3.4; Luger 3.1-3.2.
NEXT LECTURE
- Lecture 11:
- Breadth-First Search