C Blind Search
C Blind Search
Search
(Where we systematically
explore alternatives)
1
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
If states are
are allowed
allowed to
to be
be revisited,
revisited,
3 4
the search
the search tree
tree may
may be
be infinite
infinite even
even
5 1 6 when the
the state
state space
space isis finite
finite
when
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 3 4 7
the 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
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
Is it identical
to the set of
leaves?
9
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
10
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) 11
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 12
Blind vs. Heuristic
Strategies
Blind (or un-informed) strategies do not
exploit state descriptions to order
FRINGE. They only exploit the positions
of the nodes in the search tree
14
Example
For a heuristic strategy
counting the number of
8 2
misplaced tiles, N2 is more
STATE
3 4 7 promising than N1
N1
5 1 6
1 2 3
1 2 3 4 5 6
STATE
4 5 7 8
N2
7 8 6 Goal state
15
Remark
Some search problems, such as the
(n2-1)-puzzle, are NP-hard
One can’t expect to solve all instances
of such problems in less than
exponential time (in n)
One may still strive to solve each
instance as efficiently as possible
This is the purpose of the search strategy
16
Blind Strategies
Breadth-first
• Bidirectional
2 3 FRINGE = (1)
4 5 6 7
18
Breadth-First Strategy
New nodes are inserted at the end of
FRINGE
1
2 3 FRINGE = (2, 3)
4 5 6 7
19
Breadth-First Strategy
New nodes are inserted at the end of
FRINGE
1
2 3 FRINGE = (3, 4, 5)
4 5 6 7
20
Breadth-First Strategy
New nodes are inserted at the end of
FRINGE
1
2 3 FRINGE = (4, 5, 6, 7)
4 5 6 7
21
Important Parameters
1) Maximum number of successors of any
state
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:
???
24
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 = ???
25
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)
26
Big O Notation
27
Time and Memory
Requirements
d # Nodes Time Memory
2 111 .01 msec 11 Kbytes
4 11,111 1 msec 1 Mbyte
6 ~106 1 sec 100 Mb
8 ~108 100 sec 10 Gbytes
10 ~1010 2.8 hours 1 Tbyte
12 ~1012 11.6 days 100 Tbytes
14 ~1014 3.2 years 10,000 Tbytes
Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/node
28
Time and Memory
Requirements
d # Nodes Time Memory
2 111 .01 msec 11 Kbytes
4 11,111 1 msec 1 Mbyte
6 ~106 1 sec 100 Mb
8 ~108 100 sec 10 Gbytes
10 ~1010 2.8 hours 1 Tbyte
12 ~1012 11.6 days 100 Tbytes
14 ~1014 3.2 years 10,000 Tbytes
Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/node
29
Remark
If a problem has no solution, breadth-first
may run for ever (if the state space is
infinite or states can be revisited arbitrary
many times)
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
30
Bidirectional Strategy
2 fringe queues: FRINGE1 and FRINGE2
2 3
FRINGE = (1)
4 5
32
Depth-First Strategy
New nodes are inserted at the front of
FRINGE
1
2 3
FRINGE = (2, 3)
4 5
33
Depth-First Strategy
New nodes are inserted at the front of
FRINGE
1
2 3
FRINGE = (4, 5, 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
Depth-First Strategy
New nodes are inserted at the front of
FRINGE
1
2 3
4 5
41
Depth-First Strategy
New nodes are inserted at the front of
FRINGE
1
2 3
4 5
42
Evaluation
b: branching factor
d: depth of shallowest goal node
m: maximal depth of a leaf node
Depth-first search is:
Complete?
Optimal?
43
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]
44
Depth-Limited Search
45
Iterative Deepening Search
Provides the best of both breadth-first
and depth-first search
IDS
For k = 0, 1, 2, … do:
Perform depth-first search with
depth cutoff k
(i.e., only generate nodes with depth
k) 46
Iterative Deepening
47
Iterative Deepening
48
Iterative Deepening
49
Performance
Iterative deepening search is:
• Complete
• Optimal if step cost =1
Time complexity is:
(d+1)(1) + db + (d-1)b2 + … + (1) bd
= O(bd)
Space complexity is: O(bd) or O(d)
50
Calculation
db + (d-1)b2 + … + (1) bd
= bd + 2bd-1 + 3bd-2 +… + db
= (1 + 2b-1 + 3b-2 + … + db-d)bd
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 = 120/63 ~ 2
52
32
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
53
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
Quiz: Would IDS + bi-directional search
be a good combination? 54
Revisited States
No Few Many
1 2 3
search tree is finite search tree is infinite
4 5
7 8 6
56
Avoiding Revisited States
Requires comparing state descriptions
Breadth-first search:
• Store all states associated with
generated nodes in VISITED
• If the state of a new node is in VISITED,
then discard the node
Implemented as hash-table
or as explicit data structure with flags
57
Avoiding Revisited States
Depth-first search:
Solution 1:
– Store all states associated with nodes in
current path in VISITED
– If the state of a new node is in VISITED, then
discard the node
??
58
Avoiding Revisited States
Depth-first search:
Solution 1:
– Store all states associated with nodes in current
path in VISITED
– If the state of a new node is in VISITED, then
discard the node
Only avoids loops
Solution 2:
– Store all generated states in VISITED
– If the state of a new node is in VISITED, then
discard the node
Same space complexity as breadth-first !
59
Uniform-Cost Search
Each arc has some cost c > 0
The cost of the path to each node N is
g(N) = costs of arcs
The goal is to generate a solution path of minimal cost
The nodes N in the queue FRINGE are sorted in
increasing g(N)
A S
0
1 10
S 5 B 5 G A B C
1 5 15
C
5
15
G G
11 10
Need to modify search algorithm 60
Search Algorithm #2
The goal test is applied
SEARCH#2 to a node when this node is
1. INSERT(initial-node,FRINGE) expanded, not when it is
generated.
2. Repeat:
a. If empty(FRINGE) then return failure
b. N REMOVE(FRINGE)
c. s STATE(N)
d. If GOAL?(s) then return path or goal state
e. For every state s’ in SUCCESSORS(s)
i. Create a node N’ as a successor of N
ii. INSERT(N’,FRINGE)
61
Avoiding Revisited States
in Uniform-Cost Search
For any state S, when the first node N such
that STATE(N) S is expanded, the path to N
is the best path from the initial state to S
N
N”
N’
g(N) g(N’)
g(N) g(N”)
62
Avoiding Revisited States
in Uniform-Cost Search
For any state S, when the first node N such
that STATE(N) S is expanded, the path to N is
the best path from the initial state to S
So:
• When a node is expanded, store its state
into CLOSED
• When a new node N is generated:
– If STATE(N) is in CLOSED, discard N
– If there exits a node N’ in the fringe such that
STATE(N’) STATE(N), discard the node N or N’
with the highest-cost path
63