Data Structure (2130702)

BE | Semester-3   Summer-2016 | 06/09/2016

Q5) (a)

Explain Breadth First Search in graphs with an example.

Procedure : BFS (Vertex V)

This procedure traverse the graph G in BFS manner. V is a starting vertex to be explored. Q is a queue, visited[] is an array which tells you whether particular vertex is visited or not. W is a adjacent node of vertex V.
1. Initialize Q
2. [Marks visited of V as 1]
  visited [v]<--1
3. [Add vertex v to Q]
  InsertQueue(V)
4. [Repeat while Q is not empty]
  Repeat while Q is not empty
  v <-- RemoveFromQueue()
  For all vertices W adjacent to v
  if visited[w] is 0
  then visited[w] <--1
   InsertQueue(w)