Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Graph Traversals (BFS and DFS)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 47

Graph Traversals

(BFS and DFS)


There are two types of graph traversal
algorithms:-

1.Breadth First Search

2.Depth First Search


Breadth First Search
BFS is a traversing algorithm where 1 2
we should start traversing from a
selected vertex and traverse the
graph layerwise thus exploring the
adjacent vertices. After completing
4 3
all the adjacent vertices, it moves
further to check another vertex and 5
checks its adjacent vertices again.
BFS is generally implemented using
a queue. 6 7
VISITED:-
1 2 1 2 3 4 5 6 7

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.

2.Select the first element of the queue,pop it,and


change its value to true in visited array and print it. 4 3
3. Visit the adjacent nodes of the selected node if
not visited before and push them into the end of the
queue.
5
4. Keep repeating steps 2 and 3 until the queue is
empty. 6 7
VISITED:-
1 2 1 2 3 4 5 6 7

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:-

a)Adjacency List:- O(V+E)


b)Adjacency Matrix :- O(V²)

Space complexity of BFS:- O(V)

where V is the number of vertices of the graph


and E is the number of edges of the graph
BFS for Disconnected Graph

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.

2.) Take the top element of the stack ,add it to


the visited list and print it.
4 3
3.) Now add any unvisited adjacent node of
the selected node on to the top of the stack.If 5
such node is not present, pop out the selected
node.

4.) Keep repeating steps 2 and 3 until the


6 7
stack is empty.
STACK
A B VISITED
A B C D E F G

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:-

a)Adjacency List:- O(V+E)


b)Adjacency Matrix :- O(V²)

Space complexity of DFS :- O(V)

where V is the number of vertices of the graph


and E is the number of edges of the graph
DFS OF DISCONNECTED GRAPH

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

A node is fully explored before any


non-visited nodes.

Exploration of a node is suspended


4 3
other can begin. as soon as another unexplored is
found.

Uses Queue data structure to Uses Stack data structure to store 5


store unexplored nodes. unexplored nodes.

6 7
The End

You might also like