Professional Documents
Culture Documents
Chapter 3 State Space Search
Chapter 3 State Space Search
Chapter 3 State Space Search
State Search
3.0 Introduction 3.3 Using Space State to Represent
Reasoning with the Predicate Calculus
3.1 Graph Theory
3.4 Epilogue and References
3.2 Strategies for Space State Search
3.5 Exercises
George F Luger
1
Luger: Artificial Intelligence, 6th edition. © Pearson Education Limited
Why Search?
To achieve goals or to maximize our utility
we need to predict what the result of our
actions in the future will be.
2
Solving problems by searching
Questions that need to be answered include:
4
Leonard Euler (“Oiler”)
1706 - 1783
• if there is a walk around the city that crosses each bridge exactly once.
•Although the residents had failed to find such a walk and doubted that it was possible, no
one had proved its impossibility.
•Devising a form of graph theory, Euler created an alternative representation for the map,
3
presented in Figure 3.2.
AI: Chapter 3: Structure and Strategies by Searching-Luger: Artificial Intelligence, 6th edition.
Figure 3.3: A labeled directed graph.
4
AI: Chapter 3: Structure and Strategies by Searching-Luger: Artificial Intelligence, 6th edition.
Figure 3.4: A rooted tree, exemplifying family relationships.
5
AI: Chapter 3: Structure and Strategies by Searching-Luger: Artificial Intelligence, 6th edition.
Graph Vs Tree
Graph Vs Tree
AI: Chapter 3: Structure and Strategies by Searching-Luger: Artificial Intelligence, 6th edition.
AI: Chapter 3: Structure and Strategies by Searching-Luger: Artificial Intelligence, 6th edition.
Example: Eight Queens
• Place eight queens on a Q
chess board such that no
queen can attack another Q
queen
Q
• No path cost because Q
only the final state
counts! Q
Q
• Incremental formulations
• Complete state Q
formulations Q
AI: Chapter 3: Solving Problems 49
by Searching
Example: Eight Queens
• States:
– Any arrangement of 0 to 8
Q
queens on the board
• Initial state:
Q
– No queens on the board Q
• Successor function:
– Add a queen to an empty Q
square
• Goal Test: Q
– 8 queens on the board and
none are attacked Q
• 64*63*…*57 = 1.8*1014
possible sequences Q
– Ouch!
Q
AI: Chapter 3: Solving Problems 50
by Searching
Example: Eight Queens
• States: Q
– Arrangements of n queens,
one per column in the Q
leftmost n columns, with
no queen attacking another Q
are states
• Successor function: Q
– Add a queen to any square
in the leftmost empty Q
column such that it is not
attacked by any other Q
queen.
• 2057 sequences to Q
investigate Q
AI: Chapter 3: Solving Problems 51
by Searching
Other Toy Examples
• Another Example: Jug Fill
• Another Example: Black White Marbles
• Another Example: Row Boat Problem
• Another Example: Sliding Blocks
• Another Example: Triangle Tee
Problem Goal
Goal Problem
Goal Driven Vs Data Driven
Goal driven: A theorem that is to be proved,
diagnosing mechanical problem in an
automobile and finding an exit from a maze
Data driven: Configuration problems and an
expert system that will help a human classify
plants by species, genus etc
Best?
Both strategies search the same search space
graph
The order and actual number of states
searched can differ
The preferred strategy is determined by
- Properties of the problem
Include complexity of rules
“shape” of state shape
Nature of problem data
AI: Chapter 3: Structure and Strategies by Searching-Luger: Artificial Intelligence, 6th edition.
AI: Chapter 3: Structure and Strategies by Searching-Luger: Artificial Intelligence, 6th edition.
Both directions yield Exponential Complexity
AI: Chapter 3: Structure and Strategies by Searching-Luger: Artificial Intelligence, 6th edition.
Reading Task
AI: Chapter 3: Structure and Strategies by Searching-Luger: Artificial Intelligence, 6th edition.
Uninformed strategies use only the information available
in the problem definition
Also known as blind searching
Uninformed Search Strategies
• Breadth-first search
• Uniform-cost search
• Depth-first search
• Depth-limited search
• Iterative deepening search
Is A a goal state?
82
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go
at end
Expand:
fringe = [B,C]
Is B a goal state?
83
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go
at end
Expand:
fringe=[C,D,E]
Is C a goal state?
84
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go
at end
Expand:
fringe=[D,E,F,G]
Is D a goal state?
85
Example
BFS
88
Properties of breadth-first search
Complete? Yes it always reaches goal (if b is finite)
Time? 1+b+b2+b3+… +bd + (bd+1-b)) = O(bd+1)
(this is the number of nodes we generate)
Space? O(bd+1) (keeps every node in memory,
either in fringe or on a path to fringe).
Optimal? Yes (if we guarantee that deeper
solutions are less optimal, e.g. step-cost=1).
Note: in the new edition Space & Time complexity was O(bd) because we
postpone the expansion. 89
Lessons From Breadth First Search
The memory requirements are a bigger
problem for breadth-first search than is
execution time
107
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[B,C]
Is B a goal state?
108
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[D,E,C]
Is D = goal state?
109
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[H,I,E,C]
Is H = goal state?
110
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[I,E,C]
Is I = goal state?
111
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[E,C]
Is E = goal state?
112
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[J,K,C]
Is J = goal state?
113
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[K,C]
Is K = goal state?
114
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[C]
Is C = goal state?
115
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[F,G]
Is F = goal state?
116
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[L,M,G]
Is L = goal state?
117
Depth-first search
• Expand deepest unexpanded node
• Implementation:
– fringe = LIFO queue, i.e., put successors at front
queue=[M,G]
Is M = goal state?
118
Properties of depth-first search A
B C
• Complete? No: fails in infinite-depth spaces
Can modify to avoid repeated states along path
• Time? O(bm) with m=maximum depth
• terrible if m is much larger than d
– but if solutions are dense, may be much faster than
breadth-first
• Space? O(bm), i.e., linear space! (we only need to
remember a single path + expanded unexplored nodes)
• Optimal? No (It may find a non-optimal goal first)
121
Properties of depth-first search A
B C
• Complete? No: fails in infinite-depth spaces
Can modify to avoid repeated states along path
• Time? O(bm) with m=maximum depth
• terrible if m is much larger than d
– but if solutions are dense, may be much faster than
breadth-first
• Space? O(bm), i.e., linear space! (we only need to
remember a single path + expanded unexplored nodes)
• Optimal? No (It may find a non-optimal goal first)
126
Iterative deepening search
• To avoid the infinite depth problem of DFS, we can
decide to only search until depth L, i.e. we don’t expand beyond depth L.
Depth-Limited Search
131
Iterative deepening search L=0
132
Iterative deepening search L=1
133
Iterative deepening search L=2
134
Iterative Deepening Search L=3
135
Iterative deepening search
• Number of nodes generated in a depth-limited search to
depth d with branching factor b:
NDLS = b0 + b1 + b2 + … + bd-2 + bd-1 + bd
BFS
• For b = 10, d = 5,
– NDLS = 1 + 10 + 100 + 1,000 + 10,000 + 100,000 = 111,111
– NIDS = 6 + 50 + 400 + 3,000 + 20,000 + 100,000 = 123,450
– NBFS = ............................................................................................
d = 1,111,100
Note: BFS can also be adapted to be O(b ) by waiting to expand until all nodes at depth d are checked 136
Properties of iterative deepening search
• Complete? Yes
• Time? O(bd)
• Space? O(bd)
• Optimal? Yes, if step cost = 1 or increasing
function of depth.
137
Bidirectional Search
• Idea
– simultaneously search forward from S and backwards
from G
– stop when both “meet in the middle”
– need to keep track of the intersection of 2 open sets
of nodes
• What does searching backwards from G mean
– need a way to specify the predecessors of G
• this can be difficult,
• e.g., predecessors of checkmate in chess?
– which to take if there are multiple goal states?
– where to start if there is only a goal test, no explicit 138
Bi-Directional Search
Complexity: time and space complexity are:
O (b d / 2 )
139
Graph Search vs Tree S Search
B
S
B C
C C S B S
State Space
Example of a Search Tree
• Graph search optimal but memory inefficient
– never generate a state generated before
• must keep track of all possible states (uses a lot of memory)
• e.g., 8-puzzle problem, we have 9! = 362,880 states
• approximation for DFS/DLS: only avoid states in its (limited)
memory: avoid looping paths.
• Graph search optimal for BFS and UCS (do you understand why?)
140
Graph Search vs Tree S Search
B
S
B C
C C S B S
State Space
Example of a Search Tree
• Graph search optimal but memory inefficient
– never generate a state generated before
• must keep track of all possible states (uses a lot of memory)
• e.g., 8-puzzle problem, we have 9! = 362,880 states
• approximation for DFS/DLS: only avoid states in its (limited)
memory: avoid looping paths.
• Graph search optimal for BFS and UCS (do you understand why?)
141
Summary of algorithms
142
Summary
• Problem formulation usually requires abstracting away
real-world details to define a state space that can
feasibly be explored
143
Exercise
2. Consider the graph below:
B D
A F
E
C
a) [2pt]Draw the first 3 levels of the full search tree with root node given by A.
Use graph search, i.e. avoid repeated states.
b) [2pt] Give an order in which we visit nodes if we search the tree breadth first.
c) [2pt] Express time and space complexity for general breadth-first search in terms
of the branching factor, b, and the depth of the goal state, d.
d) [2pt] If the step-cost for a search problem is not constant, is breadth first search
always optimal? Is BFS graph search optimal?
e) [2pt] Now assume the constant step-cost.
144
Is BSF tree search optimal? Is BFS graph search optimal?
Next time
Questions?
145