标签:网络 树的高度 height ima ext || imp 复杂 rri
public interface UF { int getSize(); boolean isConnected(int p, int q); void unionElements(int p, int q); }
public class UnionFind1 implements UF { private int[] id; public UnionFind1(int size) { id = new int[size]; for (int i = 0; i < id.length; i++) { id[i] = i; } } // 实现getSize方法 @Override public int getSize() { return id.length; } private int find(int p) { if (p < 0 || p >= id.length) { throw new IllegalArgumentException("p is out of bound."); } return id[p]; } // 实现isConnected方法,查看元素p与元素q是否所属同一个集合 @Override public boolean isConnected(int p, int q) { return id[p] == id[q]; } // 实现unionElements方法,合并元素p和元素q所属集合 @Override public void unionElements(int p, int q){ int pID = find(p); int qID = find(q); if(pID == qID){ return; } for(int i=0;i<id.length;i++){ if(id[i]==pID){ id[i] = qID; } } } }
public class UnionFind2 implements UF { private int[] parent; public UnionFind2(int size) { parent = new int[size]; for (int i = 0; i < size; i++) { parent[i] = i; } } @Override public int getSize() { return parent.length; } 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; } // 实现isConnected方法,判断元素p与元素q是否同属一个集合 @Override public boolean isConnected(int p, int q) { return parent[p] == parent[q]; } // 实现unionElements方法,合并元素p和元素q所在集合 @Override public void unionElements(int p, int q) { int pRoot = find(p); int qRoot = find(q); if (pRoot == qRoot) { return; } parent[pRoot] = qRoot; } }
isConnected O(h) 其中,h为树的高度
unionElements O(h)
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(); 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 = 10000; UnionFind1 uf1 = new UnionFind1(size); double time1 = testUF(uf1, m); System.out.println("Quick Find, time: " + time1 + " s"); UnionFind2 uf2 = new UnionFind2(size); double time2 = testUF(uf2, m); System.out.println("Quick Union, time: " + time2 + " s"); } }
Quick Find, time: 0.272248873 s
Quick Union, time: 0.001273318 s
public class UnionFind3 implements UF { private int[] parent; private int[] sz; public UnionFind3(int size) { parent = new int[size]; sz = new int[size]; for (int i = 0; i < size; i++) { parent[i] = i; sz[i] = 1; } } @Override public int getSize() { return parent.length; } 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; } // 实现isConnected方法,判断元素p与元素q是否同属一个集合 @Override public boolean isConnected(int p, int q) { return parent[p] == parent[q]; } // 实现unionElements方法,合并元素p和元素q所在集合 @Override public void unionElements(int p, int q) { int pRoot = find(p); int qRoot = find(q); if (pRoot == qRoot) { return; } if (sz[pRoot] < sz[qRoot]) { parent[pRoot] = qRoot; sz[qRoot] += sz[pRoot]; }else{ parent[qRoot] = pRoot; sz[pRoot]+=sz[qRoot]; } } }
public class UnionFind4 implements UF { private int[] parent; private int[] rank; 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; } 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; } // 实现isConnected方法,判断元素p与元素q是否同属一个集合 @Override public boolean isConnected(int p, int q) { return parent[p] == parent[q]; } // 实现unionElements方法,合并元素p和元素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 { parent[pRoot] = qRoot; rank[qRoot] += 1; } } }
public class UnionFind5 implements UF { private int[] parent; private int[] rank; 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; } 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; } // 实现isConnected方法,判断元素p与元素q是否同属一个集合 @Override public boolean isConnected(int p, int q) { return parent[p] == parent[q]; } // 实现unionElements方法,合并元素p和元素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 { parent[pRoot] = qRoot; rank[qRoot] += 1; } } }
public class UnionFind6 implements UF { private int[] parent; private int[] rank; public UnionFind6(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; } private int find(int p) { if (p < 0 || p >= parent.length) { throw new IllegalArgumentException("p is out of bound."); } if (p != parent[p]) { parent[p] = find(parent[p]); // 优化了这里 } return parent[p]; } // 实现isConnected方法,判断元素p与元素q是否同属一个集合 @Override public boolean isConnected(int p, int q) { return parent[p] == parent[q]; } // 实现unionElements方法,合并元素p和元素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 { parent[pRoot] = qRoot; rank[qRoot] += 1; } } }
标签:网络 树的高度 height ima ext || imp 复杂 rri
原文地址:https://www.cnblogs.com/xuezou/p/9305792.html