Breadth First Search
Breadth First Search
S.Bharathiraja,
Associate Professor,
School of Computer Science and Engineering,
VIT – University - Chennai
Graph Traversal
Types:
B F
C
A
E
H
G D
Breadth First Search
B F A
C Queue
E A
H
G D
Result : A
If Queue is empty then all the vertices have been visited as follows:
ABDGEFCH
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