定義接口
package unionfind;
/**
* 并查集接口
*/
public interface UF {
int getSize();
boolean isConnected(int p, int q); // find(p) == find(q) O(1)
void unionElements(int p, int q);
}
版本1
package unionfind;
/**
* 并查集版本1:數(shù)組中存放著集合的id
*/
public class UnionFind1 implements UF {
private int[] id; // 存放數(shù)據(jù)所屬集合的id,數(shù)組的下標本身就代表數(shù)據(jù)
// 然而在并查集里映胁,數(shù)據(jù)本身并不重要潦匈,數(shù)據(jù)和數(shù)據(jù)的關(guān)系才是討論的重點
public UnionFind1(int size) {
id = new int[size];
// 初始化時阳欲,每一個元素都是獨立的级解,分配在不同的集合中
for (int i = 0; i < id.length; i++) {
id[i] = i;
}
}
@Override
public int getSize() {
return id.length;
}
// 查找元素p所在的集合的編號
private int find(int p) {
// 檢查p的合法性
if (p < 0 || p >= id.length)
throw new IllegalArgumentException("p is out of bound.");
return id[p];
}
// 時間復雜度O(1)
// 查看元素p和元素q是否在同一個集合
@Override
public boolean isConnected(int p, int q) {
// 檢查p和q的集合id是否同一個
return find(p) == find(q);
}
/**
* 合并操作:將元素放在同一個集合
* 將元素的id設置為同一個
* union(1, 4)软瞎,將4的id改成和1相同撵孤,同時和4相同的id全部改成和1相同
*/
@Override
public void unionElements(int p, int q) {
int pID = find(p);
int qID = find(q);
// 如何p和q在同一個集合肆氓,return
if (pID == qID) {
return;
}
// 如何p和q不在同一個集合,遍歷所有元素久免,將和p(q)不在同一個集合的數(shù)據(jù)id全部改成p(q)所在集合的編號
// 時間復雜度O(n)
for (int i = 0; i < id.length; i++) {
if (id[i] == qID)
id[i] = pID; // 或者id[i] = qID
}
}
}
版本2
package unionfind;
/**
* 并查集版本2:森林結(jié)構(gòu)
*/
public class UnionFind2 implements UF {
private int[] parent;// 保存的數(shù)據(jù)是代表上一個節(jié)點的下標浅辙,當i == parent[i],代表根節(jié)點
public UnionFind2(int size) {
parent = new int[size];
for (int i = 0; i < parent.length; i++) {
parent[i] = i;
}
}
@Override
public int getSize() {
return parent.length;
}
// 找元素所在的根節(jié)點(集合)
// 時間復雜度O(h)妄壶,h代表p所在的樹的高度
private int find(int p) {
if (p < 0 || p >= parent.length)
throw new IllegalArgumentException("p is out of bound.");
while (parent[p] != p)
p = parent[p];
return p;
}
// O(h)
@Override
public boolean isConnected(int p, int q) {
return find(p) == find(q);
}
// 時間復雜度O(h)摔握,h代表p所在的樹的高度
@Override
public void unionElements(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot)
return;
parent[pRoot] = qRoot; // 或者parent[qRoot] = pRoot
}
}
版本3
package unionfind;
/**
* 并查集版本3:
* 基于size的優(yōu)化:
* 記錄每一棵樹的元素個數(shù)寄狼,
* 樹中節(jié)點少的指向樹中節(jié)點多的丁寄,避免出現(xiàn)退化成鏈表的極端情況
*/
public class UnionFind3 implements UF {
private int[] parent;// 保存的數(shù)據(jù)是代表上一個節(jié)點的下標氨淌,當i == parent[i],代表根節(jié)點
private int[] sz;
public UnionFind3(int size) {
parent = new int[size];
sz = new int[size];
for (int i = 0; i < parent.length; i++) {
parent[i] = i;
sz[i] = 1;
}
}
@Override
public int getSize() {
return parent.length;
}
// 找元素所在的根節(jié)點(集合)
// 時間復雜度O(h)伊磺,h代表p所在的樹的高度
private int find(int p) {
if (p < 0 || p >= parent.length)
throw new IllegalArgumentException("p is out of bound.");
while (parent[p] != p)
p = parent[p];
return p;
}
// O(h)
@Override
public boolean isConnected(int p, int q) {
return find(p) == find(q);
}
@Override
public void unionElements(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot)
return;
/*
基于版本2的改造邏輯:
不再是簡單粗暴的讓pRoot指向qRoot
而是在進行指向的時候盛正,判斷:
樹中節(jié)點少的指向樹中節(jié)點多的,并維護記錄節(jié)點個數(shù)的數(shù)組sz
*/
if (sz[pRoot] < sz[qRoot]) {
parent[pRoot] = qRoot;
sz[qRoot] += sz[pRoot];
} else {
parent[qRoot] = pRoot;
sz[pRoot] += sz[qRoot];
}
}
}
版本4
package unionfind;
/**
* 并查集版本4:
* 基于rank的優(yōu)化:
* 記錄每一棵樹的高度
* 樹中高度少的指向樹中高度多的
*/
public class UnionFind4 implements UF {
private int[] parent;// 保存的數(shù)據(jù)是代表上一個節(jié)點的下標屑埋,當i == parent[i]豪筝,代表根節(jié)點
private int[] rank;
public UnionFind4(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < parent.length; i++) {
parent[i] = i;
rank[i] = 1;
}
}
@Override
public int getSize() {
return parent.length;
}
// 找元素所在的根節(jié)點(集合)
// 時間復雜度O(h),h代表p所在的樹的高度
private int find(int p) {
if (p < 0 || p >= parent.length)
throw new IllegalArgumentException("p is out of bound.");
while (parent[p] != p)
p = parent[p];
return p;
}
// O(h)
@Override
public boolean isConnected(int p, int q) {
return find(p) == find(q);
}
@Override
public void unionElements(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot)
return;
if (rank[pRoot] < rank[qRoot]) {
parent[pRoot] = qRoot;
} else if(rank[pRoot] > rank[qRoot]) {
parent[qRoot] = pRoot;
} else { // 相等的時候
parent[qRoot] = pRoot;
rank[pRoot] += 1;
}
}
}
版本5
package unionfind;
/**
* 并查集版本5:路徑壓縮
* 由于并查集只關(guān)心節(jié)點最終的根節(jié)點摘能,中途經(jīng)歷的節(jié)點可以進行壓縮续崖。
*/
public class UnionFind5 implements UF {
private int[] parent;// 保存的數(shù)據(jù)是代表上一個節(jié)點的下標,當i == parent[i]团搞,代表根節(jié)點
private int[] rank;
public UnionFind5(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < parent.length; i++) {
parent[i] = i;
rank[i] = 1;
}
}
@Override
public int getSize() {
return parent.length;
}
// 找元素所在的根節(jié)點(集合)
// 時間復雜度O(h)严望,h代表p所在的樹的高度
private int find(int p) {
if (p < 0 || p >= parent.length)
throw new IllegalArgumentException("p is out of bound.");
while (parent[p] != p) {
// 指向父節(jié)點所指向的父節(jié)點。達到壓縮路徑的目的
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
// O(h)
@Override
public boolean isConnected(int p, int q) {
return find(p) == find(q);
}
@Override
public void unionElements(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot)
return;
if (rank[pRoot] < rank[qRoot]) {
parent[pRoot] = qRoot;
} else if(rank[pRoot] > rank[qRoot]) {
parent[qRoot] = pRoot;
} else { // 相等的時候
parent[qRoot] = pRoot;
rank[pRoot] += 1;
}
}
}
版本6
package unionfind;
/**
* 并查集版本6:路徑壓縮
* 由于并查集只關(guān)心節(jié)點最終的根節(jié)點逻恐,中途經(jīng)歷的節(jié)點可以進行壓縮像吻。
*/
public class UnionFind6 implements UF {
private int[] parent;// 保存的數(shù)據(jù)是代表上一個節(jié)點的下標,當i == parent[i]复隆,代表根節(jié)點
private int[] rank;
public UnionFind6(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < parent.length; i++) {
parent[i] = i;
rank[i] = 1;
}
}
@Override
public int getSize() {
return parent.length;
}
// 找元素所在的根節(jié)點(集合)
// 時間復雜度O(h)拨匆,h代表p所在的樹的高度
private int find(int p) {
if (p < 0 || p >= parent.length)
throw new IllegalArgumentException("p is out of bound.");
if (parent[p] != p)
// 指向父節(jié)點所指向的父節(jié)點。達到壓縮路徑的目的
parent[p] = find(parent[p]);
return parent[p];
}
// O(h)
@Override
public boolean isConnected(int p, int q) {
return find(p) == find(q);
}
@Override
public void unionElements(int p, int q) {
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot)
return;
if (rank[pRoot] < rank[qRoot]) {
parent[pRoot] = qRoot;
} else if(rank[pRoot] > rank[qRoot]) {
parent[qRoot] = pRoot;
} else { // 相等的時候
parent[qRoot] = pRoot;
rank[pRoot] += 1;
}
}
}
測試不同版本的并查集的性能
package unionfind;
import java.util.Random;
public class Main {
private static double testUF(UF uf, int m) {
int size = uf.getSize();
Random random = new Random();
long startTime = System.nanoTime();
// m控制進行幾輪union
for (int i = 0; i < m; i++) {
int a = random.nextInt(size);
int b = random.nextInt(size);
uf.unionElements(a, b);
}
// m控制進行幾輪isConnected
for (int i = 0; i < m; i++) {
int a = random.nextInt(size);
int b = random.nextInt(size);
uf.isConnected(a, b);
}
long endTime = System.nanoTime();
return (double) (endTime - startTime) / 1000000000;
}
public static void main(String[] args) {
int size = 10000000;
// int m = 10000;
int m = 10000000;
// UnionFind1 unionFind1 = new UnionFind1(size);
// System.out.println("UNIONFIND1:"+ testUF(unionFind1, m) + "s");
//
// UnionFind2 unionFind2 = new UnionFind2(size);
// System.out.println("UNIONFIND2:" + testUF(unionFind2, m) + "s");
UnionFind3 unionFind3 = new UnionFind3(size);
System.out.println("UNIONFIND3:" + testUF(unionFind3, m) + "s");
UnionFind4 unionFind4 = new UnionFind4(size);
System.out.println("UNIONFIND4:" + testUF(unionFind4, m) + "s");
UnionFind5 unionFind5 = new UnionFind5(size);
System.out.println("UNIONFIND5:" + testUF(unionFind5, m) + "s");
UnionFind6 unionFind6 = new UnionFind6(size);
System.out.println("UNIONFIND6:" + testUF(unionFind6, m) + "s");
}
}