Follow these steps to carry out the breadth-first traversal of a graph:
- Add the first vertex of the graph into the queue. Any vertex can be chosen as a starting vertex.
- Then, repeat the following steps 3 to 8 until the queue is empty.
- Take out the vertex from the queue and store it in a variable, say v.
- Mark it as visited (the marking is done so that this vertex should not be traversed again).
- Display the marked vertex.
- Find out the adjacency vertices of the vertex v, and then perform steps 7 to 8 on each of them.
- If any of the adjacency vertices of v are not marked, mark it as visited.
- Add the adjacency vertex to the queue.
- Exit.
The program for the breadth-first traversal of a graph is as follows:
//breadthfirsttrav.c ...