Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathUnionFind.java
More file actions
Latest commit
104 lines (91 loc) · 2.54 KB
/
Copy pathUnionFind.java
File metadata and controls
104 lines (91 loc) · 2.54 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
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
packagecom.thealgorithms.searches;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.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.
*/
publicclassUnionFind {
privatefinalint[] p; // Parent array
privatefinalint[] 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
*/
publicUnionFind(intn) {
p = newint[n];
r = newint[n];
for (inti = 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
*/
publicintfind(inti) {
intparent = p[i];
if (i == parent) {
returni;
}
// Path compression
finalintresult = find(parent);
p[i] = result;
returnresult;
}
/**
* 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
*/
publicvoidunion(intx, inty) {
intr0 = find(x);
intr1 = find(y);
if (r1 == r0) {
return;
}
// Union by rank
if (r[r0] > r[r1]) {
p[r1] = r0;
} elseif (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
*/
publicintcount() {
List<Integer> parents = newArrayList<>();
for (inti = 0; i < p.length; i++) {
introot = find(i);
if (!parents.contains(root)) {
parents.add(root);
}
}
returnparents.size();
}
@Override
publicStringtoString() {
return"p " + Arrays.toString(p) + " r " + Arrays.toString(r) + "\n";
}
}