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 firstname.lastname@example.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.
- Count single node isolated sub-graphs in a disconnected graph
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Transpose graph
- Dominant Set of a Graph
- Sum of dependencies in a graph
- Biconnected graph
- Bridges in a graph
- Graph and its representations
- Hypercube Graph
- Graph representations using set and hash
- A Peterson Graph Problem
- Basic Properties of a Graph
- Graph Types and Applications
- Edge Coloring of a Graph