-
Notifications
You must be signed in to change notification settings - Fork 19.5k
/
UnionFind.java
104 lines (91 loc) · 2.54 KB
/
UnionFind.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
package com.thealgorithms.searches;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* The Union-Find data structure, also known as Disjoint Set Union (DSU),
* is a data structure that tracks a set of elements partitioned into
* disjoint (non-overlapping) subsets. It supports two main operations:
*
* 1. **Find**: Determine which subset a particular element is in.
* 2. **Union**: Join two subsets into a single subset.
*
* This implementation uses path compression in the `find` operation
* and union by rank in the `union` operation for efficiency.
*/
public class UnionFind {
private final int[] p; // Parent array
private final int[] r; // Rank array
/**
* Initializes a Union-Find data structure with n elements.
* Each element is its own parent initially.
*
* @param n the number of elements
*/
public UnionFind(int n) {
p = new int[n];
r = new int[n];
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
/**
* Finds the root of the set containing the element i.
* Uses path compression to flatten the structure.
*
* @param i the element to find
* @return the root of the set
*/
public int find(int i) {
int parent = p[i];
if (i == parent) {
return i;
}
// Path compression
final int result = find(parent);
p[i] = result;
return result;
}
/**
* Unites the sets containing elements x and y.
* Uses union by rank to attach the smaller tree under the larger tree.
*
* @param x the first element
* @param y the second element
*/
public void union(int x, int y) {
int r0 = find(x);
int r1 = find(y);
if (r1 == r0) {
return;
}
// Union by rank
if (r[r0] > r[r1]) {
p[r1] = r0;
} else if (r[r1] > r[r0]) {
p[r0] = r1;
} else {
p[r1] = r0;
r[r0]++;
}
}
/**
* Counts the number of disjoint sets.
*
* @return the number of disjoint sets
*/
public int count() {
List<Integer> parents = new ArrayList<>();
for (int i = 0; i < p.length; i++) {
int root = find(i);
if (!parents.contains(root)) {
parents.add(root);
}
}
return parents.size();
}
@Override
public String toString() {
return "p " + Arrays.toString(p) + " r " + Arrays.toString(r) + "\n";
}
}