Repository navigation
Expand file tree
/
Copy pathDisjointSet.java
More file actions
74 lines (63 loc) · 1.75 KB
/
Copy pathDisjointSet.java
File metadata and controls
74 lines (63 loc) · 1.75 KB
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
/*
Disjoint Set Union (Union-Find) in Java
----------------------------------------
Features:
- Path Compression (optimizes find operation)
- Union by Rank (balances tree height)
Time Complexity:
- find(): O(α(N)) ~ constant
- union(): O(α(N)) ~ constant
*/
class DisjointSet {
int[] parent;
int[] rank;
// Constructor: initialize DSU for 'n' elements
DisjointSet(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
// Find with path compression
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]); // compress path
}
return parent[x];
}
// Union by rank
void union(int x, int y) {
int rootX = find(x);
int rootY = find(y);
if (rootX == rootY) return; // already in same set
if (rank[rootX] < rank[rootY]) {
parent[rootX] = rootY;
} else if (rank[rootX] > rank[rootY]) {
parent[rootY] = rootX;
} else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
// Example usage
public static void main(String[] args) {
DisjointSet dsu = new DisjointSet(5);
dsu.union(0, 2);
dsu.union(4, 2);
dsu.union(3, 1);
// Should print "Connected"
if (dsu.find(4) == dsu.find(0)) {
System.out.println("Connected");
} else {
System.out.println("Not Connected");
}
// Should print "Not Connected"
if (dsu.find(1) == dsu.find(0)) {
System.out.println("Connected");
} else {
System.out.println("Not Connected");
}
}
}