Number of Connected Components in an Undirected Graph
Difficulty: Medium
Category: DSA
Topics: Depth-First Search, Breadth-First Search, Union Find, Graph
Asked at: Amazon, Facebook, LinkedIn, Microsoft, Apple
You are given an integer `n` representing the number of nodes in an undirected graph, labeled from `0` to `n - 1`. You are also given a 2D array `edges`, where each `edges[i] = [ai, bi]` indicates that there is an undirected edge between nodes `ai` and `bi`.
Return *the number of connected components* in the graph.
**Constraints:**
- `1 <= n <= 2000`
- `0 <= edges.length <= n * (n - 1) / 2`
- `edges[i].length == 2`
- `0 <= ai, bi < n`
- `ai != bi`
- There are no repeated edges.