-
Notifications
You must be signed in to change notification settings - Fork 43
/
NumberOfConnectedComponentsUndirectedGraph.java
263 lines (226 loc) · 6.73 KB
/
NumberOfConnectedComponentsUndirectedGraph.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package LeetCodeJava.Graph;
// https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/description/?envType=list&envId=xoqag3yj
// https://leetcode.ca/all/323.html
// https://leetcode.ca/2016-10-18-323-Number-of-Connected-Components-in-an-Undirected-Graph/
import java.util.*;
public class NumberOfConnectedComponentsUndirectedGraph {
// V0
// IDEA : UNION FIND
// TODO : implement it
// https://www.youtube.com/watch?v=8f1XPm4WOUc
// public int countComponents(int n, int[][] edges) {
// return 0;
// }
// V1
// IDEA : UNION FIND
// https://github.com/neetcode-gh/leetcode/blob/main/java/0323-number-of-connected-components-in-an-undirected-graph.java
// https://www.youtube.com/watch?v=8f1XPm4WOUc
private int[] parent;
private int[] rank;
public int countComponents_1(int n, int[][] edges) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 1;
}
int result = n;
for (int i = 0; i < edges.length; i++) {
if (union_1(edges[i][0], edges[i][1]) == 1) {
result--;
}
}
return result;
}
private int find_1(int node) {
int result = node;
while (parent[result] != result) {
parent[result] = parent[parent[result]];
result = parent[result];
}
return result;
}
private int union_1(int n1, int n2) {
int p1 = this.find_1(n1);
int p2 = this.find_1(n2);
if (p1 == p2) {
return 0;
}
if (rank[p2] > rank[p1]) {
parent[p1] = p2;
rank[p2] += rank[p1];
} else {
parent[p2] = p1;
rank[p1] += rank[p2];
}
return 1;
}
// V1
// IDEA : UNION FIND (gpt)
// TODO : validate
static class UnionFind {
private int[] root;
private int[] rank;
public UnionFind(int size) {
root = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++) {
root[i] = i;
rank[i] = 1;
}
}
public int find(int x) {
if (root[x] == x) {
return x;
}
root[x] = find(root[x]); // Path compression
return root[x];
}
public boolean union(int x, int y) {
int rootX = find(x);
int rootY = find(y);
if (rootX != rootY) {
// Union by rank
if (rank[rootX] > rank[rootY]) {
root[rootY] = rootX;
} else if (rank[rootX] < rank[rootY]) {
root[rootX] = rootY;
} else {
root[rootY] = rootX;
rank[rootX] += 1;
}
return true;
}
return false;
}
public int getCount() {
Set<Integer> uniqueRoots = new HashSet<>();
for (int i = 0; i < root.length; i++) {
uniqueRoots.add(find(i));
}
return uniqueRoots.size();
}
}
public int countComponents_1_1(int n, int[][] edges) {
UnionFind uf = new UnionFind(n);
for (int[] edge : edges) {
uf.union(edge[0], edge[1]);
}
return uf.getCount();
}
// V2
// IDEA : UNION FIND
// https://leetcode.ca/2016-10-18-323-Number-of-Connected-Components-in-an-Undirected-Graph/
private int[] p;
public int countComponents_2(int n, int[][] edges) {
p = new int[n];
for (int i = 0; i < n; ++i) {
p[i] = i;
}
for (int[] e : edges) {
int a = e[0], b = e[1];
p[find(a)] = find(b);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
if (i == find(i)) {
++ans;
}
}
return ans;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
// V3
// IDEA : DFS
// https://www.cnblogs.com/cnoodle/p/14197652.html
public int countComponents_3(int n, int[][] edges) {
int count = 0;
List<List<Integer>> g = new ArrayList<>();
boolean[] visited = new boolean[n];
for (int i = 0; i < n; i++) {
g.add(new ArrayList<>());
}
for (int[] e : edges) {
g.get(e[0]).add(e[1]);
g.get(e[1]).add(e[0]);
}
for (int i = 0; i < n; i++) {
if (!visited[i]) {
count++;
dfs(visited, i, g);
}
}
return count;
}
private void dfs(boolean[] visited, int node, List<List<Integer>> g) {
visited[node] = true;
for (int next : g.get(node)) {
if (!visited[next]) {
dfs(visited, next, g);
}
}
}
// V4
// IDEA : UNION FIND
// https://www.cnblogs.com/cnoodle/p/14197652.html
public int countComponents_4(int n, int[][] edges) {
int count = n;
int[] parents = new int[n];
for (int i = 0; i < n; i++) {
parents[i] = i;
}
for (int[] edge : edges) {
int p = find(parents, edge[0]);
int q = find(parents, edge[1]);
if (p != q) {
parents[p] = q;
count--;
}
}
return count;
}
private int find(int[] parents, int i) {
while (parents[i] != i) {
parents[i] = parents[parents[i]]; // route compression
i = parents[i];
}
return i;
}
// V5
// IDEA : BFS
// https://www.cnblogs.com/cnoodle/p/14197652.html
public int countComponents_5(int n, int[][] edges) {
int count = 0;
List<List<Integer>> g = new ArrayList<>();
boolean[] visited = new boolean[n];
for (int i = 0; i < n; i++) {
g.add(new ArrayList<>());
}
for (int[] e : edges) {
g.get(e[0]).add(e[1]);
g.get(e[1]).add(e[0]);
}
for (int i = 0; i < n; i++) {
if (!visited[i]) {
count++;
Queue<Integer> queue = new LinkedList<>();
queue.offer(i);
while (!queue.isEmpty()) {
int index = queue.poll();
visited[index] = true;
for (int next : g.get(index)) {
if (!visited[next]) {
queue.offer(next);
}
}
}
}
}
return count;
}
}