Theory Code Visualisation

Breadth First Search (BFS)

In this technique ,first visit the starting vertex and then visit all the vertices adjacent to the Starting vertex. After this we pick these adjacent vertices one by one and visit their adjacent vertices and this process goes on .
bfsimg
During the algorithm, any vertex will be in one of the tree states-Initial, waiting, visited . At the start of the algorithm all vertices will be in initial state, when a vertex will be inserted in the queue its state will change from initial to waiting. When a vertex will be delected from queue and visited, its state will change from waiting to visited.
Initially queue is empty:
    1. Insert the starting vertex into the queue, change its state to waiting.
    2. Delete front element from the queue and visit it, change its state to visited.
    3. Look for the adjacent vertices of the deleted element, and from these insert only those vertices into the queue which are in the initial state. Change the state of all these inserted vertices from initial to waiting.
    4. Repeat step 2,3 until the queue is empty.