In previous post, BFS only with a particular vertex is performed i.e. it is assumed that all vertices are reachable from the starting vertex. But in the case of disconnected graph or any vertex that is unreachable from all vertex, the previous implementation will not give the desired output, so in this post, a modification is done in BFS.

All vertices are reachable. So, for above graph simple BFS will work.

As in above graph a vertex 1 is unreachable from all vertex, so simple BFS wouldn’t work for it.
Just to modify BFS, perform simple BFS from each unvisited vertex of given graph.
// C++ implementation of modified BFS #include<bits/stdc++.h> using namespace std; // A utility function to add an edge in an // undirected graph. void addEdge(vector<int> adj[], int u, int v) { adj[u].push_back(v); } // A utility function to do BFS of graph // from a given vertex u. void BFSUtil(int u, vector<int> adj[], vector<bool> &visited;) { // Create a queue for BFS list<int> q; // Mark the current node as visited and enqueue it visited[u] = true; q.push_back(u); // 'i' will be used to get all adjacent vertices 4 // of a vertex list<int>::iterator i; while(!q.empty()) { // Dequeue a vertex from queue and print it u = q.front(); cout << u << " "; q.pop_front(); // Get all adjacent vertices of the dequeued // vertex s. If an adjacent has not been visited, // then mark it visited and enqueue it for (int i = 0; i != adj[u].size(); ++i) { if (!visited[adj[u][i]]) { visited[adj[u][i]] = true; q.push_back(adj[u][i]); } } } } // This function does BFSUtil() for all // unvisited vertices. void BFS(vector<int> adj[], int V) { vector<bool> visited(V, false); for (int u=0; u<V; u++) if (visited[u] == false) BFSUtil(u, adj, visited); } // Driver code int main() { int V = 5; vector<int> adj[V]; addEdge(adj, 0, 4); addEdge(adj, 1, 2); addEdge(adj, 1, 3); addEdge(adj, 1, 4); addEdge(adj, 2, 3); addEdge(adj, 3, 4); BFS(adj, V); return 0; } |
Output:
0 4 1 2 3
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Move weighting scale alternate under given constraints
- Breadth First Search or BFS for a Graph
- Depth First Search or DFS for a Graph
- Best First Search (Informed Search)
- Detect Cycle in a Directed Graph
- Find length of the largest region in Boolean Matrix
- Minimum edge reversals to make a root
- Find the minimum number of moves needed to move from one cell of matrix to another
- Print all paths from a given source to a destination using BFS
- Connected Components in an undirected graph
- Minimum number of edges between two vertices of a Graph
- Number of pair of positions in matrix which are not accessible
- Applications of Breadth First Traversal
- Shortest path in an unweighted graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)


