BFS & DFS

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 83

State Space Search

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

- A traveler considers a number of possible alternates


to reach his destination, including intermediate
stops

- A doctor examines several possible diagnosis, and


refines his search by collecting more data about
the patient

• Hence search among alternate choices is an important


tool for exhibiting intelligence
2
Problem Solving as Search
• Requirements of search

- Definition of starting state and the goal state


e.g. I am in Islamabad and want to be in Paris

- Definition of possible intermediate states


e.g. different cities between Islamabad & Paris

- Rules for transition between these states


e.g. air flights available from one city to another

3
Problem Solving as Search
• Search is an important technique, however it has a severe
problem

• The overwhelming number of the possible states for


interesting problems makes it impossible to search
them all

• Searching the entire set of states for solving a problem is


called exhaustive search

• Human beings do not use exhaustive search


e.g. a doctor may form a diagnosis just on the basis of
“season” or “frequency of a disease in other patients”

4 • Such shortcuts are called heuristics


Search Strategies

• Blind or Uninformed search


We are not informed about the quality of our choices

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

enqueue source node front A


FIFO Queue
Breadth
- First Search
A B C D

E F G H

dequeue next vertex front A


FIFO Queue
Breadth
- First Search
A B C D

E F G H

visit neighbors of A front

FIFO Queue
Breadth
- First Search
A B C D

E F G H

visit neighbors of A front

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

visit neighbors of A front B


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

I discovered front B I
FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

finished with A front B I


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

dequeue next vertex front B I


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

visit neighbors of B front I


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

visit neighbors of B front I


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

F discovered front I F
FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

visit neighbors of B front I F


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

A already discovered front I F


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

finished with B front I F


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

dequeue next vertex front I F


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

visit neighbors of I front F


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

visit neighbors of I front F


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

A already discovered front F


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

visit neighbors of I front F


FIFO Queue
Breadth
- First Search
A
A B C D

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

visit neighbors of I front F E


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

I B

F already discovered front F E


FIFO Queue
Breadth
- First Search
A
A B C D

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

dequeue next vertex front F E


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

I B

visit neighbors of F front E


FIFO Queue
Breadth
- First Search
A
A B C D

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

dequeue next vertex front E G


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

I B F

visit neighbors of E front G


FIFO Queue
Breadth
- First Search
A
A B C D

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

dequeue next vertex front G


FIFO Queue
Breadth
- First Search
A
A B C D

E F G H

I B F

visit neighbors of G front

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

visit neighbors of G front C


FIFO Queue
Breadth
- First Search
A G
A B C D

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

dequeue next vertex front C H


FIFO Queue
Breadth
- First Search
A G
A B C D

E F G H

I B F G

visit neighbors of C front H


FIFO Queue
Breadth
- First Search
A G C
A B C D

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

get next vertex front H D


FIFO Queue
Breadth
- First Search
A G C
A B C D

E F G H

I B F G

visit neighbors of H front D


FIFO Queue
Breadth
- First Search
A G C
A B C D

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

dequeue next vertex front D


FIFO Queue
Breadth
- First Search
A G C
A B C D

E F G H

I B F G

visit neighbors of D front

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

dequeue next vertex front

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

S = start, G = goal, other nodes = intermediate states, links = legal transitions


Initial BFS Search Tree
S
A D

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.

For Shortest path or uniform cost:


5’ Otherwise, expand n, generating all its successors attach to them pointers back to n, and put them in
OPEN in order of shortest cost path.

* 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

Here, to avoid repeated states


F assume we don’t expand any
D child node which appears
already in the path from the
root S to the parent. (Again,
G one could use other
strategies)
Depth-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 top of OPEN in some order.
6. Go to step 2.
Comparing DFS and BFS
 Same worst-case time Complexity, but
 In the worst-case BFS is always better than DFS
 Sometime, on the average DFS is better if:
 many goals, no loops and no infinite paths
 BFS is much worse memory-wise
 DFS is linear space
 BFS may store the whole search space.
 In general
 BFS is better if goal is not deep, if infinite paths, if many
loops, if small search space
 DFS is better if many goals, not many loops,
 DFS is much better in terms of memory

You might also like