03 - AI - Week2
03 - AI - Week2
Week 2
Reference: http://ai.stanford.edu/~latombe/cs121/2011/schedule.htm
Simple Problem-Solving-Agent
Agent Algorithm
2
Search Tree
8 2
3 4 7
5 1 6
8 2 7
3 4
5 1 6
8 2 8 2 8 4 2 8 2
3 4 7 3 4 7 3 7 3 4 7
5 1 6 5 1 6 5 1 6 5 1 6
4
Search Nodes and States
8 2
3 4 7
5 1 6
8 2 7
If states are allowed to be revisited,
3 4
the search tree may be infinite even
5 1 6 when the state space is finite
8 2 8 2 8 4 2 8 2
3 4 7 3 4 7 3 7 3 4 7
5 1 6 5 1 6 5 1 6 5 1 6
5
Data Structure of a Node
8 2
STATE
3 4 7 PARENT-NODE
5 1 6
BOOKKEEPING
CHILDREN
Action Right
Depth 5
... Path-Cost 5
yes
Expanded
Depth of a node N
= length of path from root to N
(depth of the root = 0) 6
Node expansion 8 2
The expansion of a node N of the 3 4 7
search tree consists of: 5 1 6
1) Evaluating the successor
function on STATE(N) N
2) Generating a child of N for
each state returned by the
function
node generation node expansion
8 2 8 4 2 8 2
3 4 7 3 7 3 4 7
5 1 6 5 1 6 5 1 6
7
Fringe of Search Tree
The fringe is the set of all search nodes
that haven’t been expanded yet
8 2
3 4 7
5 1 6
8 2 7
3 4
5 1 6
8 2 8 2 8 4 2 8 2
3 4 7 3 4 7 3 7 3 4 7
5 1 6 5 1 6 5 1 6 5 1 6 8
Search Strategy
The fringe is the set of all search nodes
that haven’t been expanded yet
The fringe is implemented as a priority
queue FRINGE
• INSERT(node,FRINGE)
• REMOVE(FRINGE)
The ordering of the nodes in FRINGE
defines the search strategy
9
Search Algorithm #1
SEARCH#1
1. If GOAL?(initial-state) then return initial-state
2. INSERT(initial-node,FRINGE)
3. Repeat:
a. If empty(FRINGE) then return failure
b. N REMOVE(FRINGE) Expansion of N
c. s STATE(N)
d. For every state s’ in SUCCESSORS(s)
i. Create a new node N’ as a child of N
ii. If GOAL?(s’) then return path or goal state
iii. INSERT(N’,FRINGE)
10
Performance Measures
Completeness
A search algorithm is complete if it finds a
solution whenever one exists
[What about the case when no solution exists?]
Optimality
A search algorithm is optimal if it returns a
minimum-cost path whenever a solution exists
Complexity
It measures the time and amount of memory
required by the algorithm
11
Blind vs. Heuristic Strategies
1 2 3
1 2 3 4 5 6
STATE
4 5 7 8
N2
7 8 6 Goal state
13
Example
For a heuristic strategy counting
the number of misplaced tiles,
8 2
N2 is more promising than N1
STATE
3 4 7
N1
5 1 6
1 2 3
1 2 3 4 5 6
STATE
4 5 7 8
N2
7 8 6 Goal state
14
Blind Strategies
Breadth-first
• Bidirectional
2 3 FRINGE = (1)
4 5 6 7
16
Breadth-First Strategy
New nodes are inserted at the end of FRINGE
2 3 FRINGE = (2, 3)
4 5 6 7
17
Breadth-First Strategy
New nodes are inserted at the end of FRINGE
2 3 FRINGE = (3, 4, 5)
4 5 6 7
18
Breadth-First Strategy
New nodes are inserted at the end of FRINGE
2 3 FRINGE = (4, 5, 6, 7)
4 5 6 7
19
Important Parameters
1) Maximum number of successors of any state
20
Evaluation
b: branching factor
d: depth of shallowest goal node
Breadth-first search is:
• Complete? Not complete?
• Optimal? Not optimal?
21
Evaluation
b: branching factor
d: depth of shallowest goal node
Breadth-first search is:
• Complete
• Optimal if step cost is 1
Number of nodes generated:
???
22
Evaluation
b: branching factor
d: depth of shallowest goal node
Breadth-first search is:
• Complete
• Optimal if step cost is 1
Number of nodes generated:
1 + b + b2 + … + bd = ???
23
Evaluation
b: branching factor
d: depth of shallowest goal node
Breadth-first search is:
Complete
• Optimal if step cost is 1
Number of nodes generated:
1 + b + b2 + … + bd = (bd+1-1)/(b-1) = O(bd)
Time and space complexity is O(bd)
When we can apply goal test? Node
generation/Node expansion?
24
Evaluation
If goal test is applied during node
expansion (rather than node generation)
25
Time and Memory Requirements
1 2 3 4 1 2 3 4
5 6 7 8 ? 5 6 7 8
9 10 11 12 9 10 11 12
13 14 15 13 15 14
28
Bidirectional Strategy
2 fringe queues: FRINGE1 and FRINGE2
2 3
FRINGE = (1)
4 5
30
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
FRINGE = (2, 3)
4 5
31
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
FRINGE = (4, 5, 3)
4 5
32
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
4 5
33
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
4 5
34
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
4 5
35
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
4 5
36
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
4 5
37
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
4 5
38
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
4 5
39
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
4 5
40
Evaluation
b: branching factor
d: depth of shallowest goal node
m: maximal depth of a leaf node
Depth-first search is:
Complete?
Optimal?
41
Evaluation
b: branching factor
d: depth of shallowest goal node
m: maximal depth of a leaf node
Depth-first search is:
Complete only for finite search tree
Not optimal
Number of nodes generated (worst case):
1 + b + b2 + … + bm = O(bm)
Time complexity is O(bm)
Space complexity is O(bm) [or O(m)]
[Reminder: Breadth-first requires O(bd) time and space]
42
Evaluation
• Space complexity: it needs to store a
single path along with the remaining
unexpanded sibling nodes for each node
on the path
IDS
For k = 0, 1, 2, … do:
Perform depth-first search with
depth cutoff k
(i.e., only generate nodes with depth k)
47
Iterative Deepening
48
Iterative Deepening
49
Iterative Deepening
50
Performance
51
Number of Generated Nodes
(Breadth-First & Iterative Deepening)
d = 5 and b = 2
BF ID
1 1x6=6
2 2 x 5 = 10
4 4 x 4 = 16
8 8 x 3 = 24
16 16 x 2 = 32
32 32 x 1 = 32
63 120 120/63 ~ 2
53
Number of Generated Nodes
(Breadth-First & Iterative Deepening)
d = 5 and b = 10
BF ID
1 6
10 50
100 400
1,000 3,000
10,000 20,000
100,000 100,000
111,111 123,456 123,456/111,111 ~ 1.111
54
Comparison of Strategies
• Infinite state space: number of states is potentially high.
Branching factor (no of actions) may also be infinite (but
not common) (the search tree --infinite/finite??).
• Finite state space may also lead to the infinite search
tree.
• BFS is complete and optimal (same arc cost) for both the
cases (if there is a shallowest goal node at depth d, it will
eventually find it after expanding all the nodes at the
depth shallower than d)
• DFS is likely to stuck into a wrong path. It is not
complete and optimal even if the state space is infinite
but the shallowest goal sate is in a depth which is much
lower than infinity.
• DFS is complete only if the search tree is finite. 55
Comparison of Strategies
Breadth-first is complete and optimal,
but has high space complexity
Depth-first is space efficient, but is
neither complete, nor optimal
Iterative deepening is complete and
optimal, with the same space complexity
as depth-first and almost the same time
complexity as breadth-first
56
Outline of Search Algorithm(Ver-2)
1. Initialize: Set OPEN/FRINGE = {s0 }
2. Fail: If OPEN = { }, Terminate with Fail
3. Select: Select a state/node, n, from OPEN
4. Terminate: If n ∈ G, terminate with success
5. Expand: Generate the successors of n using
successor function and insert them in OPEN
6. Loop: Go To Step 2.
57
Revisited States
No Few Many
1 2 3
search tree is finite search tree is infinite
4 5
7 8 6
1 10 A B C
S 1 5 15
5 B 5 G
5 G G
15 C 11 10
65