Академический Документы
Профессиональный Документы
Культура Документы
Search
(Where we systematically explore
alternatives)
1
Simple Problem-Solving-Agent
Agent Algorithm
s0 sense/read state
GOAL? select/read goal test
SUCCESSORS read successor function
solution search(s0, G, Succ)
perform(solution)
2
Searching the State Space
Search tree
3
Basic Search Concepts
Search tree
Search node
Node expansion
Fringe of search tree
Search strategy: At each stage it
determines which node to expand
4
Search Nodes ≠ States
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
5
Search Nodes ≠ 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
6
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
8
Fringe and Search Strategy
The fringe is the set of all search nodes
that haven’t been expanded yet
8 2 Is it identical
3 4 7 to the set of
5 1 6 leaves?
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
9
Fringe and Search Strategy
The fringe is the set of all search nodes
that haven’t been expanded yet
It 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
If GOAL?(initial-state) then return initial-state
INSERT(initial-node,FRINGE)
Repeat:
If empty(FRINGE) then return failure
n REMOVE(FRINGE)
s STATE(n)
For every state s’ in SUCCESSORS(s)
Create a new node n’ as a child of n
If GOAL?(s’) then return path or goal state
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 an
optimal solution whenever a solution exists
Complexity
It measures the time and amount of memory
required by the algorithm
12
Important Parameters
13
Blind vs. Heuristic Strategies
14
Example
For a blind strategy, N1 and N2
8 2 are just two nodes (at some
depth in the search tree)
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
15
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
16
Important 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
One may still strive to solve each instance
as efficiently as possible
17
Blind Strategies
Breadth-first
• Bidirectional
Depth-first
• Depth-limited
• Iterative deepening
Arc cost
Uniform-Cost = c(action) ≥ ε > 0
(variant of breadth-first)
18
Breadth-First Strategy
2 3 FRINGE = (1)
4 5 6 7
19
Breadth-First Strategy
2 3 FRINGE = (2, 3)
4 5 6 7
20
Breadth-First Strategy
2 3 FRINGE = (3, 4, 5)
4 5 6 7
21
Breadth-First Strategy
2 3 FRINGE = (4, 5, 6, 7)
4 5 6 7
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 = (bd+1-1)/(b-1) = O(bd)
Time and space complexity is O(bd)
23
Big O Notation
24
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
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
26
Bidirectional Strategy
2 fringe queues: FRINGE1 and FRINGE2
27
Bidirectional Strategy
Major difficulties:
• Need to have a predecessor function
• What about multiple goals?
• What about implicitly defined goals?
(e.g. n-queens problem)
28
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
FRINGE = (1)
4 5
29
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
FRINGE = (2, 3)
4 5
30
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 3
FRINGE = (4, 5, 3)
4 5
31
Depth-First Strategy
New nodes are inserted at the front of FRINGE
1
2 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
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:
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]
40
Depth-Limited Search
41
Iterative Deepening Search
Provides the best of both breadth-first
and depth-first search
Main idea:
IDS
For k = 0, 1, 2, … do:
Perform depth-first search with
depth cutoff k
42
Iterative Deepening
43
Iterative Deepening
44
Iterative Deepening
45
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)
46
Calculation
db + (d-1)b2 + … + (1) bd
= bd + 2bd-1 + 3bd-2 +… + db
= (1 + 2b-1 + 3b-2 + … + db-d)×bd
47
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
120/63 ~ 2
63 120 48
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
49
Comparison of Strategies
1 2 3
search tree is finite search tree is infinite
4 5
7 8 6
52
Avoiding Revisited States
Depth-first search:
Solution 1:
– Store all states associated with nodes in
current path in CLOSED
– If the state of a new node is in CLOSED, then
discard the node
Only avoids loops
Solution 2:
– Store of all generated states in CLOSED
– If the state of a new node is in CLOSED, then
discard the node
Same space complexity as breadth-first !
53
Uniform-Cost Search (Optimal)
Each arc has some cost c ≥ ε > 0
The cost of the path to each fringe node N is
g(N) = Σ costs of arcs
The goal is to generate a solution path of minimal cost
The queue FRINGE is sorted in increasing cost
A S
0
1 10
S 5 B 5 G A B C
1 5 15
5
15 C
G G
11 10
55
Modified Search Algorithm
INSERT(initial-node,FRINGE)
Repeat:
If empty(FRINGE) then return failure
n REMOVE(FRINGE)
s STATE(n)
If GOAL?(s) then return path or goal state
For every state s’ in SUCCESSORS(s)
Create a node n’ as a successor of n
INSERT(n’,FRINGE)
56
Avoiding Revisited States in
Uniform-Cost Search
When a node N is expanded the path to N is
also the best path from the initial state to
STATE(N) if it is the first time STATE(N) is
encountered.
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 57
Comments:
- Each edge
represents two
opposite arcs
From: http://www.cs.sunysb.edu/~skiena/combinatorica/animations/dijkstra.html 58