BFS DFS
BFS DFS
Search Tree
– Generated as the search space is traversed
• The search space itself is not necessarily a tree, frequently it is a
graph
• The tree specifies possible paths through the search space
– Expansion of nodes
• As states are explored, the corresponding nodes are expanded by
applying the successor function
– this generates a new set of (child) nodes
• The fringe (frontier/queue) is the set of nodes not yet visited
– newly generated nodes are added to the fringe
– Search strategy
• Determines the selection of the next node to be expanded
• Can be achieved by ordering the nodes in the fringe
– e.g. queue (FIFO), stack (LIFO), “best” node w.r.t. some measure (cost)
18
Search Tree Vs Graph Tree
BASIS FOR
TREE GRAPH
COMPARISON
Path Only one between two More than one path is
vertices. allowed.
Root node It has exactly one root Graph doesn't have a
node. root node.
Loops No loops are permitted. Graph can have loops.
1
1
A4 D3 3
1 1
5 2
S3 C2 G0
3 3
1 4
B2 E1
Uninformed Search
– breadth-first Informed Search
– uniform-cost search – best-first search
– depth-first – search with heuristics
– depth-limited search – memory-bounded search
– iterative deepening – iterative improvement search
– bi-directional search
30
Breadth-First Search
Algorithm (Informal)
1. Enqueue the root/initial node (Queue Structure).
2. Dequeue a node and examine it.
1. If the element sought is found in this node, quit the search and
return a result.
2. Otherwise enqueue any successors (the direct child nodes) that
have not yet been discovered.
3. If the queue is empty, every node on the graph has been
examined – quit the search and return "not found".
4. Repeat from Step 2.
32
Breadth-First Search
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
Fringe: [] + [2,3]
24
Breadth-First Snapshot 2
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
Fringe: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21] + [22,23]
34
Breadth-First Snapshot 12
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
Note:
The goal node is
8 9 10 11 12 13 14 15 “visible” here, but
we can not
perform the goal
test yet.
16 17 18 19 20 21 22 23 24 25
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
39
Breadth-First Snapshot 17
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [18,19,20,21,22,23,24,25,26,27,28,29,30,31]
40
Breadth-First Snapshot 18
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [19,20,21,22,23,24,25,26,27,28,29,30,31]
41
Breadth-First Snapshot 19
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [20,21,22,23,24,25,26,27,28,29,30,31]
42
Breadth-First Snapshot 20
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [21,22,23,24,25,26,27,28,29,30,31]
43
Breadth-First Snapshot 21
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [22,23,24,25,26,27,28,29,30,31]
44
Breadth-First Snapshot 22
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [23,24,25,26,27,28,29,30,31]
45
Breadth-First Snapshot 23
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Fringe: [24,25,26,27,28,29,30,31]
46
Breadth-First Snapshot 24
1
Initial
Visited
Fringe
2 3 Current
Visible
Goal
4 5 6 7
Note:
The goal test
8 9 10 11 12 13 14 15
is positive for
this node, and
a solution is
found in 24
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
steps.
Fringe: [25,26,27,28,29,30,31]
47
Example BFS
B S
G C
F D
H E
Solve using BFS
ABDGEFCH
B F
C
A
E H
D
G
Properties of Breadth-First Search (BFS)
Optimality: Yes
b Branching Factor
d The depth of the goal
75
Depth-First Search
Based on [4]
76
Depth-First Search
1. Start
2. Push Root Node to Stack
• Mark Root Node as
Visited
• Print Root Node as Output
3. Check Top of the Stack If
Stack is Empty, Go to Step 6
4. Else, Check Adjacent Top of
the Stack
• If Adjacent is not Visited
• Push Node to Stack
• Mark Node as Visited
• Print Node as Output
• Else Adjacent Visited
5. Go to Step 3
6. Stop
77
Example DFS
B S
G C
F D
H E
Example DFS ABEGFCHD
B F
C
A
E H
D
G
Example DFS
AGEDFCHB
B F
C
A
E H
D
G
Properties of Depth-First Search
Space: O(bm)
Optimal: No
Depth-first goes off into one branch until it reaches a leaf node
– Not good if the goal is on another branch
– Neither complete nor optimal
– Uses much less space than breadth-first
• Much fewer visited nodes to keep track, smaller fringe
Breadth-first is more careful by checking all alternatives
– Complete and optimal (Under most circumstances)
– Very memory-intensive
For a large tree, breadth-first search memory requirements maybe
excessive
For a large tree, a depth-first search may take an excessively long time to
find even a very nearby goal node.
How can we combine the advantages (and avoid the disadvantages) of
these two search techniques?
82