Number of Provinces – Solution & Complexity
Solution Walkthrough
1. Understand the Goal
isConnectedis an n x n adjacency matrix over directly-connected cities; "province" means a connected component under the transitive closure of that direct-connection relation (if A-B and B-C are both direct connections, A and C are in the same province even with no direct A-C entry).- The answer is just the number of connected components in this graph.
2. Choose the Core Pattern
- Disjoint-set union (union-find): start with every city in its own set, then union the two endpoints of every direct connection. The final answer is the number of distinct sets remaining.
- Path compression (flattening each
findchain toward the root as you traverse it) and union by rank (attaching the shorter tree under the taller one) keep the amortized cost of each operation nearly constant, which is why DSU beats a plain DFS/BFS-per-node only in code simplicity here, not asymptotics — but it generalizes better to the "incremental connection updates" follow-up.
3. Build the Algorithm
- Initialize
parent[i] = ifor every city and a runningprovincescounter atn(every city starts as its own province). find(x)walks parent pointers to the root, compressing the path along the way so future lookups are faster.union(a, b): find both roots; if they're already equal, this connection is redundant — do nothing and report no merge happened. Otherwise attach the lower-rank root under the higher-rank root (bumping rank on a tie), and report that a merge happened.- Scan only the upper triangle of the matrix (
col > row, since it's symmetric) and callunionfor everyisConnected[row][col] == 1; decrementprovincesonly whenunionreports an actual merge (roots were different) — unioning two cities already in the same province must not double-decrement.
4. Check Edge Cases
- All cities connected to each other (all 1s): every union after the first in a component reports "already merged," so
provincesends at 1. - No city connected to any other except itself (identity matrix): no union ever succeeds, so
provincesstays atn. - A single city (
n == 1): the loop over pairs never runs, and the answer is correctly 1. - A chain like 1-2, 2-3 (but no direct 1-3 entry): after unioning (1,2) and (2,3),
find(1) == find(3)transitively through path compression, even though the matrix has no direct 1-3 edge.
5. Final Solution and Complexity
- Time complexity is O(n^2 α(n)).
- Space complexity is O(n).