Finding Connected Components in a City 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] = 1

Goal: 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
City Network Province DetectionAdjacency Matrix: 0 1 2 30 [1,1,0,1]1 [1,1,0,0]2 [0,0,1,0]3 [1,0,0,1]Graph Representation:0123Detected Provinces:Province 1Cities: 0, 1, 3Size: 3 citiesProvince 2Cities: 2Size: 1 cityAlgorithm Steps:1. Start DFS from city 0 (unvisited)2. Visit connected cities: 0→1, 0→33. Mark cities 0,1,3 as visited4. Increment province count: 15. Start DFS from city 2 (unvisited)6. No connections, increment count: 27. All cities visited, return count: 2Result: 2 Provinces Found
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

n
2n
Quadratic Growth
Space Complexity
O(n)

Parent and rank arrays each take O(n) space

n
2n
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]
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
25.7K Views
High Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen