-
Notifications
You must be signed in to change notification settings - Fork 0
/
Numberofconnectedcomponent.java
38 lines (33 loc) · 1.27 KB
/
Numberofconnectedcomponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Numberofconnectedcomponent {
/*
Runtime: 4 ms, faster than 59.19% of Java online submissions for Number of Connected Components in an Undirected Graph.
Memory Usage: 48.1 MB, less than 26.32% of Java online submissions for Number of Connected Components in an Undirected Graph.
*/
private void dfs(List<Integer>[] adjList, int[] visited, int startNode) {
visited[startNode] = 1;
for (int i = 0; i < adjList[startNode].size(); i++) {
if (visited[adjList[startNode].get(i)] == 0) {
dfs(adjList, visited, adjList[startNode].get(i));
}
}
}
public int countComponents(int n, int[][] edges) {
int components = 0;
int[] visited = new int[n];
List<Integer>[] adjList = new ArrayList[n];
for (int i = 0; i < n; i++) {
adjList[i] = new ArrayList<Integer>();
}
for (int i = 0; i < edges.length; i++) {
adjList[edges[i][0]].add(edges[i][1]);
adjList[edges[i][1]].add(edges[i][0]);
}
for (int i = 0; i < n; i++) {
if (visited[i] == 0) {
components++;
dfs(adjList, visited, i);
}
}
return components;
}
}