Skip to content
AI360Xpert
Beta
Difficulty: MediumGraphs

Number of Connected Components in an Undirected Graph

Problem in Plain English

Given a node count and undirected edges, count how many separate connected groups exist. Isolated nodes each count as their own group.

Problem Statement

You are given n nodes labeled 0 to n - 1 and undirected edges with edges[i] = [a, b].

Return the number of connected components: maximal groups of mutually reachable nodes. A node with no edges forms a component of its own.

Constraints

  • 1 <= n <= 2000
  • 0 <= edges.length <= 5000
  • edges[i].length == 2
  • 0 <= a, b < n
  • No repeated edges.

Examples

Example 1

Input
n = 5, edges = [[0,1],[1,2],[3,4]]
Output
2

Nodes 0, 1 and 2 link together while nodes 3 and 4 form a second group.

Example 2

Input
n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]
Output
1

The chain links every node, so the whole graph is a single component.

Example 3

Input
n = 4, edges = []
Output
4

All four nodes are isolated, so each is its own connected component. Looking only at endpoints would miss every one.

Example 4

Input
n = 5, edges = [[0,1],[1,2],[2,0],[3,4]]
Output
2

The triangle contains one group, not three merges: its last edge closes a cycle. Nodes 3 and 4 make the other group, so the answer is 2.

Intuition

A connected component contains all nodes reachable from one another. In Example 1, edges 0-1 and 1-2 join three nodes, while 3-4 joins a separate pair. Begin with five individual groups and let edges connect them: there are three genuine merges, leaving two groups. A cycle does not create a new merge once its endpoints are already connected, and a node absent from every edge must still be counted.

Approaches

Label Propagation

Solution Details

Reveal Label Propagation: intuition, complexity, and code

Hints

Hint 1
A node with no edges is a component by itself, so start counting from n and work down.
Hint 2
Each edge can merge at most two groups, which suggests tracking merges rather than recounting.
Hint 3
Successful unions equal n minus the component count, so count merges and subtract.

Edge Cases

  • With no edges the answer is n, including n = 1. Initialization still costs O(n).
  • A graph can contain a connected group plus isolated nodes: n = 4 and [[0,1]] returns 3.
  • Cycle edges do not reduce the counter: [[0,1],[1,2],[2,0]] on three nodes returns 1.
  • Reversing edge endpoints or changing edge order does not change connectivity. Reverse path order can greatly increase label-propagation sweeps.
  • At the maximum 2000 nodes, a chain connects all nodes; 5000 edges can still leave isolated nodes outside the subgraph they occupy.

Common Mistakes and Interview Tips

  • Counting only nodes that occur in edges loses isolated components. Start from all n labels or singleton sets.
  • Subtracting one for every edge undercounts whenever an edge closes a cycle. Decrement only when roots differ.
  • Comparing immediate parents rather than roots can mistake indirectly connected nodes for separate groups. Always find both representatives.
  • Stopping label propagation after one sweep depends on edge order and fails on a reverse-ordered chain. Require an entire unchanged sweep.
  • Treating rank as set size or exact current height after compression leads to incorrect updates. Rank increases only for equal-rank root merges.
  • Calling the union-find bound strictly linear hides alpha(n). Explain amortized cost and include O(n) initialization for an edgeless graph.

Key Takeaway

Counting groups in undirected graphs is union-find home turf: merge each edge once and read the survivor count.