Graph Theory37 sections · 1633 units
Open in Course

LeetCode 547 Number of Provinces - Problem Statement

LeetCode 547

Problem: Number of Provinces (LeetCode 547547). You are given nn cities. Some are directly connected. Find how many provinces exist. A province is a group of directly or indirectly connected cities. Input is an adjacency matrix: isConnected[i][j] = 1 means cities ii and jj are connected.

This is a connected components problem. Each province is one component. Run the component counting algorithm: loop through cities, DFS from each unvisited city, count how many times you start a DFS. That is your answer.