2Brute2Best
🔥0

Number of Connected Components in an Undirected Graph

Medium·LeetCode #323 Premium Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You have a graph with n nodes labelled 0 to n − 1. The graph is undirected: each edges[i] = [a, b] is one link between nodes a and b, and it works in both directions.

A connected component is a group of nodes where every node can reach every other node through links, and no link leaves the group.

Return the number of connected components in the graph.

Input

n, the number of nodes, and edges, a list of [a, b] pairs.

Output

One integer: the number of connected components.

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

The two components are {0, 1, 2} and {3, 4}.

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

The path 0 – 1 – 2 – 3 – 4 links all five nodes into one component.

Example 3
Input: n = 4, edges = []
Output: 4

No links at all, so every node is its own component.

Constraints
  • • Nodes are labelled 0 to n − 1.
  • • Each edge is a pair [a, b] of node labels from 0 to n − 1.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“number of connected components”
→ Count the groups that the edges form → start with n groups and subtract the merges.
“undirected edges”
→ A link works both ways, so one union per edge is enough.
“nodes labelled 0 to n − 1”
→ A fixed set of items that all start alone → a DSU with n parents.
03

Approach Complexity Ladder

Brute Force — quick-find labelsO(N · E)O(N)

Label each node with its component; an edge renames a whole component

Optimal — DSU: start at n, subtract one per mergeO(E · α(N))O(N)

Union every edge; each union that really merges removes one from n

Brute Force Intuition

Start with label[v] = v. For each edge (a, b) with different labels, rename every node that has label[b] to label[a]. At the end, the number of distinct labels is the number of components.

Recognition rule for this sub-pattern:

Start with n groups; each successful union reduces the count by one.
Remember this · Union-Find (DSU)
Trigger
Edges arrive one by one, "are these connected?", merge equivalent items, count components, extra edge creating a cycle.
Think
Am I repeatedly merging groups and asking whether two things are in the same group?
Key question
What are the elements, and what does it mean to union two of them?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Union-Find (DSU) translate into production architectures and low-level memory mechanics.