Data Structure — Breadth First Traversal

  • Visited and
  • Not visited.
  • Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
  • Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
  • Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
  • 2 is also an adjacent vertex of 0.
  • If we don’t mark visited vertices, then 2 will be processed again and it will become a non-terminating process.

Implementation of BFS traversal:

Follow the below method to implement BFS traversal.

  • Declare a queue and insert the starting vertex.
  • Initialize a visited array and mark the starting vertex as visited.
  • Follow the below process till the queue becomes empty:
  • Remove the first vertex of the queue.
  • Mark that vertex as visited.
  • Insert all the unvisited neighbours of the vertex into the queue.
Following is Breadth First Traversal (starting from vertex 2) 
2 0 3 1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pushan Mukhopadhyay

Pushan Mukhopadhyay

3 Followers

I am a Tech lover and love to explore new technologies. I am also a Coder. I will share whatever I know and going to learn about this in future.