> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Provinces Python Solution with Tests

> Tested Python solution for LeetCode 547 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 547, Medium. Topics: Depth-First Search, Breadth-First Search, Union-Find, Graph Theory. [View on LeetCode](https://leetcode.com/problems/number-of-provinces/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 547   # by problem number
lcpy gen -s number_of_provinces   # by problem name
```

## Problem

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 `isConnected` where `isConnected[i][j] = 1` if the `ith` city and the `jth` city are directly connected, and `isConnected[i][j] = 0` otherwise.

Return *the total number of **provinces***.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg)

```
Input: isConnected = [[1,1,0],[1,1,0],[0,0,1]]
Output: 2
```

![Example 2](https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg)

```
Input: isConnected = [[1,0,0],[0,1,0],[0,0,1]]
Output: 3
```

### 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]

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_provinces/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_provinces/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2)
    # Space: O(n)
    def find_circle_num(self, is_connected: list[list[int]]) -> int:
        n = len(is_connected)
        visited = [False] * n

        def dfs(city: int) -> None:
            visited[city] = True
            for neighbor in range(n):
                if is_connected[city][neighbor] == 1 and not visited[neighbor]:
                    dfs(neighbor)

        provinces = 0
        for city in range(n):
            if not visited[city]:
                dfs(city)
                provinces += 1

        return provinces
```

## Complexity

| Time   | Space |
| ------ | ----- |
| O(n^2) | O(n)  |

## Tags

[NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
