-
Notifications
You must be signed in to change notification settings - Fork 7
/
countConnectedComponenetsInUndirectedGraph.java
82 lines (66 loc) · 2.33 KB
/
countConnectedComponenetsInUndirectedGraph.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes),
write a function to find the number of connected components in an undirected graph.
//intution RUN BFS/DFS on every connected compoenent, update counter once current element has been explored
//BFS AND DFS SOLUTIONS PROVIED!!!!
//TC: O(V+E) to run BFS or BFS
//SC: O(V)
class Solution {
public int countComponentsDFS(int n, int[][] edges) {
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
for(int node = 0; node < n; node++) {
adjList.put(node, new ArrayList<>());
}
for(int[] edge : edges) {
adjList.get(edge[0]).add(edge[1]);
adjList.get(edge[1]).add(edge[0]);
}
int connectedComponents = 0;
HashSet<Integer> visited = new HashSet<>();
for(Integer node : adjList.keySet()) {
if(!visited.contains(node)) {
connectedComponents++;
dfs(adjList, visited, node);
}
}
return connectedComponents;
}
private void dfs(HashMap<Integer, List<Integer>> adjList, HashSet<Integer> visited, int node) {
visited.add(node);
for(Integer neighbor : adjList.get(node)) {
if(!visited.contains(neighbor)) {
dfs(adjList, visited, neighbor);
}
}
}
//BFS VERSION
public int countComponentsBFS(int n, int[][] edges) {
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
for(int node = 0; node < n; node++) {
adjList.put(node, new ArrayList<>());
}
for(int[] edge : edges) {
adjList.get(edge[0]).add(edge[1]);
adjList.get(edge[1]).add(edge[0]);
}
int connectedComponents = 0;
HashSet<Integer> visited = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
for(Integer node : adjList.keySet()) {
if(!visited.contains(node)) {
connectedComponents++;
queue.offer(node);
visited.add(node);
}
while(!queue.isEmpty()) {
int curr = queue.poll();
for(Integer neighbor : adjList.get(curr)) {
if(!visited.contains(neighbor)) {
queue.offer(neighbor);
visited.add(neighbor);
}
}
}
}
return connectedComponents;
}
}