0% found this document useful (0 votes)
37 views22 pages

Breadth First Search

Uploaded by

ABHISHEK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views22 pages

Breadth First Search

Uploaded by

ABHISHEK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Graph Traversal

S.Bharathiraja,
Associate Professor,
School of Computer Science and Engineering,
VIT – University - Chennai
Graph Traversal
Types:

1. DFS Depth First Search

2. BFS  Breadth First Search


Graph Traversal

BFS Breadth First Search

The BFS begins at a root node and inspects all the

neighboring nodes. Then for each of those

neighbor nodes in turn, it inspects their neighbor

nodes which were unvisited, and so on.


Breadth First Search

B F

C
A
E
H
G D
Breadth First Search
B F A

C Queue
E A
H
G D
Result : A

Make use of Queue for performing BFS in a graph

Select a vertex randomly say ‘A’ and mark it visited


Breadth First Search
B F A B
C Queue
E A
H
G D
Result : A B

Adjacent vertices of ‘A’ are ‘B’, ‘D’ & ‘G’


Select ‘B’ and mark it visited. Enqueue ‘B’ on to Queue & update result
Breadth First Search
B F A B D
C Queue
E A
H
G D
Result : A B D

Adjacent vertices of ‘A’ are ‘B’, ‘D’ & ‘C’


Select ‘D’ and mark it visited. Enqueue ‘D’ on to Queue & update result
Breadth First Search
B F A B D G
C Queue
E A
H
G D
Result : A B D G

Adjacent vertices of ‘A’ are ‘B’, ‘D’ & ‘C’


Select ‘G’ and mark it visited. Enqueue ‘G’ on to Queue & update result
All adjacent vertices of ‘A’ are visited, so dequeue ‘A’ and pointer points
to ‘B’.
Breadth First Search
B F A
X B D G
C Queue
E A
H
G D Result : A B D G

Adjacent vertices of ‘B’ are ‘A’, ‘E’ & ‘F’


Select ‘E’ and mark it visited. Enqueue ‘E’ on to Queue & update result
Breadth First Search
B F A
X B D G E
C Queue
E A
H
G D Result : A B D G E

Adjacent vertices of ‘B’ are ‘A’, ‘E’ & ‘F’


‘E’ is already visited.
Select ‘F’ and mark it visited. Enqueue ‘F’ on to Queue & update result
Breadth First Search
B F A
X B D G E F
C Queue
E A
H
G D Result : A B D G E F

Adjacent vertices of ‘B’ are ‘A’, ‘E’ & ‘F’


All adjacent vertices of ‘B’ are visited, so dequeue ‘B’ and the pointer
points to ‘D’.
Breadth First Search
B F A B D G E F
X X
C Queue
E A
H
G D Result : A B D G E F

Adjacent vertices of ‘D’ are ‘A’ & ‘F’


‘A’ & ‘F’ are already visited, so dequeue ‘D’ from the queue.
Breadth First Search
B F A B D
X X X G E F
C Queue
E A
H
G D Result : A B D G E F

Adjacent vertices of ‘G’ are ‘A’ & ‘E’


Dequeue ‘G’ since both adjacent vertices are already visited.
Breadth First Search
B F A
X B
X D
X G
X E F
C Queue
E A
H
G D Result : A B D G E F

Adjacent vertices of ‘E’ are ‘B’ & ‘G’


Dequeue ‘E’ both adjacent vertices are visited so go back to queue
Breadth First Search
B F A
X B
X D
X G
X E
X F
C Queue
E A
H
G D Result : A B D G E F

Adjacent vertices of ‘F’ are ‘D’ & ‘C’


‘D’ is visited so mark ‘C’ as visited and enqueue it..
Depth First Search
B F A
X B
X D
X G
X E
X F C
C Queue
E A
H
G D Result : A B D G E F C

Adjacent vertices of ‘F’ are ‘D’ & ‘C’


Since adjacent vertices of ‘F’ are visited, dequeue ‘F’ from the queue.
Breadth First Search
B F A
X B
X D
X G
X E
X XF C
C Queue
E A
H
G D Result : A B D G E F C

Adjacent vertices of ‘C’ are ‘F’ & ‘H’


Vertex ‘F’ is visited so mark ‘H’ as visited and enqueue it.
Breadth First Search
B F A
X B
X D
X G
X E
X XF C H
C Queue
E A
H
G D Result : A B D G E F C H

Adjacent vertices of ‘C’ are ‘F’ & ‘H’


Adjacent vertices of ‘C’ are visited so dequeue ‘C’ from queue.
Breadth First Search
B F A
X B
X D
X G
X E
X XF C
X H
C Queue
E A
H
G D Result : A B D G E F C H

Adjacent vertices of ‘H’ is ‘C’


Adjacent vertices of ‘H’ is visited so dequeue ‘H’ from queue.
Breadth First Search
B F A
X B
X D
X G
X E
X XF C
X H
X
C Queue
E A
H
G D Result : A B D G E F C H

If Queue is empty then all the vertices have been visited as follows:

ABDGEFCH
Algorithm - BFS
BFS ( G, S )
{
Initialize queue Q;
Visited(S) = true;
Enqueue(Q,S);
While(!Q.empty) do
v = dequeu(Q);
print(v);
For all vertices w adjacent to v do
if(visited(w) == false) then
{ enqueue(Q,w);
visited(w) = true;
}
end for
End while
}
Exercise

Solution: a  b  c  d e  f  g

You might also like