Professional Documents
Culture Documents
ARTIFICIAL INTELLIGENCE
FOR IT & NON-IT PROFESSIONALS
DFS VS BFS
2
DFS VS BFS
3
DFS VS BFS
4
DFS VS BFS
5
DFS VS BFS
6
DFS VS BFS
• The following sequence of diagrams will show you how DFS works on
a tree using the Simple Search Algorithm.
7
DFS: Example
S
A B
C D E F
G H
Q Visited
1
2
3
4
5
DFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2
3
4
5
DFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3
4
5
DFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4
5
DFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5
DFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
DFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
6 D,B S,A,B,C,D,G,H
DFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3 C,D,B S,A,B,C,D
4 G,H,D,B S,A,B,C,D,G,H
5 H,D,B S,A,B,C,D,G,H
6 D,B S,A,B,C,D,G,H
DFS VS BFS
• BFS explores the breadth of the tree first and progresses downward
level by level.
• Now, we use the same Simple Search Algo to implement BFS by
keeping our priority function as
P(n) = height(n)
• Select node with Min value of P(n)
16
DFS VS BFS
• In BFS, we still give priority to the node/ element with minimum P(n)
• Node with the largest value of height will be at the maximum priority to
be picked from Q.
• Greater the depth/height greater the priority.
• Sequence of diagrams illustrate BFS using the Simple Search
Algorithm.
17
BFS: Example
S
A B
C D E F
G H
Q Visited
1
2
3
4
5
BFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2
3
4
5
BFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3
4
5
BFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4
5
BFS: Example
S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5
BFS: Example
S
A B
C D E F
G H
Q Visited
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6
BFS: Example
S
A B
C D E F
G H
Q Visited
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7
BFS: Example S
A B
C D E F
G H
Q Visited
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8
BFS: Example S
A B
C D E F
G H
Q Visited
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9
BFS: Example S
A B
C D E F
G H
Q Visited
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 H S,A,B,C,D,E,F,G,H
10
BFS: Example S
A B
C D E F
G H
Q Visited
1 S S
2 A,B S,A,B
3 B,C,D S,A,B,C,D
4 C,D,E,F S,A,B,C,D,E,F
5 D,E,F,G,H S,A,B,C,D,E,F,G,H
6 E,F,G,H S,A,B,C,D,E,F,G,H
7 F,G,H S,A,B,C,D,E,F,G,H
8 G,H S,A,B,C,D,E,F,G,H
9 H S,A,B,C,D,E,F,G,H
10 S,A,B,C,D,E,F,G,H
DFS VS BFS
29
DFS VS BFS
30
DFS VS BFS
31
DFS VS BFS
32
DFS VS BFS
33
DFS VS BFS
34
DFS VS BFS
35
DFS VS BFS
36
DFS VS BFS
37