Breadth First Traversal for a graph is similar to Breadth First Traversal of a tree. The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same node again. To avoid processing a node more than once, we use a boolean visited array. For simplicity, it is assumed that all vertices are reachable from the starting vertex.
For example, in the following graph, we start traversal from vertex 2. When we come to vertex 0, we look for all adjacent vertices of it. 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. A Breadth First Traversal of the following graph is 2, 0, 3, 1.

Following are C++ and Java implementations of simple Breadth First Traversal from a given source.

The C++ implementation uses adjacency list representation of graphs. STL‘s list container is used to store lists of adjacent nodes and queue of nodes needed for BFS traversal.

[ad type=”banner”]

PYTHON Programming:

[pastacode lang=”python” manual=”%23%20Program%20to%20print%20BFS%20traversal%20from%20a%20given%20source%0A%23%20vertex.%20BFS(int%20s)%20traverses%20vertices%20reachable%0A%23%20from%20s.%0Afrom%20collections%20import%20defaultdict%0A%20%0A%23%20This%20class%20represents%20a%20directed%20graph%20using%20adjacency%0A%23%20list%20representation%0Aclass%20Graph%3A%0A%20%0A%20%20%20%20%23%20Constructor%0A%20%20%20%20def%20__init__(self)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20default%20dictionary%20to%20store%20graph%0A%20%20%20%20%20%20%20%20self.graph%20%3D%20defaultdict(list)%0A%20%0A%20%20%20%20%23%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20def%20addEdge(self%2Cu%2Cv)%3A%0A%20%20%20%20%20%20%20%20self.graph%5Bu%5D.append(v)%0A%20%0A%20%20%20%20%23%20Function%20to%20print%20a%20BFS%20of%20graph%0A%20%20%20%20def%20BFS(self%2C%20s)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20all%20the%20vertices%20as%20not%20visited%0A%20%20%20%20%20%20%20%20visited%20%3D%20%5BFalse%5D*(len(self.graph))%0A%20%0A%20%20%20%20%20%20%20%20%23%20Create%20a%20queue%20for%20BFS%0A%20%20%20%20%20%20%20%20queue%20%3D%20%5B%5D%0A%20%0A%20%20%20%20%20%20%20%20%23%20Mark%20the%20source%20node%20as%20visited%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20queue.append(s)%0A%20%20%20%20%20%20%20%20visited%5Bs%5D%20%3D%20True%0A%20%0A%20%20%20%20%20%20%20%20while%20queue%3A%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Dequeue%20a%20vertex%20from%20queue%20and%20print%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20queue.pop(0)%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20s%2C%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Get%20all%20adjacent%20vertices%20of%20the%20dequeued%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20vertex%20s.%20If%20a%20adjacent%20has%20not%20been%20visited%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20then%20mark%20it%20visited%20and%20enqueue%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20i%20in%20self.graph%5Bs%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20visited%5Bi%5D%20%3D%3D%20False%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20queue.append(i)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%20True%0A%20%0A%20%0A%23%20Driver%20code%0A%23%20Create%20a%20graph%20given%20in%20the%20above%20diagram%0Ag%20%3D%20Graph()%0Ag.addEdge(0%2C%201)%0Ag.addEdge(0%2C%202)%0Ag.addEdge(1%2C%202)%0Ag.addEdge(2%2C%200)%0Ag.addEdge(2%2C%203)%0Ag.addEdge(3%2C%203)%0A%20%0Aprint%20%22Following%20is%20Breadth%20First%20Traversal%20(starting%20from%20vertex%202)%22%0Ag.BFS(2)%0A%20%0A%23%20This%20code%20is%20contributed%20by%20Neelam%20Yadav” message=”” highlight=”” provider=”manual”/]

Output:

Following is Breadth First Traversal (starting from vertex 2)
2 0 3 1

Note that the above code traverses only the vertices reachable from a given source vertex. All the vertices may not be reachable from a given vertex (example Disconnected graph). To print all the vertices, we can modify the BFS function to do traversal starting from all nodes one by one (Like the DFS modified version) .

Time Complexity: O(V+E) where V is number of vertices in the graph and E is number of edges in the graph.

[ad type=”banner”]