mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Add tests, remove main
in UnionFind
(#5678)
This commit is contained in:
@ -4,11 +4,28 @@ 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;
|
||||
private final int[] r;
|
||||
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];
|
||||
@ -18,6 +35,13 @@ public class UnionFind {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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];
|
||||
|
||||
@ -25,12 +49,19 @@ public class UnionFind {
|
||||
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);
|
||||
@ -39,6 +70,7 @@ public class UnionFind {
|
||||
return;
|
||||
}
|
||||
|
||||
// Union by rank
|
||||
if (r[r0] > r[r1]) {
|
||||
p[r1] = r0;
|
||||
} else if (r[r1] > r[r0]) {
|
||||
@ -49,39 +81,24 @@ public class UnionFind {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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++) {
|
||||
if (!parents.contains(find(i))) {
|
||||
parents.add(find(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";
|
||||
}
|
||||
|
||||
// Tests
|
||||
public static void main(String[] args) {
|
||||
UnionFind uf = new UnionFind(5);
|
||||
System.out.println("init /w 5 (should print 'p [0, 1, 2, 3, 4] r [0, 0, 0, 0, 0]'):");
|
||||
System.out.println(uf);
|
||||
|
||||
uf.union(1, 2);
|
||||
System.out.println("union 1 2 (should print 'p [0, 1, 1, 3, 4] r [0, 1, 0, 0, 0]'):");
|
||||
System.out.println(uf);
|
||||
|
||||
uf.union(3, 4);
|
||||
System.out.println("union 3 4 (should print 'p [0, 1, 1, 3, 3] r [0, 1, 0, 1, 0]'):");
|
||||
System.out.println(uf);
|
||||
|
||||
uf.find(4);
|
||||
System.out.println("find 4 (should print 'p [0, 1, 1, 3, 3] r [0, 1, 0, 1, 0]'):");
|
||||
System.out.println(uf);
|
||||
|
||||
System.out.println("count (should print '3'):");
|
||||
System.out.println(uf.count());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user