# dfs by yaoyufang

VIEWS: 4 PAGES: 4

• pg 1
```									Depth-First Searches

G5AIAI – Introduction to AI
Depth-First Search

• Depth first search explores one branch of a tree before it
starts to explore another branch. It can be implemented by
adding newly expanded nodes at the front of the queue.
• Using the general search algorithm we can implement it as
follows:
Function DEPTH-FIRST-SEARCH(problem) returns a solution or failure
Return GENERAL-SEARCH(problem,ENQUEUE-AT-FRONT)
• The following slide illustrates a depth-first search of 26
nodes (states) with an initial state of node A and a goal
state of node L. Press space to see the example node set.
The example node set
Initial state
A

B          C                  D                  E             F
Goal state

G H        I       J          K       L          M N           O       P

Q   R     S        T          U V                W X           Y       Z

Press space to see a DFS of the example node set
Node B is expanded and removed node
process now continues until the
The search then moves to the first from            This node removed from state: the node
Node A is is then expanded queue. Each
We begin with our initialtheto reveal
the queue.is achieved. Press continue
goal state Revealed nodes space.
in the queue. Press space toare added to       A   revealed node is added to the FRONT of
labeled A. Press space nodes. Press space.
further (unexpanded) to continue
the FRONT of the queue. Press space.               the queue. Press space to continue.

B                  C                     D                 E                    F

Node L is located and the search
G H                I        J            K       L    returns a solution. Press space to
end.

Q      R          S        T             U

continue search
Press space to begin thethe search

3
4
6
5
1
Size of Queue: 0             Queue: A J, D, E, FF F
T, D, D, E, FF
L,
U,
K,
D,
J,
S,
I, C,
C, H, C,
R, L, D,
H, D, E, D,
Q, E, D, E,
G, C, F E,
B, J, E, F
Empty F E,
Nodes expanded: 0
14
13
12
11
10
9
8
7
6
5
4
3
2
1                    Action: Expanding
Backtracking
CurrentFINISHED SEARCH                                   3
2
1
0
Current level: n/a

DEPTH-FIRST SEARCH PATTERN

```
To top