数据结构源码 - UnionFind1
接口
public interface UnionFind {
int getSize();
boolean isConnected(int p, int q);
void unionElements(int p, int q);
}
实现类
import java.util.Random;
/**
* 第二种UnionFind实现
*/
public class UnionFind4 implements UnionFind {
private int[] parent;
private int[] rank; // rank[i]表示以i为根的集合所表示的树的层数
public UnionFind4(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++) {
parent[i] = i;
rank[i] = 1;
}
}
@Override
public int getSize() {
return parent.length;
}
/**
* 查找过程,查找元素p所对应的集合编号
* O(h)复杂度,h为树高度
* @param p
* @return
*/
private int find(int p) {
if (p < 0 && p >= parent.length)
throw new IllegalArgumentException("p is out of bound");
while (p != parent[p]) {
p = parent[p];
}
return p;
}
/**
* 查看元素p和元素q是否所属一个集合
* @param p
* @param q
* @return
*/
@Override
public boolean isConnected(int p, int q) {
return find(p) == find(q);
}
/**
* 合并元素p和元素q所属的集合
* O(h)复杂度,h为树的高度
* @param p
* @param 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[qRoot] < rank[pRoot]) {
parent[qRoot] = pRoot;
}
else { // rank[qRoot] == rank[pRoot]
parent[qRoot] = pRoot;
rank[pRoot] += 1;
}
}
private static double testUF(UnionFind uf, int m) {
int size = uf.getSize();
Random random = new Random();
long startTime = System.nanoTime();
for (int i = 0; i < m; i++) {
int a = random.nextInt(size);
int b = random.nextInt(size);
uf.unionElements(a, b);
}
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 (endTime - startTime) / 1000000000.0;
}
public static void main(String[] args) {
int size = 100000;
int m = 100000;
UnionFind1 uf1 = new UnionFind1(size);
System.out.println("UnionFind1 : " + testUF(uf1, m) + " s");
UnionFind2 uf2 = new UnionFind2(size);
System.out.println("UnionFind2 : " + testUF(uf2, m) + " s");
UnionFind3 uf3 = new UnionFind3(size);
System.out.println("UnionFind3 : " + testUF(uf3, m) + " s");
UnionFind4 uf4 = new UnionFind4(size);
System.out.println("UnionFind4 : " + testUF(uf4, m) + " s");
}
}
数据结构拆解 - UnionFind4
维护字段和内部类
private int[] parent;
private int[] rank; // rank[i]表示以i为根的集合所表示的树的层数
构造函数
public UnionFind4(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++) {
parent[i] = i;
rank[i] = 1;
}
}
改
/**
* 合并元素p和元素q所属的集合
* O(h)复杂度,h为树的高度
* @param p
* @param 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[qRoot] < rank[pRoot]) {
parent[qRoot] = pRoot;
}
else { // rank[qRoot] == rank[pRoot]
parent[qRoot] = pRoot;
rank[pRoot] += 1;
}
}
查
@Override
public int getSize() {
return parent.length;
}
/**
* 查找过程,查找元素p所对应的集合编号
* O(h)复杂度,h为树高度
* @param p
* @return
*/
private int find(int p) {
if (p < 0 && p >= parent.length)
throw new IllegalArgumentException("p is out of bound");
while (p != parent[p]) {
p = parent[p];
}
return p;
}
/**
* 查看元素p和元素q是否所属一个集合
* @param p
* @param q
* @return
*/
@Override
public boolean isConnected(int p, int q) {
return find(p) == find(q);
}
测试用例 - UnionFind4
private static double testUF(UnionFind uf, int m) {
int size = uf.getSize();
Random random = new Random();
long startTime = System.nanoTime();
for (int i = 0; i < m; i++) {
int a = random.nextInt(size);
int b = random.nextInt(size);
uf.unionElements(a, b);
}
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 (endTime - startTime) / 1000000000.0;
}
public static void main(String[] args) {
int size = 100000;
int m = 100000;
UnionFind1 uf1 = new UnionFind1(size);
System.out.println("UnionFind1 : " + testUF(uf1, m) + " s");
UnionFind2 uf2 = new UnionFind2(size);
System.out.println("UnionFind2 : " + testUF(uf2, m) + " s");
UnionFind3 uf3 = new UnionFind3(size);
System.out.println("UnionFind3 : " + testUF(uf3, m) + " s");
UnionFind4 uf4 = new UnionFind4(size);
System.out.println("UnionFind4 : " + testUF(uf4, m) + " s");
}
数据结构源码 - UnionFind5
接口
public interface UnionFind {
int getSize();
boolean isConnected(int p, int q);
void unionElements(int p, int q);
}
实现类
import java.util.Random;
/**
* 第二种UnionFind实现
*/
public class UnionFind5 implements UnionFind {
private int[] parent;
private int[] rank; // rank[i]表示以i为根的集合所表示的树的层数
public UnionFind5(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++) {
parent[i] = i;
rank[i] = 1;
}
}
@Override
public int getSize() {
return parent.length;
}
/**
* 查找过程,查找元素p所对应的集合编号
* O(h)复杂度,h为树高度
* @param p
* @return
*/
private int find(int p) {
if (p < 0 && p >= parent.length)
throw new IllegalArgumentException("p is out of bound");
while (p != parent[p]) {
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
/**
* 查看元素p和元素q是否所属一个集合
* @param p
* @param q
* @return
*/
@Override
public boolean isConnected(int p, int q) {
return find(p) == find(q);
}
/**
* 合并元素p和元素q所属的集合
* O(h)复杂度,h为树的高度
* @param p
* @param 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[qRoot] < rank[pRoot]) {
parent[qRoot] = pRoot;
}
else { // rank[qRoot] == rank[pRoot]
parent[qRoot] = pRoot;
rank[pRoot] += 1;
}
}
private static double testUF(UnionFind uf, int m) {
int size = uf.getSize();
Random random = new Random();
long startTime = System.nanoTime();
for (int i = 0; i < m; i++) {
int a = random.nextInt(size);
int b = random.nextInt(size);
uf.unionElements(a, b);
}
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 (endTime - startTime) / 1000000000.0;
}
public static void main(String[] args) {
int size = 10000000;
int m = 10000000;
// UnionFind1 uf1 = new UnionFind1(size);
// System.out.println("UnionFind1 : " + testUF(uf1, m) + " s");
//
// UnionFind2 uf2 = new UnionFind2(size);
// System.out.println("UnionFind2 : " + testUF(uf2, m) + " s");
UnionFind3 uf3 = new UnionFind3(size);
System.out.println("UnionFind3 : " + testUF(uf3, m) + " s");
UnionFind4 uf4 = new UnionFind4(size);
System.out.println("UnionFind4 : " + testUF(uf4, m) + " s");
UnionFind5 uf5 = new UnionFind5(size);
System.out.println("UnionFind5 : " + testUF(uf5, m) + " s");
}
}
数据结构拆解 - UnionFind5
维护字段和内部类
private int[] parent;
private int[] rank; // rank[i]表示以i为根的集合所表示的树的层数
构造函数
public UnionFind5(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++) {
parent[i] = i;
rank[i] = 1;
}
}
改
/**
* 合并元素p和元素q所属的集合
* O(h)复杂度,h为树的高度
* @param p
* @param 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[qRoot] < rank[pRoot]) {
parent[qRoot] = pRoot;
}
else { // rank[qRoot] == rank[pRoot]
parent[qRoot] = pRoot;
rank[pRoot] += 1;
}
}
查
@Override
public int getSize() {
return parent.length;
}
/**
* 查找过程,查找元素p所对应的集合编号
* O(h)复杂度,h为树高度
* @param p
* @return
*/
private int find(int p) {
if (p < 0 && p >= parent.length)
throw new IllegalArgumentException("p is out of bound");
while (p != parent[p]) {
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
/**
* 查看元素p和元素q是否所属一个集合
* @param p
* @param q
* @return
*/
@Override
public boolean isConnected(int p, int q) {
return find(p) == find(q);
}
测试用例 - UnionFind5
private static double testUF(UnionFind uf, int m) {
int size = uf.getSize();
Random random = new Random();
long startTime = System.nanoTime();
for (int i = 0; i < m; i++) {
int a = random.nextInt(size);
int b = random.nextInt(size);
uf.unionElements(a, b);
}
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 (endTime - startTime) / 1000000000.0;
}
public static void main(String[] args) {
int size = 10000000;
int m = 10000000;
// UnionFind1 uf1 = new UnionFind1(size);
// System.out.println("UnionFind1 : " + testUF(uf1, m) + " s");
//
// UnionFind2 uf2 = new UnionFind2(size);
// System.out.println("UnionFind2 : " + testUF(uf2, m) + " s");
UnionFind3 uf3 = new UnionFind3(size);
System.out.println("UnionFind3 : " + testUF(uf3, m) + " s");
UnionFind4 uf4 = new UnionFind4(size);
System.out.println("UnionFind4 : " + testUF(uf4, m) + " s");
UnionFind5 uf5 = new UnionFind5(size);
System.out.println("UnionFind5 : " + testUF(uf5, m) + " s");
}