Given an undirected graph $G$ with $n$ nodes and $m$ edges. We are required to find in it all the connected components, i.e, several groups of vertices such that within a group each vertex can be reached from another and no path exists between different groups.
1. An algorithm for solving the problem
- To solve the problem, we can use Depth First Search or Breadth First Search.
- In fact, we will be doing a series of rounds of DFS: The first round will start from first node and all the nodes in the first connected component will be traversed (found). Then we find the first unvisited node of the remaining nodes, and run Depth First Search on it, thus finding a second connected component. And so on, until all the nodes are visited.
- The total asymptotic running time of this algorithm is $O(n + m)$ : In fact, this algorithm will not run on the same vertex twice, which means that each edge will be seen exactly two times (at one end and at the other end).
2. Implementation
int n;
vector<int> g[MAXN] ;
bool used[MAXN] ;
vector<int> comp ;
void dfs(int v) {
used[v] = true ;
comp.push_back(v);
for (size_t i = 0; i < (int) g[v].size(); ++i) {
int to = g[v][i];
if (!used[to])
dfs(to);
}
}
void find_comps() {
for (int i = 0; i < n ; ++i)
used [i] = false;
for (int i = 0; i < n ; ++i)
if (!used[i]) {
comp.clear();
dfs(i);
cout << "Component:" ;
for (size_t j = 0; j < comp.size(); ++j)
cout << ' ' << comp[j];
cout << endl ;
}
}
- The most important function that is used is
find_comps()which finds and displays connected components of the graph. - The graph is stored in adjacency list representation, i.e
g[i]contains a list of vertices that have edges from the vertexi. The constantMAXNshould be set equal to the maximum possible number of vertices in the graph. - Vector
compcontains a list of nodes in the current connected component.
3. Practice Problems
Related posts:
Running with Obstacles
New Year and the Factorisation Collaboration
Little Artem and 2-SAT
Half-plane intersection - S&I Algorithm in O(Nlog N)
Java Program to Implement Find all Back Edges in a Graph
Definite Game
Wise Men (Hard Version)
Solving assignment problem using min-cost-flow
Minimum spanning tree - Kruskal with Disjoint Set Union
Java Program to Implement Find all Cross Edges in a Graph
Calculating the determinant of a matrix by Gauss
TOF
Zoning Restrictions
Limericks
Dice Tower
Cinema
Om Nom and Spiders
Booking System
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Cycles
Ehab and Path-etic MEXs
Fox and Perfect Sets
Falling Blocks
Egor and an RPG game
Numbers
New Year and Social Network
String Hashing
Breaking Good
Euclidean algorithm for computing the greatest common divisor
Interesting Array
Cut the pie
Game