-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
455 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import java.util.Random; | ||
|
||
public class Main { | ||
|
||
/** | ||
* 测试并查集 | ||
* @param uf | ||
* @param m | ||
* @return | ||
*/ | ||
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) { | ||
//设置并查集的大小为10000 | ||
int size = 10000000; | ||
//设置对并查集的操作为10000 | ||
int m = 10000000; | ||
// double time1 = testUF(new UnionFind1(size), m); | ||
// double time2 = testUF(new UnionFind2(size), m); | ||
double time3 = testUF(new UnionFind3(size), m); | ||
double time4 = testUF(new UnionFind4(size), m); | ||
double time5 = testUF(new UnionFind5(size), m); | ||
double time6 = testUF(new UnionFind6(size), m); | ||
|
||
|
||
// System.out.println("UF1 need time :" + time1 + "s"); | ||
// System.out.println("UF2 need time :" + time2 + "s"); | ||
System.out.println("UF3 need time :" + time3 + "s"); | ||
System.out.println("UF3 need time :" + time4 + "s"); | ||
System.out.println("UF3 need time :" + time5 + "s"); | ||
System.out.println("UF3 need time :" + time6 + "s"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public interface UF { | ||
int getSize(); | ||
boolean isConnected(int p, int q); | ||
void unionElements(int p, int q); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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; | ||
} | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
return id.length; | ||
} | ||
|
||
@Override | ||
public boolean isConnected(int p, int q) { | ||
if (find(p) == find(q)){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 合并元素p和元素q的集合 | ||
* @param p | ||
* @param 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; | ||
} | ||
} | ||
} | ||
|
||
//查询元素p对应的集合的编号 | ||
private int find(int p){ | ||
if (p < 0 || p > id.length){ | ||
throw new IllegalArgumentException("p is out of bound."); | ||
} | ||
return id[p]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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; | ||
} | ||
|
||
/** | ||
* 查找过程,查找元素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; | ||
} | ||
|
||
@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 (qRoot == pRoot){ | ||
return; | ||
} | ||
parent[pRoot] = parent[qRoot]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
public class UnionFind3 implements UF { | ||
private int[] parent; | ||
private int[] sz;// sz[i] 表示以i为根的集合中元素的个数 | ||
|
||
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; | ||
} | ||
|
||
/** | ||
* 查找过程,查找元素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; | ||
} | ||
|
||
@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 (qRoot == pRoot){ | ||
return; | ||
} | ||
//让元素个数比较少的根节点去指向元素个数比较多的根节点 | ||
if (sz[pRoot] < sz[qRoot]){ | ||
parent[pRoot] = qRoot; | ||
//更新一下qRoot这棵数的元素个数的值 | ||
sz[qRoot] += sz[pRoot]; | ||
}else { | ||
parent[qRoot] = pRoot; | ||
sz[pRoot] += sz[qRoot]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
public class UnionFind4 implements UF { | ||
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; | ||
} | ||
|
||
@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 (qRoot == pRoot){ | ||
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.