Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emahtab/number-of-provinces
https://github.com/emahtab/number-of-provinces
connected-components depth-first-search graph leetcode
Last synced: 22 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/number-of-provinces
- Owner: eMahtab
- Created: 2021-02-12T10:02:21.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-09-18T09:57:24.000Z (3 months ago)
- Last Synced: 2024-09-18T13:13:48.748Z (3 months ago)
- Topics: connected-components, depth-first-search, graph, leetcode
- Homepage:
- Size: 46.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Number of provinces
## https://leetcode.com/problems/number-of-provincesThere 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.
![Number of provinces](number-of-provinces.JPG?raw=true "Number of provinces")
## Approach :
In graph, components which are connected can be reached starting from any single node of the connected group. Thus, to find the number of connected components, we start from every node which isn't visited right now and apply DFS starting with it. We increment the count of connected components for every new starting node.## Implementation 1 : DFS
```java
class Solution {
public int findCircleNum(int[][] isConnected) {
int n = isConnected.length;
boolean[] visited = new boolean[n];
int components = 0;
for(int vertex = 0; vertex < n; vertex++) {
if(!visited[vertex]) {
visited[vertex] = true;
components++;
visitNeighbors(vertex, isConnected, visited);
}
}
return components;
}
private void visitNeighbors(int vertex, int[][] isConnected, boolean[] visited) {
for(int v = 0; v < isConnected.length; v++) {
if(isConnected[vertex][v] == 1 && !visited[v]) {
visited[v] = true;
visitNeighbors(v, isConnected, visited);
}
}
}
}```
### Caution : StackOverflowError
Note that we need the `visited` array to mark the nodes/vertices that we have already visited otherwise we will run into StackOverflowError.
Even though its naive but lets say this loud, one vertex can not be part of two different graph components.![StackOverflowError](graph.JPG?raw=true "StackOverflowError")
## Implementation 2 : BFS
```java
class Solution {
public int findCircleNum(int[][] isConnected) {
if(isConnected == null || isConnected.length == 0)
return 0;int province = 0;
int totalNodes = isConnected.length;
Set visited = new HashSet<>();
Queue queue = new ArrayDeque<>();
for(int vertex = 0; vertex < totalNodes; vertex++){
if(!visited.contains(vertex)) {
province++;
queue.add(vertex);
while(!queue.isEmpty()) {
int u = queue.remove();
if(visited.contains(u)) continue;
visited.add(u);
for(int v = 0; v < totalNodes; v++) {
if(v != u && isConnected[u][v] == 1) {
if(!visited.contains(v))
queue.add(v);
}
}
}
}
}
return province;
}
}
```## Time Complexity : O(N^2) for both the DFS and BFS implementation.