首先给出一个简易的并查集:
public class UnionFind {
static int[] f;
public UnionFind(int n) {
f = new int[n];
for (int i = 0; i < f.length; i++) {
f[i] = i;
}
}
static int find(int x) {
if (f[x] == x)
return x;
return f[x] = find(f[x]);
}
static void union(int x, int y) {
int a = find(x);
int b = find(y);
if (a != b) {
f[a] = b;
}
}
}
接下来给出一个完整的、时间效率很高的并查集。
昨天晚上看书的时候看见了并查集的实现,与之前见过的递归写法不同,在这里记录一下。
出处是《算法》第四版的配套网站。
/**
* The {@code UF} class represents a <em>union–find data type</em> (also known
* as the <em>disjoint-sets data type</em>). It supports the classic
* <em>union</em> and <em>find</em> operations, along with a <em>count</em>
* operation that returns the total number of sets.
* <p>
* The union–find data type models a collection of sets containing <em>n</em>
* elements, with each element in exactly one set. The elements are named 0
* through <em>n</em>–1. Initially, there are <em>n</em> sets, with each element
* in its own set. The <em>canonical element</em> of a set (also known as the
* <em>root</em>, <em>identifier</em>, <em>leader</em>, or <em>set
* representative</em>) is one distinguished element in the set. Here is a
* summary of the operations:
* <ul>
* <li><em>find</em>(<em>p</em>) returns the canonical element of the set
* containing <em>p</em>. The <em>find</em> operation returns the same value for
* two elements if and only if they are in the same set.
* <li><em>union</em>(<em>p</em>, <em>q</em>) merges the set containing element
* <em>p</em> with the set containing element <em>q</em>. That is, if <em>p</em>
* and <em>q</em> are in different sets, replace these two sets with a new set
* that is the union of the two.
* <li><em>count</em>() returns the number of sets.
* </ul>
* <p>
* The canonical element of a set can change only when the set itself changes
* during a call to <em>union</em>—it cannot change during a call to
* either <em>find</em> or <em>count</em>.
* <p>
* This implementation uses <em>weighted quick union by rank</em> with <em>path
* compression by halving</em>. The constructor takes Θ(<em>n</em>) time,
* where <em>n</em> is the number of elements. The <em>union</em> and
* <em>find</em> operations take Θ(log <em>n</em>) time in the worst case.
* The <em>count</em> operation takes Θ(1) time. Moreover, starting from
* an empty data structure with <em>n</em> sites, any intermixed sequence of
* <em>m</em> <em>union</em> and <em>find</em> operations takes
* <em>O</em>(<em>m</em> α(<em>n</em>)) time, where α(<em>n</em>) is
* the inverse of <a href =
* "https://en.wikipedia.org/wiki/Ackermann_function#Inverse">Ackermann's
* function</a>.
* <p>
* For alternative implementations of the same API, see {@link QuickUnionUF},
* {@link QuickFindUF}, and {@link WeightedQuickUnionUF}. For additional
* documentation, see <a href="https://algs4.cs.princeton.edu/15uf">Section
* 1.5</a> of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin
* Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
class UF {
private int[] parent; // parent[i] = parent of i
private byte[] rank; // rank[i] = rank of subtree rooted at i (never more than 31)
private int count; // number of components
/**
* Initializes an empty union-find data structure with {@code n} elements
* {@code 0} through {@code n-1}. Initially, each elements is in its own set.
*
* @param n
* the number of elements
* @throws IllegalArgumentException
* if {@code n < 0}
*/
public UF(int n) {
if (n < 0)
throw new IllegalArgumentException();
count = n;
parent = new int[n];
rank = new byte[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
/**
* Returns the canonical element of the set containing element {@code p}.
*
* @param p
* an element
* @return the canonical element of the set containing {@code p}
* @throws IllegalArgumentException
* unless {@code 0 <= p < n}
*/
// 这里通过循环,使得p及其所有之前是p的parent的变量与p在树的同一层
public int find(int p) {
validate(p);
while (p != parent[p]) {
parent[p] = parent[parent[p]]; // path compression by halving
p = parent[p];
}
return p;
}
/**
* Returns the number of sets.
*
* @return the number of sets (between {@code 1} and {@code n})
*/
public int count() {
return count;
}
/**
* Returns true if the two elements are in the same set.
*
* @param p
* one element
* @param q
* the other element
* @return {@code true} if {@code p} and {@code q} are in the same set;
* {@code false} otherwise
* @throws IllegalArgumentException
* unless both {@code 0 <= p < n} and {@code 0 <= q < n}
* @deprecated Replace with two calls to {@link #find(int)}.
*/
@Deprecated
public boolean connected(int p, int q) {
return find(p) == find(q);
}
/**
* Merges the set containing element {@code p} with the the set containing
* element {@code q}.
*
* @param p
* one element
* @param q
* the other element
* @throws IllegalArgumentException
* unless both {@code 0 <= p < n} and {@code 0 <= q < n}
*/
public void union(int p, int q) {
// union之前先find p和q使得两者均在各自树的第一层
// 这样之后如果再find p或者q,就会大幅减少时间
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ)
return;
// make root of smaller rank point to root of larger rank
if (rank[rootP] < rank[rootQ])
parent[rootP] = rootQ;
else if (rank[rootP] > rank[rootQ])
parent[rootQ] = rootP;
else {
// 这一步把Q挂在P上,使得P是Q的父亲结点,两个深度相同的树挂在一起,其中一个根结点是另一个树的根节点,挂起来之后的树的深度显然要自增。
parent[rootQ] = rootP;
rank[rootP]++;
}
count--;
}
// validate that p is a valid index
private void validate(int p) {
int n = parent.length;
if (p < 0 || p >= n) {
throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n - 1));
}
}
}