Number of Provinces - Problem
Finding Connected Components in a City Network
Imagine you're a data analyst for a transportation company studying city connectivity patterns. You have
A province is defined as a group of cities that are all connected to each other (either directly or indirectly), with no connections to cities outside the group. Your task is to determine how many such provinces exist.
You're given an
•
•
• The matrix is symmetric:
• Each city is connected to itself:
Goal: Return the total number of provinces (connected components) in the network.
Imagine you're a data analyst for a transportation company studying city connectivity patterns. You have
n cities, and some pairs of cities are directly connected by roads. However, cities can also be indirectly connected through other cities - if city A connects to city B, and city B connects to city C, then A and C are indirectly connected.A province is defined as a group of cities that are all connected to each other (either directly or indirectly), with no connections to cities outside the group. Your task is to determine how many such provinces exist.
You're given an
n × n adjacency matrix isConnected where:•
isConnected[i][j] = 1 means cities i and j are directly connected•
isConnected[i][j] = 0 means no direct connection• The matrix is symmetric:
isConnected[i][j] = isConnected[j][i]• Each city is connected to itself:
isConnected[i][i] = 1Goal: Return the total number of provinces (connected components) in the network.
Input & Output
example_1.py — Three Cities, Two Provinces
$
Input:
isConnected = [[1,1,0],[1,1,0],[0,0,1]]
›
Output:
2
💡 Note:
Cities 0 and 1 are directly connected, forming one province. City 2 is isolated, forming a second province.
example_2.py — Three Cities, Three Provinces
$
Input:
isConnected = [[1,0,0],[0,1,0],[0,0,1]]
›
Output:
3
💡 Note:
No cities are connected to each other, so each city forms its own province.
example_3.py — Four Cities, One Province
$
Input:
isConnected = [[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]
›
Output:
1
💡 Note:
All cities are connected to each other either directly or indirectly, forming one large province.
Visualization
Tap to expand
Understanding the Visualization
1
Map the connections
The adjacency matrix shows direct relationships between cities
2
Explore each group
Starting from an unvisited city, explore all cities reachable through connections
3
Mark visited cities
Once a city is part of a province, mark it to avoid counting again
4
Count provinces
Each DFS or Union-Find component represents one province
Key Takeaway
🎯 Key Insight: This problem is fundamentally about finding connected components in an undirected graph. Whether using DFS or Union-Find, we're grouping cities that can reach each other through any path of direct connections.
Time & Space Complexity
Time Complexity
O(n² × α(n))
We check n² matrix entries, each union/find operation takes nearly O(1) with path compression
⚠ Quadratic Growth
Space Complexity
O(n)
Parent and rank arrays each take O(n) space
⚡ Linearithmic Space
Constraints
- 1 ≤ n ≤ 200
- n == isConnected.length
- n == isConnected[i].length
- isConnected[i][j] is 1 or 0
- isConnected[i][i] == 1
- isConnected[i][j] == isConnected[j][i]
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code