Exploring the “Number of Provinces” 🌍🏰: Coding Interview Question

Ever thought of countries and provinces in terms of computer science? Today, you’ll discover that cities and their connections can form a graph, and deciphering this graph can be fun!

Tech Sauce
3 min readAug 26, 2023

📜 Problem Statement

Imagine a country divided into cities, where some cities are directly connected, while others are isolated. Your goal is to determine the number of provinces. A province is a group of cities that are interconnected, either directly or indirectly.

For example, given a matrix:

[[1,1,0],
[1,1,0],
[0,0,1]]

The answer is 2. Here's why:

  • City 1 is connected to City 2. Together, they form one province.
  • City 3 stands alone, forming the second province.

So, how can you solve this?

🤔 Intuition

When you think about cities and their connections, it’s quite similar to nodes in a graph and their edges. A city can be represented by a node, and a direct connection between two cities can be represented by an edge.

In this problem, our task is to find how many connected components (or groups of interconnected nodes) are present in this graph.

💡 Solution

The most efficient approach for this problem is to use Depth First Search (DFS).

  1. Treat each city as a node in a graph.
  2. Visit each city, and from that city, visit all its directly connected neighbors using DFS.
  3. Every time we start visiting from an unvisited city, it signifies a new province.

Let’s dive into the code!

public class Solution {
public int findCircleNum(int[][] isConnected) {
int provinces = 0;
boolean[] visited = new boolean[isConnected.length];

for (int i = 0; i < isConnected.length; i++) {
if (!visited[i]) {
dfs(isConnected, visited, i);
provinces++;
}
}

return provinces;
}

private void dfs(int[][] isConnected, boolean[] visited, int city) {
visited[city] = true;

for (int j = 0; j < isConnected.length; j++) {
if (isConnected[city][j] == 1 && !visited[j]) {
dfs(isConnected, visited, j);
}
}
}
}

📚 Explanation

  • We start with zero provinces.
  • We create an array visited to keep track of cities we've visited.
  • For every city we haven’t visited, we start a DFS to explore all cities connected to it. Each such traversal indicates a province.
  • Inside the dfs method, we mark the current city as visited and then explore all of its directly connected cities. If a city is directly connected and not visited, we recursively visit it.
  • The number of provinces is the number of times we had to invoke the DFS method from our main function.

⏲️ Time Complexity

The time complexity is O(n²), where n is the number of cities. This is because, in the worst case, we might need to visit all cities and check all their connections.

🌌 Space Complexity

The space complexity is O(n), considering the space required for the visited array and the call stack in the case of deep recursion.

🎓 Conclusion

This problem gives us a fresh perspective on the concept of graph traversal. By looking at cities and their connections, we learn how interconnected components play a fundamental role in solving real-world problems with computer science principles.

So, the next time you think of cities and their connections, remember that they can be visualized and solved with a simple DFS traversal!

🌟 Stay Tuned! If you’ve liked this post, make sure to follow my blog for more!

Further Learning at DSAGuide.com
For an in-depth exploration of data structures, algorithms, and coding interview solutions, I invite you to visit DSAGuide.com. Elevate your understanding with our detailed guides on essential data structures, algorithms and coding interview problem solving.

--

--

Tech Sauce
Tech Sauce

Written by Tech Sauce

Everything Software Engineering ...

No responses yet