Depth First and Breadth First
Search
Definitions
Heuristics (Greek heuriskein = find, discover): "the study of the methods and rules of discovery and invention".
We use our knowledge of the problem to consider some
(not all) successors of the current state (preferably just one, as with an oracle). This means clipping the state space, gaining speed, but perhaps missing the solution!
In chess: consider one (apparently best) move, maybe a few -- but not all possible legal moves.
Definitions
(2)
For heuristic search to work, we must be able to rank the children of a node. A heuristic function takes a state and returns a numeric value -- a composite
assessment of this state. We then choose a child with the best score (this could be a maximum or
minimum).
A heuristic function can help gain or lose a lot, but finding the right function is not always easy.
The 8-puzzle: how many misplaced tiles? how many slots away from the correct place? and so on.
Water jugs: ???
Definitions
(3)
The principal gain -- often spectacular -- is the reduction of the state space. For example, the full tree for Tic-Tac-Toe has 9! leaves. If we consider symmetries, the tree becomes six times smaller, but it is still quite large.
With a fairly simple heuristic function we can get the tree down to 40 states. (More on this when we discuss games.)
Heuristics can also help speed up exhaustive,
Search strategies Evaluation
A search strategy is defined by picking the order of node expansion
Strategies are evaluated along the following dimensions:
Completeness: does it always find a solution if one exists? Optimality: does it always find a least-cost solution?
Time Complexity: number of nodes generated
Space Complexity: maximum number of nodes in memory
Time and space complexity are measured in terms of
b: maximum branching factor of the search tree d: depth of the least-cost solution
m: maximum depth of the state space (may be ∞)
Search Strategies
Un-informed (Blind) Searches
We wonder in the search space without any idea
where the goal could be
Informed (Heuristic) Searches
Heuristic function guides us whether we are
getting near or far from the goal
Adversarial Searches
When two agents have conflicting goals, such as
Un-informed Search Strategies
Uninformed (Blind)
search strategies use only the
information available in the problem definition
Breadth-first Search
Depth-first search
Uniform-cost search
Depth-limited search
Iterative deepening search
Bi-directional Search
CS 103 8
Breadth-First Search
BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the
root in a BFS tree being formed. Its level is called the current level.
2. From each node z in the current level, in the
order in which the level nodes were visited, visit all the unvisited neighbors of z. The newly visited nodes from this level form a new level that
becomes the next current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step
Breadth-first Search
Expand shallowest node first, i.e., nodes visited 1st shall be
expanded 1st
Implementation:
Can be implemented by calling Tree-Search algo use the fringe as a Queue, FIFO
Put the newly expanded node at the back of the fringe Fringe = A
10
Breadth-first Search
Check if A is goal, it is not, expand A,
remove A
Breadth-first Search
Check if B is goal, it is not, expand B, remove B
Fringe = C, D, E
Check if C is goal, it is not, expand C, remove C
Fringe = D, E, F, G
Breadth-first Search
Expand shallowest unexpanded node Check if D is goal, it is not, no children so remove D
Fringe = E, F, G
Search goes on until goal is found. Once it is found, the
search terminates
Properties of Breadth-first
S
earch
Complete:
Yes (if
b
is finite)
Optimal:
Yes (if cost = 1 per step)
Time:
1+b+b
2+b
3+… +
b
d+ (
b
d+1-b
) =
O(b
d+1)
Space:
O(b
d+1)
(keeps every node in memory)
Memory requirements are the bigger are bigger problem
Exponential-complexity problems cannot be solved by
un-informed searche
Depth Nodes Time Memory
2 1,100 .11 Secs 1 MB
4 111,100 11 Secs 106 MB
6 107 19 Mins 10 GB
8 109 31 Hrs 1 TB
10 1011 120 Days 101 TB
12 1013 35 Years 10 PB
14 1015 3,523 Years 1 XB
14
Breadth-first Search
CS 103 15
Depth-First Search
DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as
the current node
2. Find an unvisited neighbor of the current node,
visit it, and make it the new current node;
3. If the current node has no unvisited neighbors,
backtrack to the its parent, and make that parent the new current node;
4. Repeat steps 3 and 4 until no more nodes can be
visited.
5. If there are still unvisited nodes, repeat from
Depth-first Search
Expand deepest unexpanded node Goes to the deepest level of the search tree where there are no further successors
Implementation:
use fringe as a Stack, LIFO
Put the expanded node at the front of the fringe Fringe = A
Depth-first Search
Expand deepest unexpanded node
Check if A is goal, it is not, expand A, remove A
Fringe = B,C
Depth-first Search
Check if B is goal, it is not, expand B, remove B
Fringe = D,E,C
Depth-first Search
Check if D is goal, it is not, expand D, remove D
Fringe = H, I, E, C
Depth-first Search
Both H and I has not successors so remove them if it is not
goal
Fringe = E, C
Depth-first Search
Che
ck
if E
is goal, it is not, expand E, remove E Fringe = J, K, C
Depth-first Search
J and K do not have successors neither is
any of them goal so remove them from
fringe one by one
Fringe = C
Depth-first Search
Check if C is goal, it is not, expand C, remove C
Fringe = F, G
Depth-first Search
Check if F is goal, it is not, expand F, remove F
Fringe = L, M, G
Depth-first Search
Both L, M are not goal, neither they have any successor
Remove L, M from fringe
Fringe = G
Check if G is goal, it is
The search terminates
Properties of Depth-first
S
earch
Complete? No: fails in infinite-depth spaces, spaces with
loops
Complete in finite state space
Modify to avoid repeated states along path
Optimal?
No
Time? O(bm): terrible if m is much larger than d Space? O(bm), i.e., linear space
Expands a node with the lowest path cost
May get stuck in infinite loop if a node has a
zero-cost action leading back to the same
state
Equivalent to breadth-first if all step costs
are equal
Implementation
:
fringe
= queue ordered by path cost
27
Uniform-cost Search
The search is guided by the path cost instead of depths, so
complexity cannot be easily characterized by b and d
Let C * is the cost of the optimal solution and every action
cost is at least ε
Complete? Yes, if step cost ≥ ε, a positive number
Optimal? Yes – nodes expanded in increasing order of g(n)
Time? # of nodes with g ≤ cost of optimal solution,
O(b[C*/ ε])
Space? # of nodes with g ≤ cost of optimal solution,
O(b[C*/ ε])