BFS & DFS
BFS & DFS
BFS & DFS
1
Problem Solving as Search
• Humans solve problems by considering and evaluating
alternate choices, until one set of choices lead to an
acceptable solution of the problem
3
Problem Solving as Search
• Search is an important technique, however it has a severe
problem
5
Depth First Search
Method
• When a state is examined, all of its children and their
descendants are examined before any of its siblings
• The siblings are considered only when no further descendants
of a state can be found
6
Depth First Search
7
Depth First Search
8
Depth First Search
9
Breadth
- First Search
A B C D
E F G H
front
FIFO Queue
Breadth
- First Search
A B C D
E F G H
E F G H
E F G H
FIFO Queue
Breadth
- First Search
A B C D
E F G H
FIFO Queue
Breadth
- First Search
A
A B C D
E F G H
B discovered front B
FIFO Queue
Breadth
- First Search
A
A B C D
E F G H
E F G H
I discovered front B I
FIFO Queue
Breadth
- First Search
A
A B C D
E F G H
E F G H
E F G H
E F G H
E F G H
F discovered front I F
FIFO Queue
Breadth
- First Search
A
A B C D
E F G H
E F G H
E F G H
E F G H
E F G H
E F G H
E F G H
E F G H
E F G H
I B
E discovered front F E
FIFO Queue
Breadth
- First Search
A
A B C D
E F G H
I B
E F G H
I B
E F G H
I B
I finished front F E
FIFO Queue
Breadth
- First Search
A
A B C D
E F G H
I B
E F G H
I B
E F G H
I B F
G discovered front E G
FIFO Queue
Breadth
- First Search
A
A B C D
E F G H
I B F
F finished front E G
FIFO Queue
Breadth
- First Search
A
A B C D
E F G H
I B F
E F G H
I B F
E F G H
I B F
E finished front G
FIFO Queue
Breadth
- First Search
A
A B C D
E F G H
I B F
E F G H
I B F
FIFO Queue
Breadth
- First Search
A G
A B C D
E F G H
I B F
C discovered front C
FIFO Queue
Breadth
- First Search
A G
A B C D
E F G H
I B F
E F G H
I B F G
H discovered front C H
FIFO Queue
Breadth
- First Search
A G
A B C D
E F G H
I B F G
G finished front C H
FIFO Queue
Breadth
- First Search
A G
A B C D
E F G H
I B F G
E F G H
I B F G
E F G H
I B F G
D discovered front H D
FIFO Queue
Breadth
- First Search
A G C
A B C D
E F G H
I B F G
C finished front H D
FIFO Queue
Breadth
- First Search
A G C
A B C D
E F G H
I B F G
E F G H
I B F G
E F G H
I B F G
finished H front D
FIFO Queue
Breadth
- First Search
A G C
A B C D
E F G H
I B F G
E F G H
I B F G
FIFO Queue
Breadth
- First Search
A G C
A B C D
E F G H
I B F G
D finished front
FIFO Queue
Breadth
- First Search
A G C
A B C D
E F G H
I B F G
FIFO Queue
Breadth First Search
- A G C
A B C D
E F G H
I B F G
STOP front
FIFO Queue
Breadth-first search
Expand shallowest unexpanded node
Fringe: nodes waiting in a queue to be explored, also called OPEN
Implementation:
fringe is a first-in-first-out (FIFO) queue, i.e., new successors go
at end of the queue.
Is A a goal state?
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go at end
Expand:
fringe = [B,C]
Is B a goal state?
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go at end
Expand:
fringe=[C,D,E]
Is C a goal state?
Breadth-first search
Expand shallowest unexpanded node
Implementation:
fringe is a FIFO queue, i.e., new successors go at end
Expand:
fringe=[D,E,F,G]
Is D a goal state?
Example: Map Navigation
A B C
S G
D E F
B D E
C E
A B C
S G
D E F
Note: this is the search tree at some particular point in
in the search.
Search Method 1: Breadth First Search
S
A D
B A
D E
C E E S B S B F
(Use the simple heuristic of not generating a child node if that node is
a parent to avoid “obvious” loops: this clearly does not avoid all loops
and there are other ways to do this)
Breadth-First Search
Breadth-First-Search (*)
1. Put the start node s on OPEN
2. If OPEN is empty exit with failure.
3. Remove the first node n from OPEN and place it on CLOSED.
4. If n is a goal node, exit successfully with the solution obtained by tracing back pointers from n to s.
5. Otherwise, expand n, generating all its successors attach to them pointers back to n, and put them at the
end of OPEN in some order.
6. Go to step 2.
* This simplified version does not check for loops or repeated states
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = Last In First Out (LIFO) queue, i.e., put successors at
front
Is A a goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[B,C]
Is B a goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[D,E,C]
Is D = goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[H,I,E,C]
Is H = goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[I,E,C]
Is I = goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[E,C]
Is E = goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[J,K,C]
Is J = goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[K,C]
Is K = goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[C]
Is C = goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[F,G]
Is F = goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[L,M,G]
Is L = goal state?
Depth-first search
Expand deepest unexpanded node
Implementation:
fringe = LIFO queue, i.e., put successors at front
queue=[M,G]
Is M = goal state?
Search Method 2: Depth First Search
(DFS)
S
A D
A B C
B D
S G
D E F
C E