You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [a_i, b_i] indicates an edge between a_i and b_i. Return the number of connected components.
"This is the quintessential connected component problem. Vertices are $0..n-1$. We build an adjacency list and count how many DFS explorations are initiated from the outer loop."
"Both approaches are interview favorites: DFS achieves $O(V + E)$ using adjacency lists; DSU achieves $O(V + E \cdot \alpha(V))$ starting with $count = n$ and subtracting 1 for each successful union."
"Whenever !visited[i] in the outer loop, city $i$ belongs to a new component. Increment count++ and exhaustively visit its entire component before continuing."
"Total time is $O(V + E)$ since every vertex and edge is touched twice (once from each endpoint). Space is $O(V + E)$ to store the adjacency list and $O(V)$ for the visited array."