2. Tree searching
A
B
D
C
E
F
H
L
M
I
N
O
G
J
P
K
Q
• A tree search starts at the
root and explores nodes
from there, looking for a
goal node (a node that
satisfies certain conditions,
depending on the problem)
• For some problems, any goal
node is acceptable (N or J);
for other problems, you
want a minimum-depth goal
node, that is, a goal node
nearest the root (only J)
Goal nodes
2
3. Search Algorithms
Uninformed vs. informed
Search algorithms differ by how they pick which node to
expand.
• Uninformed search Algorithms
– In making this decision, these look only at the structure of the
search tree and not at the states inside the nodes.
– Also known as “blind search”.
– Uninformed search methods: Breadth-first, depth-first, depthlimited, uniform-cost, depth-first iterative deepening,
bidirectional.
• Informed search Algorithms
– In making this decision , these look at the states inside the
nodes.
– Also known as “heuristic search”.
– Informed search methods: Hill climbing, best-first, greedy
search, beam search, A, A*
3
5. Evaluating Search Strategies
Strategies are evaluated along the following
dimensions:
– Completeness: Does it always find a solution if
one exists?
– Time complexity: How long does it take to find
a solution ?
– Space complexity: How much memory is
needed to perform the search?
– Optimality: Does the strategy find the optimal
solution?
5
7. Depth-first searching
• A depth-first search (DFS)
explores a path all the way to a
leaf before backtracking and
exploring another path.
Animated Graphic
• The search proceeds immediately
to the deepest level of the search
tree, where the nodes have no
successors . As those nodes are
expanded , they are dropped
from the Frontier [stack] ,so then
the search “backs up” to the
deepest next node that still has
unexplored successors .
• Use Stack data structure [FILO] as
a frontier .
7
8. How to do depth-first searching
A
Example
B
Algorithm
Put the root node on a stack;
while (stack is not empty) {
remove a node from the
stack;
if (node is a goal node)
return success;
put all children of node onto
the stack;
}
return failure;
D
C
E
F
H
L
M
I
N
O
G
J
P
K
Q
Animated Graphic
•
•
•
For example, after searching A, then B, then D, the
search backtracks and tries another path from B
Node are explored in the order A B D E H L M N I
OPCFGJKQ
N will be found before J
8
10. Properties of Depth First Algorithm
b( branching factor) : Maximum number of successors of any
node.
m: maximal depth of a leaf node
•
•
•
•
•
Number of nodes generated (worst case):
1 + b + b2 + … + bm = O(bm)
Complete: only for finite search tree
Optimal : Not optimal
Time complexity: O(bm)
Space complexity : O(bm) [or O(m)]
10
11. Breadth-first searching
• A breadth-first search (BFS) is
a simple strategy in which the
root node is expanded first ,
then all the successors of the
root node , then their
successors.
•
Animated Graphic
In general , all the nodes are
expanded at a given depth in
the search tree before any
nodes at the next level are
expanded.
• Use a queue data
structure [FIFO].
11
12. How to do breadth-first searching
A
Algorithm
•
•
•
•
B
Enqueue the root node
Dequeue a node and examine it
D
– If the element sought is found in
this node, quit the search and
return a result.
– Otherwise enqueue any successors
(the direct child nodes) that have
not yet been discovered.
If the queue is empty, every node
on the graph has been examined
– quit the search and return "not
found".
If the queue is not empty, repeat
from Step 2.
C
E
F
H
L
M
I
N
O
G
J
P
K
Q
Animated Graphic
• For example, after searching A,
then B, then C, the search
proceeds with D, E, F, G
• Node are explored in the order A
B C D E F G H I J K L M N O12 Q
P
13. Properties of breadth-first search
b: branching factor
d: depth of shallowest goal node
Total number of nodes generated is:
1+b+b2+b3+… … + bd = O(bd )
• Complete Yes (if b is finite)
• Time 1+b+b2+b3+… … + bd + b(bd -1) = O(bd+1)
Because the whole layer of nodes at depth d would be
expanded before detecting the goal .
• Space O(bd) (keeps every node in memory)
• Optimal if all operators have the same cost.
13
14. Depth- vs. breadth-first searching
• When a breadth-first search succeeds, it finds a
minimum-depth (nearest the root) goal node.
• When a depth-first search succeeds, the found
goal node is not necessarily minimum depth.
• For a large tree, breadth-first search memory
requirements may be excessive.
• For a large tree, a depth-first search may take an
excessively long time to find even a very nearby
goal node.
14
15. Uniform cost search
•
Find the least-cost goal .
•
The search begins at the root node. The
search continues by visiting the next node
which has the least total cost from the
root. Nodes are visited in this manner
until a goal state is reached.
•
Each node has a path cost from start (=
sum of edge costs along the path). Expand
the least cost node first.
•
Equivalent to breadth-first if step costs all
equal
•
Use a priority queue (ordered by path
cost) instead of a normal queue .
Animated Graph
15
16. Pesudocode
procedure UniformCostSearch(Graph, root, goal)
node := root, cost = 0
frontier := priority queue containing node only
explored := empty set
Do
if frontier is empty
return failure
node := frontier.pop()
if node is goal
return solution
explored.add(node)
for each of node's neighbors n
if n is not in explored
if n is not in frontier
frontier.add(n)
else if n is in frontier with higher cost
replace existing node with n
16
17. How to do Uniform Cost Search
Algorithm
if
- Frontier : empty >Return fail
else
- Add node to frontier.
- Check: node (goal)>solution
- Add node to explored.
- Neighbor s: if not explored
>add to frontier
- Else :if was with higher cost
replace it .
Example
Solution
Explored : A D B E F C
path: A to D to F to G
Cost = 8
17
18. Properties of uniform cost search
- Equivalent to breadth-first if step costs all equal.
• Complete
Yes .assuming that operator costs are nonnegative (the cost of
path never decreases)
• Optimal
Yes. Returns the least-cost path.
18