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.
Just to modify BFS, perform simple BFS from each unvisited vertex of given graph.
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 email@example.com. 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.
- Count single node isolated sub-graphs in a disconnected graph
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Transpose graph
- Sum of dependencies in a graph
- Hypercube Graph
- Dominant Set of a Graph
- Bridges in a graph
- Graph and its representations
- Biconnected graph
- Graph Types and Applications
- Graph representations using set and hash
- Coloring a Cycle Graph
- Edge Coloring of a Graph
- A Peterson Graph Problem