| Learn | Algorithm - Breadth-First Search - Connected components of undirected graph using bfs |
|---|
| Connected Components Of Undirected Graph Using BFS. BFS can be used to find the connected components of an undirected graph (opens new window). We can also find if the given graph is connected or not. Our subsequent discussion assumes we are dealing with undirected graphs.The definition of a connected graph is: A graph is connected if there is a path between every pair of vertices. Following is a connected graph. Following graph is not connected and has 2 connected components:
BFS is a graph traversal algorithm. So starting from a random source node, if on termination of algorithm, all nodes are visited, then the graph is connected,otherwise it is not connected. PseudoCode for the algorithm. C implementation for finding the whether an undirected graph is connected or not: For Finding all the Connected components of an undirected graph, we only need to add 2 lines of code to the BFS function. The idea is to call BFS function until all vertices are visited. The lines to be added are: AND and we define the following function: |
Read Article
https://develop.horje.com/learn/1434/reference