Number of Provinces - Complete Solution Guide
Number of Provinces is LeetCode problem 547, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 i th city and the j th city are directly connected, and isConnected[i][j] = 0 otherwise. Return the total
Detailed Explanation
The problem asks us to find the number of provinces in a given undirected graph represented by an adjacency matrix. Each city represents a node in the graph, and `isConnected[i][j] = 1` means there's an edge between city `i` and city `j`. A province is a group of connected cities (nodes) where any two cities in the province are reachable from each other, either directly or indirectly. The input is an `n x n` adjacency matrix representing the connections between `n` cities. The output should be the total number of distinct provinces.
Solution Approach
The provided solutions use Depth-First Search (DFS) to explore each connected component. The algorithm iterates through each city. If a city hasn't been visited yet, it means a new province has been found. DFS is then initiated from that city to mark all connected cities as visited. This process continues until all cities have been checked.
Step-by-Step Algorithm
- Step 1: Initialize a `visited` array of size `n` (number of cities) to keep track of visited cities. Initially, all cities are marked as not visited (false).
- Step 2: Initialize a `provinces` counter to 0. This counter will store the number of provinces found.
- Step 3: Iterate through each city from 0 to `n-1`.
- Step 4: For each city `i`, check if it has been visited. If `visited[i]` is false, it means a new province has been found.
- Step 5: Increment the `provinces` counter by 1.
- Step 6: Perform a DFS starting from city `i`. The DFS function marks the current city as visited and then explores all its adjacent cities (neighbors).
- Step 7: In the DFS function, for each neighbor `v` of city `u`, if there's an edge between `u` and `v` (i.e., `isConnected[u][v] == 1`) and `v` hasn't been visited, recursively call DFS on `v`.
- Step 8: After the DFS is complete for city `i`, all cities in the same province as `i` will be marked as visited.
- Step 9: Continue iterating through the remaining cities. Repeat steps 4-8 until all cities have been processed.
- Step 10: Return the `provinces` counter, which represents the total number of provinces.
Key Insights
- Insight 1: The problem can be modeled as finding the number of connected components in a graph.
- Insight 2: Depth-First Search (DFS) or Breadth-First Search (BFS) are suitable algorithms to traverse the graph and identify connected components.
- Insight 3: Union Find data structure provides an efficient solution for connected component problems.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n)
Topics
This problem involves: Depth-First Search, Breadth-First Search, Union Find, Graph.
Companies
Asked at: DoorDash, Sprinklr, Two Sigma.