Problem
Given n
nodes labeled from 0
to n - 1
and a list of undirected edges (each edge is a pair of nodes), write a function to find the number of connected components in an undirected graph.
Examples
Example 1:
|
|
Example 2:
|
|
Note
You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1]
is the same as [1, 0]
and thus will not appear together in edges.
Solution
Method 1 - DFS on Graph
We can use normal DFS to find number of components. But it is good to use Union Find Data structure.
An undirected graph is represented by:
- A number of nodes,
n
(labelled0
ton-1
). - A list of
edges
, each representing a connection between any two nodes.
To find the number of connected components:
- Think of this problem as identifying clusters of connected nodes in the graph.
- Use Graph Traversal Algorithms:
- DFS (Depth First Search) or BFS (Breadth First Search) to explore each connected component fully.
- Maintain an array to track visited nodes. Each time you encounter an unvisited node, it’s the start of a new connected component.
Algorithm
- Create an adjacency list to store the graph from the input edges.
- Use a
visited
array to track whether a node has been visited. - Perform a DFS or BFS from each unvisited node, marking all connected nodes as visited during traversal. For each traversal, increment the connected components count.
- Return the count of connected components.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(V + E)
due to DFS - 🧺 Space complexity:
O(V + E)
, Storing adjacency list usesO(V + E)
space, and visited array usesO(V)
space.
Method 2 - Union Find
This problem can be solved by using union-find beautifully. Initially, there are n nodes. The nodes that are involved in each edge is merged.
We can use Disjoint Set - Union by Rank Implementation
Here is the approach:
- Treat each node as its own component initially.
- Use the Union-Find/Disjoint Set data structure to group connected nodes together:
- Union Operation: Merge two sets containing two connected nodes.
- Find Operation: Identify the “parent” or representative of a set, enabling us to determine if two nodes belong to the same set.
- Path Compression: Optimisation to ensure that all nodes in a set point directly to their representative, making future lookups faster.
- After processing all edges, count the number of distinct sets (connected components).
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(E * α(V))
E
is the number of edges.α(V)
is the inverse Ackermann function, almost constant for practical purposes.
- 🧺 Space complexity:
O(V)
as space is used to store the parent and rank.