1. 程式人生 > >Programming Assignment 1: Percolation程式碼

Programming Assignment 1: Percolation程式碼

演算法第一部分

Algorithms, Part Iby Kevin Wayne, Robert SedgewickPrinceton University

Week1——程式設計作業1:Percolation 完整程式碼

Percolation.java

public class Percolation {
	private boolean[] NN;
	private int N;
	private WeightedQuickUnionUF WQUF;
	private WeightedQuickUnionUF WQUFBack;

	public Percolation(int N){
		this.N = N;
		NN = new boolean[N*N];
		WQUF = new WeightedQuickUnionUF(N*N + 4);
		WQUFBack = new WeightedQuickUnionUF(N*N + 1);
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				NN[i*N+j]=false;
			}
		}
	}	
	public void open(int i,int j){
		if(i<1 || i>N || j<1 || j>N){
			throw new java.lang.IndexOutOfBoundsException();
		}
		int ii = i - 1;
		int jj = j - 1;
		if(NN[ii * N + jj] ){
			return;
		}
		NN[ii * N + jj]=true;
		//the first line
		if(i==1){
			WQUF.union(ii*N+jj,N*N);
			WQUFBack.union(ii*N+jj,N*N);
		}
		//the last line
		if(i==N){
			WQUF.union(ii*N+jj,N*N+1);
		}
		//up
		if((ii-1)>=0 && NN[(ii-1)*N+jj]){
			WQUF.union(ii*N+jj,(ii-1)*N+jj);
			WQUFBack.union(ii*N+jj,(ii-1)*N+jj);
		}
		//left
		if((jj-1)>=0 && NN[ii*N+(jj-1)]){
			WQUF.union(ii*N+jj,ii*N+(jj-1));
			WQUFBack.union(ii*N+jj,ii*N+(jj-1));
		}
		//below
		if((ii+1)<N && NN[(ii+1)*N+jj]){
			WQUF.union(ii*N+jj,(ii+1)*N+jj);
			WQUFBack.union(ii*N+jj,(ii+1)*N+jj);
		}
		//right
		if((jj+1)<N && NN[ii*N+(jj+1)]){
			WQUF.union(ii*N+jj,ii*N+(jj+1));
			WQUFBack.union(ii*N+jj,ii*N+(jj+1));
		}
	}
	
	public boolean isOpen(int i,int j){
		if(i<1 || i>N || j<1 || j>N){
			throw new java.lang.IndexOutOfBoundsException();
		}
		return NN[(i-1)*N+(j-1)];
	}
	
	public boolean isFull(int i,int j){
		if(i<1 || i>N || j<1 || j>N){
			throw new java.lang.IndexOutOfBoundsException();
		}
		return NN[(i-1)*N+(j-1)] && WQUFBack.connected((i-1)*N+(j-1),N*N);
	}
	
	public boolean percolates(){
		return WQUF.connected(N*N,N*N+1);
	}
}

PercolationStats .java

public class PercolationStats {
	private double mean;
	private double stddev;
	private double confLow;
	private double confUp;
	
	public PercolationStats(int N,int T){
		if(N <= 0 || T <= 0){
			throw new java.lang.IllegalArgumentException();
		}
		double times[] = new double[T];
		for(int t=0;t<T;t++){
			Percolation perc = new Percolation(N);
			for(int a=0;a<N*N;a++){
				int pt = (int) (Math.random() * N * N);
				int i = pt / N + 1;
				int j = pt % N + 1;
				while(perc.isOpen(i, j)){
					pt = (int) (Math.random() * N * N);
					i = pt / N + 1;
					j = pt % N + 1;
				}
				perc.open(i, j);
				if(perc.percolates()){
					times[t] = ((double) a) / (N *N);
					break;
				}
			}
		}
		for (int t = 0; t < T; t++)
			mean += times[t];
		mean /= T;
		
		for (int t = 0; t < T; t++)
			stddev += (times[t] - mean) * (times[t] - mean);
		stddev /= (T - 1);
		stddev = Math.sqrt(stddev);
		
		confLow = mean - 1.96 * stddev / Math.sqrt((double)T);
		confUp = mean + 1.96 * stddev / Math.sqrt((double)T);
	}
	
	public double mean(){
		return mean;
	}
	
	public double stddev(){
		return stddev;
	}
	
	public double confidenceLo(){
		return confLow;
	}
	
	public double confidenceHi(){
		return confUp;
	}
	
	public static void main(String[] args){
		int N = StdIn.readInt();
		int T = StdIn.readInt();
		PercolationStats pers = new PercolationStats(N,T);
		StdOut.println("mean=" + pers.mean());
		StdOut.println("stddev=" + pers.stddev());
		StdOut.println("95% confidence interval=" + pers.confidenceLo() + ", " + pers.confidenceHi());
	}
}