1. 程式人生 > >普林斯頓演算法課第一週作業

普林斯頓演算法課第一週作業

public class Percolation {
	private WeightedQuickUnionUF uf;
	private WeightedQuickUnionUF uf_backwash;
	private int N;
	//private int count_n=0;//record the number of open site;
	private boolean[] ifopen;//an array record if the site is open; 
	
		   public Percolation(int N) {
			   if (N<=0) throw new IllegalArgumentException("N is<=0");
			   this.N = N;
			   int i;
			   uf=new WeightedQuickUnionUF((N+1)*(N)+N+1);
			   uf_backwash=new WeightedQuickUnionUF(N*N+N+1);
			   ifopen=new boolean[(N+1)*(N)+N+1];
			   for(i=1;i<=N;i++){
				   uf.union(0*N+1, 0*N+i);
				   uf_backwash.union(0*N+1, 0*N+i);
				   ifopen[0*N+i]=true;
				   uf.union((N+1)*N+1, (N+1)*N+i);  
		            ifopen[(N+1)*N+i] = true;  
			   }
			  }// create N-by-N grid, with all sites blocked
		   public void open(int i, int j){
			   if (i<1||i>N) throw new IndexOutOfBoundsException("row index i out of bounds");
			   if(j<1||j>N)  throw new IndexOutOfBoundsException("column index j out of bounds");

	               if(ifopen[i*N+j]) return;
	               ifopen[i*N+j]=true;
	               //count_n++;
	               if (ifopen[(i-1)*N+j]){  
	                   uf.union(i*N+j, (i-1)*N+j);  
	                   uf_backwash.union(i*N+j, (i-1)*N+j);  
	               }  
	               if (ifopen[(i+1)*N+j]){  
	                   uf.union(i*N+j, (i+1)*N+j);  
	                   if (i!=N){  
	                       uf_backwash.union(i*N+j, (i+1)*N+j);  
	                   }  
	               }  
	               if (j!=1 && ifopen[i*N+j-1]){  
	                   uf.union(i*N+j, i*N+j-1);  
	                   uf_backwash.union(i*N+j, i*N+j-1);  
	               }  
	               if (j!=N && ifopen[i*N+j+1]){  
	                   uf.union(i*N+j, i*N+j+1);  
	                   uf_backwash.union(i*N+j, i*N+j+1);  
	               }  
	               
			   
		   }// open site (row i, column j) if it is not already
		   public boolean isOpen(int i, int j){
			   if (i<1||i>N) throw new IndexOutOfBoundsException("row index i out of bounds");
			   if(j<1||j>N)  throw new IndexOutOfBoundsException("column index j out of bounds");
				   
			   return ifopen[i*N+j];
		   }// is site (row i, column j) open?
		   public boolean isFull(int i, int j){
			   if (i<1||i>N) throw new IndexOutOfBoundsException("row index i out of bounds");
			   if(j<1||j>N)  throw new IndexOutOfBoundsException("column index j out of bounds");
		        return uf_backwash.connected(i*N+j, 0*N+1) && ifopen[i*N+j]; 
		   } // is site (row i, column j) full?
		   public boolean percolates() {
			   return uf.connected(0*N+1, (N+1)*N+1);
		   }// does the system percolate?
		   public static void main(String[] args){
			   int N = StdIn.readInt();
			   Percolation pe=new Percolation(N);
			   pe.open(1, 1);
			   pe.open(2, 1);
			   System.out.println(pe.percolates());
		   }// test client, optional
		
}


第二個類:
public class PercolationStats {
   public PercolationStats(int N, int T)     // perform T independent experiments on an N-by-N grid
   public double mean()                      // sample mean of percolation threshold
   public double stddev()                    // sample standard deviation of percolation threshold
   public double confidenceLo()              // low  endpoint of 95% confidence interval
   public double confidenceHi()              // high endpoint of 95% confidence interval


   public static void main(String[] args)    // test client (described below)
}


這個類是用來求取統計概率,想法如下:
初始化一個N*N格子,並處於關閉狀態。
每次隨機選擇一個格子進行開啟操作,直到滲漏為止。
記錄此時開啟格子數與總格子數的比例P
重複N此得到最後的樣本平均值。
1.public PercolationStats(int N, int T)
進行T次關於N*N格子的獨立重複試驗。


進行一次迴圈,通過Percolation類建構函式,用到了書中給出的類庫
StdRandom.uniform(int N)  生成隨機數
StdStats.mean(double); 求均值
StdStats.stddev(double);求方差