Count Groups of Connected Cities From an Adjacency Matrix

Quick Overview

Given a symmetric adjacency matrix of cities, count how many groups of directly or indirectly connected cities there are. It tests graph traversal with depth-first or breadth-first search, union-find, and counting connected components in an undirected graph.

Count Groups of Connected Cities From an Adjacency Matrix

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

There are `n` cities. Some of them are connected, while some are not. If city `a` is connected directly with city `b`, and city `b` is connected directly with city `c`, then city `a` is connected indirectly with city `c`. A province is a group of directly or indirectly connected cities and no other cities outside of the group. You are given an `n x n` matrix `is_connected` where `is_connected[i][j] = 1` if city `i` and city `j` are directly connected, and `is_connected[i][j] = 0` otherwise. Return the total number of provinces, that is, the number of connected components in the graph. ### Function Signature ```python def find_circle_num(is_connected: list[list[int]]) -> int: ``` ### Rules - `is_connected` is an adjacency matrix of an undirected graph: city `i` is row `i` and column `i`, cities are numbered from `0` to `n - 1`. - The matrix is symmetric and every city is connected to itself (`is_connected[i][i] == 1`). - A city with no connection to any other city forms a province on its own. ### Constraints - `1 <= n <= 200` - `len(is_connected) == n` - `len(is_connected[i]) == n` - `is_connected[i][j]` is `1` or `0` - `is_connected[i][i] == 1` - `is_connected[i][j] == is_connected[j][i]` ### Examples ```text Input: is_connected = [[1,1,0],[1,1,0],[0,0,1]] Output: 2 ``` Cities 0 and 1 are directly connected and form one province. City 2 has no connections, so it forms another. ```text Input: is_connected = [[1,0,0],[0,1,0],[0,0,1]] Output: 3 ``` Only the diagonal is set, so every city is isolated and each one is its own province. ```text Input: is_connected = [[1,0,0,1],[0,1,1,0],[0,1,1,1],[1,0,1,1]] Output: 1 ``` The direct links are 0-3, 1-2 and 2-3. City 0 reaches city 1 through 3 and 2, so all four cities are in a single province even though 0 and 1 are not directly connected.

Overview: Given a symmetric adjacency matrix of cities, count how many groups of directly or indirectly connected cities there are. It tests graph traversal with depth-first or breadth-first search, union-find, and counting connected components in an undirected graph.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

There are n cities. Some of them are connected, while some are not. If city a is connected directly with city b, and city b is connected directly with city c, then city a is connected indirectly with city c.

A province is a group of directly or indirectly connected cities and no other cities outside of the group.

You are given an n x n matrix is_connected where is_connected[i][j] = 1 if city i and city j are directly connected, and is_connected[i][j] = 0 otherwise.

Return the total number of provinces, that is, the number of connected components in the graph.

Function Signature

def find_circle_num(is_connected: list[list[int]]) -> int:

Rules

  • is_connected is an adjacency matrix of an undirected graph: city i is row i and column i , cities are numbered from 0 to n - 1 .
  • The matrix is symmetric and every city is connected to itself ( is_connected[i][i] == 1 ).
  • A city with no connection to any other city forms a province on its own.

Constraints

  • 1 <= n <= 200
  • len(is_connected) == n
  • len(is_connected[i]) == n
  • is_connected[i][j] is 1 or 0
  • is_connected[i][i] == 1
  • is_connected[i][j] == is_connected[j][i]

Examples

Input: is_connected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2

Cities 0 and 1 are directly connected and form one province. City 2 has no connections, so it forms another.

Input: is_connected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3

Only the diagonal is set, so every city is isolated and each one is its own province.

Input: is_connected = [[1,0,0,1],[0,1,1,0],[0,1,1,1],[1,0,1,1]]
Output: 1

The direct links are 0-3, 1-2 and 2-3. City 0 reaches city 1 through 3 and 2, so all four cities are in a single province even though 0 and 1 are not directly connected.