Graph Traversals (BFS and DFS)
Graph Traversals (BFS and DFS)
Graph Traversals (BFS and DFS)
0 0 0 0 0 0 0
4 3 QUEUE:-
5
PRINT:-
6 7
The algorithm works as follows:-
1 2
1.Start by visiting any one of the node and pushing it
into the end of a queue.
0 0 0 0 0 0 0
4 3 QUEUE:-
5
PRINT:-
6 7
VISITED:-
1 2 1 2 3 4 5 6 7
1 0 0 0 0 0 0
4 3 QUEUE:-
1
5
PRINT:-
6 7
VISITED:-
1 2 1 2 3 4 5 6 7
1 0 0 0 0 0 0
4 3 QUEUE:-
5
PRINT:-
6 7 1
VISITED:-
1 2 1 2 3 4 5 6 7
1 1 1 1 0 0 0
4 3 QUEUE:-
2 3 4
5
PRINT:-
6 7 1
VISITED:-
1 2 1 2 3 4 5 6 7
1 1 1 1 0 0 0
4 3 QUEUE:-
3 4
5
PRINT:-
6 7 1 2
VISITED:-
1 2 1 2 3 4 5 6 7
1 1 1 1 0 0 0
4 3 QUEUE:-
4
5
PRINT:-
6 7 1 2 3
VISITED:-
1 2 1 2 3 4 5 6 7
1 1 1 1 1 0 0
4 3 QUEUE:-
4 5
5
PRINT:-
6 7 1 2 3
VISITED:-
1 2 1 2 3 4 5 6 7
1 1 1 1 1 0 0
4 3 QUEUE:-
5
5
PRINT:-
6 7 1 2 3 4
VISITED:-
1 2 1 2 3 4 5 6 7
1 1 1 1 1 0 0
4 3 QUEUE:-
5
PRINT:-
6 7 1 2 3 4 5
VISITED:-
1 2 1 2 3 4 5 6 7
1 1 1 1 1 1 1
4 3 QUEUE:-
6 7
5
PRINT:-
6 7 1 2 3 4 5
VISITED:-
1 2 1 2 3 4 5 6 7
1 1 1 1 1 1 1
4 3 QUEUE:-
7
5
PRINT:-
6 7 1 2 3 4 5 6
VISITED:-
1 2 1 2 3 4 5 6 7
1 1 1 1 1 1 1
4 3 QUEUE:-
5
PRINT:-
6 7 1 2 3 4 5 6 7
CODE FOR
BREADTH FIRST SEARCH
1 2
4 3
5
6 7
CODE:https://ideone.com/Mgimyh
Time complexity of BFS:-
CODE:-https://ideone.com/mS5XTM
Depth First Search
The DFS algorithm is a recursive
algorithm. In this algorithm one starting
1 2
vertex is taken, and when an adjacent
vertex is found, it moves to that adjacent
vertex and then try to traverse in the 4 3
same manner.If an univisted adjacent
node is not found then it backtracks until
it finds an unexplored path, and then 5
explores it.
6 7
STACK
A B VISITED
A B C D E F G
0 0 0 0 0 0 0
C D
E PRINT :-
F G
The dfs algorithm works as follows:-
1 2
1.) Start by pushing any one of the graph's
nodes on top of a stack.
0 0 0 0 0 0 0
C D
E PRINT :-
F G
STACK
A B VISITED
A B C D E F G
1 0 0 0 0 0 0
C D
E PRINT :-
A
F G A
STACK
A B VISITED
A B C D E F G
1 0 1 0 0 0 0
C D
E PRINT :-
A C
C
F G A
STACK
A B VISITED
A B C D E F G
1 0 1 0 1 0 0
C D
E E
PRINT :-
A C E
C
F G A
STACK
A B VISITED
A B C D E F G
1 0 1 0 1 0 1
C D
G
E E
PRINT :-
A C E G
C
F G A
STACK
A B VISITED
A B C D E F G
1 0 1 0 1 0 1
C D
E E
PRINT :-
A C E G
C
F G A
STACK
A B VISITED
A B C D E F G
1 0 1 0 1 1 1
C D
F
E E
PRINT :-
A C E G F
C
F G A
STACK
A B VISITED
A B C D E F G
1 0 1 0 1 1 1
C D
E E
PRINT :-
A C E G F
C
F G A
STACK
A B VISITED
A B C D E F G
1 0 1 1 1 1 1
C D
D
E E
PRINT :-
A C E G F D
C
F G A
STACK
A B VISITED
A B C D E F G
1 1 1 1 1 1 1
C D B
D
E E
PRINT :-
A C E G F D B
C
F G A
STACK
A B VISITED
A B C D E F G
1 1 1 1 1 1 1
C D
D
E E
PRINT :-
A C E G F D B
C
F G A
STACK
A B VISITED
A B C D E F G
1 1 1 1 1 1 1
C D
E E
PRINT :-
A C E G F D B
C
F G A
STACK
A B VISITED
A B C D E F G
1 1 1 1 1 1 1
C D
E PRINT :-
A C E G F D B
C
F G A
STACK
A B VISITED
A B C D E F G
1 1 1 1 1 1 1
C D
E PRINT :-
A C E G F D B
F G A
STACK
A B VISITED
A B C D E F G
1 1 1 1 1 1 1
C D
E PRINT :-
A C E G F D B
F G
CODE FOR
DEPTH FIRST SEARCH
1 2
4 3
5
6 7
CODE:-https://ideone.com/2lcwzP
Time complexity of DFS:-
CODE:-https://ideone.com/lacjxP
DIFFERENCES BETWEEN BFS AND DFS
1 2
Breadth First Search (BFS) Depth First Search (DFS)
BFS visit nodes level by level in DFS visit nodes of a graph depth
Graph. wise. It visits nodes until it reaches a
leaf or a node which doesn’t have
6 7
The End