求解系统的渗透阈值——基于union-find算法

 

问题描述:http://coursera.cs.princeton.edu/algs4/assignments/percolation.html

Percolation的求解基于Union-Find算法,这里用了Quick Find和Weighted Quick Union算法,并对两种算法进行了比较。

首先建立渗透模型类:

/ **
*  @Author DXH924 
*  Xidian University
*/
/*渗透模型*/
import java.lang.Math;
public class Percolation{
	static int[][] a;//虚拟化网格
	static int[] id;//每个网格对应的id,相同id的网格视为连通
	
	public int size;//网格数量
	public int cnt=0;//“开启”的网格数
	public int top, bottom;//增加两个虚拟化的网格(顶部和底部)
	public Percolation(int N){
		a=new int[N][N];
		size=N*N+2;
		top=size-2;
		bottom=size-1;
		id=new int[size];
		
		for (int i=0; i=0){
				q=p-N;
				if (a[i-1][j]==1)
					union(p, q);
			}	
			if (j+1=0){
				q=p-1;
				if (a[i][j-1]==1)
					union(p, q);
			}	
	}//连通与(i, j)相邻的网格
	
	public double test(int N){
		while (!percolates()){
			int i=(int)(Math.random()*N);
			int j=(int)(Math.random()*N);
			set(N);
		}
		double mean=cnt*1.0/(size-2);
		return mean;
	}//计算渗透阈值
}//based on Quick Find

/*QuickFind类*/
public class QF extends Percolation{
	public QF(int N){
		super(N);
	}
}

/*WeightedQuickUnion类*/
public class WQU extends Percolation{
	public int[] sz;//连通分量的大小
	public WQU(int N){
		super(N);
		sz=new int[size];
		for (int i=0; i

计算及测试类: 

/*计算及测试类*/
public class PercolationStats {
	int N, T;//N为系统规模,T为测试数据组数
	double sample[];//每组测试数据样本值
	double mean=0, stddev, lo, hi;//渗透阈值,标准差以及置信区间
	double start, end, runtime;//运行时间

	public PercolationStats(int N, int T, String arg){
		this.N=N;
		this.T=T;
		sample=new double[T];
		
		start=System.currentTimeMillis();
        if (arg.equals("Quick Find"))
			for (int i=0; i

测试样例:

enter N and T:
20 50
Quick Find
N = 20, T = 50
mean           = 0.59345
stddev         = 0.05346153013225148
confidenceLow  = 0.5786312198882635
confidenceHigh = 0.6082687801117366
running time   = 14.0ms

Weighted Quick Union
N = 20, T = 50
mean           = 0.59695
stddev         = 0.04694843567409778
confidenceLow  = 0.583936557565348
confidenceHigh = 0.609963442434652
running time   = 8.0ms

When N = 20 ,
Weighted Quick Union is 1.75 times faster than Quick Find

Quick Find
N = 40, T = 50
mean           = 0.6013999999999999
stddev         = 0.02381052974065492
confidenceLow  = 0.5948000578790416
confidenceHigh = 0.6079999421209583
running time   = 49.0ms

Weighted Quick Union
N = 40, T = 50
mean           = 0.59295
stddev         = 0.031969283328385896
confidenceLow  = 0.5840885667637791
confidenceHigh = 0.6018114332362209
running time   = 34.0ms

When N = 40 ,
Weighted Quick Union is 1.4411764705882353 times faster than Quick Find

Quick Find
N = 80, T = 50
mean           = 0.5915874999999999
stddev         = 0.01903813368268796
confidenceLow  = 0.5863103985761594
confidenceHigh = 0.5968646014238405
running time   = 453.0ms

Weighted Quick Union
N = 80, T = 50
mean           = 0.59128125
stddev         = 0.021882378493217065
confidenceLow  = 0.5852157641373505
confidenceHigh = 0.5973467358626494
running time   = 48.0ms

When N = 80 ,
Weighted Quick Union is 9.4375 times faster than Quick Find

Quick Find
N = 160, T = 50
mean           = 0.5930671875
stddev         = 0.010708300388289247
confidenceLow  = 0.5900989980667396
confidenceHigh = 0.5960353769332604
running time   = 6405.0ms

Weighted Quick Union
N = 160, T = 50
mean           = 0.5936234375
stddev         = 0.012259872074749474
confidenceLow  = 0.5902251743372303
confidenceHigh = 0.5970217006627697
running time   = 198.0ms

When N = 160 ,
Weighted Quick Union is 32.34848484848485 times faster than Quick Find

Quick Find
N = 320, T = 50
mean           = 0.5923541015624999
stddev         = 0.006205852182211583
confidenceLow  = 0.5906339270993554
confidenceHigh = 0.5940742760256444
running time   = 99309.0ms

Weighted Quick Union
N = 320, T = 50
mean           = 0.5916140625000001
stddev         = 0.0081841638093851
confidenceLow  = 0.5893455280306407
confidenceHigh = 0.5938825969693596
running time   = 807.0ms

When N = 320 ,
Weighted Quick Union is 123.05947955390334 times faster than Quick Find
各种union-find算法在N个触点成本时的性能特点(最坏情况)
算法 构造函数 union() find()
quick-find N N 1
quick-union N 树的高度 树的高度
加权quick-union N lgN lgN
路径压缩加权quick-union N →1 →1
理想情况 N 1 1

 

 

 

 

 

 

 

你可能感兴趣的:(排序算法)