Programming Assignment 1: Percolation

来源:互联网 发布:unity3d htc vive开发 编辑:程序博客网 时间:2024/05/17 19:58

Programming Assignment 1: Percolation

作业网址如下:
http://coursera.cs.princeton.edu/algs4/assignments/percolation.html

正文
————————————————————–
第一章的作业需要制作两个类
第一个Percolation包含五个方法,主要是用来打开渗漏的格子。
第二个类PercolationStats 同样有五个方法,用来找出N*N格子中,能够产生渗漏的阈值概率的95%置信区间。
先说一下大概的想法和原理。
官网上提供有这道题目的思路,就是在创建格子的过程中首先要在顶层和底层各加一层,
这样的话在测试是否渗漏时直接测试(0,1)和(N+1,1)就好了
首先来说下第一个类:
官网的API已经写好:
[java] view plain copy
print?
  1. public class Percolation {  
  2.    public Percolation(int N)               // create N-by-N grid, with all sites blocked  
  3.    public void open(int i, int j)          // open site (row i, column j) if it is not open already  
  4.    public boolean isOpen(int i, int j)     // is site (row i, column j) open?  
  5.    public boolean isFull(int i, int j)     // is site (row i, column j) full?  
  6.    public boolean percolates()             // does the system percolate?  
  7.   
  8.   
  9.    public static void main(String[] args   // test client (optional)  
  10. }  
public class Percolation {   public Percolation(int N)               // create N-by-N grid, with all sites blocked   public void open(int i, int j)          // open site (row i, column j) if it is not open already   public boolean isOpen(int i, int j)     // is site (row i, column j) open?   public boolean isFull(int i, int j)     // is site (row i, column j) full?   public boolean percolates()             // does the system percolate?   public static void main(String[] args   // test client (optional)}

1.构造函数 public Percolation(int N)
运用了虚拟节点的方法,把坐标(i,j)转化为(i*N+J),这样能更直观的找到坐标点的位置。
可以直接使用quick-find 和quick-union。
为了实现这个目标,调用WeightedQuickUnionUF创建了一个((N+1)*(N)+N+1)的一位数组。
第零排和第N+1排用来判断是否渗漏,而第零列被整体抛弃。
这样的做法是浪费了一定的存储空间,若非为了查找坐标方便,采用N*N+2个格子也是足够用的。
2.public void open(int i, int j)
判断该点是否打开,如果打开则结束。未打开则与上下左右四个点连接。
3.public boolean isOpen(int i, int j)
只需要返回该点的开闭状态值;
4.public boolean isFull(int i, int j)
判断该点是否与顶部联通。
5.public boolean percolates()  
判断是否渗漏
也就是检查(0,1)和(N+1,1);
注意:题目中提到了一个回流问题(如图)

由于底部的一排是联通的,那么所有与底部相连的点将会出现“灌水”。
为了解决这个问题,就要再建立一个不包含最底层的格子。在isFull()中使用该数组进行检查
如果底部的格子确实没有与顶部的虚拟点连通,是不会“灌满”的。
全部代码如下:
[java] view plain copy
print?
  1. public class Percolation {  
  2.     private WeightedQuickUnionUF uf;  
  3.     private WeightedQuickUnionUF uf_backwash;  
  4.     private int N;  
  5.     //private int count_n=0;//record the number of open site;  
  6.     private boolean[] ifopen;//an array record if the site is open;   
  7.       
  8.            public Percolation(int N) {  
  9.                if (N<=0throw new IllegalArgumentException(“N is<=0”);  
  10.                this.N = N;  
  11.                int i;  
  12.                uf=new WeightedQuickUnionUF((N+1)*(N)+N+1);  
  13.                uf_backwash=new WeightedQuickUnionUF(N*N+N+1);  
  14.                ifopen=new boolean[(N+1)*(N)+N+1];  
  15.                for(i=1;i<=N;i++){  
  16.                    uf.union(0*N+10*N+i);  
  17.                    uf_backwash.union(0*N+10*N+i);  
  18.                    ifopen[0*N+i]=true;  
  19.                    uf.union((N+1)*N+1, (N+1)*N+i);    
  20.                     ifopen[(N+1)*N+i] = true;    
  21.                }  
  22.               }// create N-by-N grid, with all sites blocked  
  23.            public void open(int i, int j){  
  24.                if (i<1||i>N) throw new IndexOutOfBoundsException(“row index i out of bounds”);  
  25.                if(j<1||j>N)  throw new IndexOutOfBoundsException(“column index j out of bounds”);  
  26.   
  27.                    if(ifopen[i*N+j]) return;  
  28.                    ifopen[i*N+j]=true;  
  29.                    //count_n++;  
  30.                    if (ifopen[(i-1)*N+j]){    
  31.                        uf.union(i*N+j, (i-1)*N+j);    
  32.                        uf_backwash.union(i*N+j, (i-1)*N+j);    
  33.                    }    
  34.                    if (ifopen[(i+1)*N+j]){    
  35.                        uf.union(i*N+j, (i+1)*N+j);    
  36.                        if (i!=N){    
  37.                            uf_backwash.union(i*N+j, (i+1)*N+j);    
  38.                        }    
  39.                    }    
  40.                    if (j!=1 && ifopen[i*N+j-1]){    
  41.                        uf.union(i*N+j, i*N+j-1);    
  42.                        uf_backwash.union(i*N+j, i*N+j-1);    
  43.                    }    
  44.                    if (j!=N && ifopen[i*N+j+1]){    
  45.                        uf.union(i*N+j, i*N+j+1);    
  46.                        uf_backwash.union(i*N+j, i*N+j+1);    
  47.                    }    
  48.                      
  49.                  
  50.            }// open site (row i, column j) if it is not already  
  51.            public boolean isOpen(int i, int j){  
  52.                if (i<1||i>N) throw new IndexOutOfBoundsException(“row index i out of bounds”);  
  53.                if(j<1||j>N)  throw new IndexOutOfBoundsException(“column index j out of bounds”);  
  54.                      
  55.                return ifopen[i*N+j];  
  56.            }// is site (row i, column j) open?  
  57.            public boolean isFull(int i, int j){  
  58.                if (i<1||i>N) throw new IndexOutOfBoundsException(“row index i out of bounds”);  
  59.                if(j<1||j>N)  throw new IndexOutOfBoundsException(“column index j out of bounds”);  
  60.                 return uf_backwash.connected(i*N+j, 0*N+1) && ifopen[i*N+j];   
  61.            } // is site (row i, column j) full?  
  62.            public boolean percolates() {  
  63.                return uf.connected(0*N+1, (N+1)*N+1);  
  64.            }// does the system percolate?  
  65.            public static void main(String[] args){  
  66.                int N = StdIn.readInt();  
  67.                Percolation pe=new Percolation(N);  
  68.                pe.open(11);  
  69.                pe.open(21);  
  70.                System.out.println(pe.percolates());  
  71.            }// test client, optional  
  72.           
  73. }  
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}


第二个类:
[java] view plain copy
print?
  1. public class PercolationStats {  
  2.    public PercolationStats(int N, int T)     // perform T independent experiments on an N-by-N grid  
  3.    public double mean()                      // sample mean of percolation threshold  
  4.    public double stddev()                    // sample standard deviation of percolation threshold  
  5.    public double confidenceLo()              // low  endpoint of 95% confidence interval  
  6.    public double confidenceHi()              // high endpoint of 95% confidence interval  
  7.   
  8.   
  9.    public static void main(String[] args)    // test client (described below)  
  10. }  
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);求方差

全部代码如下:
[java] view plain copy
print?
  1. public class PercolationStats {  
  2.     private double staT[];    
  3.     private double sta_mean;    
  4.     private double sta_stddev;    
  5.     private int N;  
  6.    public PercolationStats(int N, int T){  
  7.       staT=new double[T];  
  8.       this.N=N;  
  9.       int times=0;  
  10.       if(N<=0throw new IllegalArgumentException();  
  11.       if(T<=0throw new IllegalArgumentException();  
  12.       while(times<T){  
  13.           Percolation pe=new Percolation(N);  
  14.           int count=0;  
  15.           while(true){  
  16.               count++;  
  17.               while(true){  
  18.                   int x = StdRandom.uniform(N) + 1;    
  19.                   int y = StdRandom.uniform(N) + 1;  
  20.                   if(pe.isOpen(x, y)){  
  21.                       continue;  
  22.                   }else{  
  23.                       pe.open(x, y);  
  24.                       break;  
  25.                   }  
  26.   
  27.               }  
  28.               if(pe.percolates()){  
  29.                   staT[times]=(double)count/((double)N*(double)N);  
  30.                   break;  
  31.               }  
  32.              }  
  33.           times++;  
  34.      }  
  35.       this.sta_mean = StdStats.mean(staT);    
  36.       this.sta_stddev = StdStats.stddev(staT);  
  37.    }// perform T independent computational experiments on an N-by-N grid  
  38.    public double mean(){  
  39.        return this.sta_mean;  
  40.    }// sample mean of percolation threshold  
  41.    public double stddev(){  
  42.        return this.sta_stddev;  
  43.    } // sample standard deviation of percolation threshold  
  44.    public double confidenceLo(){  
  45.        return this.sta_mean-1.96*this.sta_stddev/Math.sqrt(N);  
  46.    }// returns lower bound of the 95% confidence interval  
  47.    public double confidenceHi(){  
  48.        return this.sta_mean+1.96*this.sta_stddev/Math.sqrt(N);  
  49.    }             // returns upper bound of the 95% confidence interval  
  50.    public static void main(String[] args){  
  51.         
  52.             int N = StdIn.readInt();    
  53.             int T = StdIn.readInt();    
  54.             PercolationStats percolationStats = new PercolationStats(N, T);    
  55.             StdOut.println(”mean = ” + percolationStats.mean());    
  56.             StdOut.println(”stddev = ” + percolationStats.stddev());    
  57.             StdOut.println(”95% confidence interval ” + percolationStats.confidenceLo() + “, ” + percolationStats.confidenceHi());    
  58.    } // test client, described below  
  59. }  
public class PercolationStats {    private double staT[];      private double sta_mean;      private double sta_stddev;      private int N;   public PercolationStats(int N, int T){      staT=new double[T];      this.N=N;      int times=0;      if(N<=0) throw new IllegalArgumentException();      if(T<=0) throw new IllegalArgumentException();      while(times<T){          Percolation pe=new Percolation(N);          int count=0;          while(true){              count++;              while(true){                  int x = StdRandom.uniform(N) + 1;                    int y = StdRandom.uniform(N) + 1;                  if(pe.isOpen(x, y)){                      continue;                  }else{                      pe.open(x, y);                      break;                  }              }              if(pe.percolates()){                  staT[times]=(double)count/((double)N*(double)N);                  break;              }             }          times++;     }      this.sta_mean = StdStats.mean(staT);        this.sta_stddev = StdStats.stddev(staT);   }// perform T independent computational experiments on an N-by-N grid   public double mean(){       return this.sta_mean;   }// sample mean of percolation threshold   public double stddev(){       return this.sta_stddev;   } // sample standard deviation of percolation threshold   public double confidenceLo(){       return this.sta_mean-1.96*this.sta_stddev/Math.sqrt(N);   }// returns lower bound of the 95% confidence interval   public double confidenceHi(){       return this.sta_mean+1.96*this.sta_stddev/Math.sqrt(N);   }             // returns upper bound of the 95% confidence interval   public static void main(String[] args){            int N = StdIn.readInt();              int T = StdIn.readInt();              PercolationStats percolationStats = new PercolationStats(N, T);              StdOut.println("mean = " + percolationStats.mean());              StdOut.println("stddev = " + percolationStats.stddev());              StdOut.println("95% confidence interval " + percolationStats.confidenceLo() + ", " + percolationStats.confidenceHi());     } // test client, described below}


原创粉丝点击