princeton algorithms part 1 第一周作业题
2018-01-03 02:01
288 查看
import edu.princeton.cs.algs4.WeightedQuickUnionUF; public class Percolation { private WeightedQuickUnionUF uf; private WeightedQuickUnionUF uf_backwash; private int n; private boolean[] ifopen; private int count = 0; public Percolation(int n) // create n-by-n grid, with all sites blocked { if (n<=0) throw new IllegalArgumentException("n is<=0"); uf= new WeightedQuickUnionUF((n+1)*n+n+1); uf_backwash = new WeightedQuickUnionUF(n*n+n+1); this.n = n; ifopen = new boolean[(n+ 1) * (n) + n + 1]; for(int i=0*n+1;i<=0*n+n;i++) { uf.union(0 * n + 1, i); uf_backwash.union(0 * n + 1, i); ifopen[i]=true; uf.union((n+1)*n+1, (n+1)*n+i); ifopen[(n+1)*n+i] = true; } } public void open(int row, int col) // open site (row, col) if it is not open already { if (row<1||row>n) throw new IllegalArgumentException("row index out of bounds"); if (col<1||col>n) throw new IllegalArgumentException("column index out of bounds"); if (! this.isOpen(row, col) ) { count++; ifopen[row*n+col] = true; if (ifopen[n*(row-1)+col]) { uf.union(n * (row 4000 - 1) + col, n * row + col); uf_backwash.union(n * (row - 1) + col, n * row + col); } if (ifopen[n*(row+1)+col]) { uf.union(n * (row + 1) + col, n * row + col); if(row != n) uf_backwash.union(n * (row +1) + col, n * row + col); } if(col != 1 && ifopen[n*row+col-1] ) { uf.union(n*row+col-1,n*row+col); uf_backwash.union(n*row+col-1,n*row+col); } if(col != n && ifopen[n*row+col+1] ) { uf.union(n*row+col+1,n*row+col); uf_backwash.union(n*row+col+1,n*row+col); } } } public boolean isOpen(int row, int col) // is site (row, col) open? { if (row<1||row>n) throw new IllegalArgumentException("row index out of bounds"); if(col<1||col>n) throw new IllegalArgumentException("column index out of bounds"); return ifopen[row*n+col]; } public boolean isFull(int row, int col) // is site (row, col) full? { if (row<1||row>n) throw new IllegalArgumentException("row index out of bounds"); if(col<1||col>n) throw new IllegalArgumentException("column index out of bounds"); return uf_backwash.connected(n*row+col,0*n+1); } public int numberOfOpenSites() // number of open sites { return count; } public boolean percolates() // does the system percolate? { return uf.connected(0*n+1,(n+1)*n+1); } public static void main(String[] args){ Percolation percolation = new Percolation(4); System.out.println(percolation.percolates()); percolation.open(1, 1); percolation.open(2, 1); System.out.println(percolation.percolates()); percolation.open(3, 1); percolation.open(3, 2); System.out.println(percolation.isFull(3, 2)); percolation.open(4, 2); System.out.println(percolation.percolates()); } }
import edu.princeton.cs.algs4.StdRandom; import edu.princeton.cs.algs4.StdStats; public class PercolationStats { private double staT[]; private double sta_mean; private double sta_stddev; private int trials ; public PercolationStats(int n, int trials) // perform trials independent experiments on an n-by-n grid { if(n<=0) throw new IllegalArgumentException(); if(trials<=0) throw new IllegalArgumentException(); staT=new double[trials]; this.trials=trials; int times=0; while(times<trials) { Percolation pe = new Percolation(n); while (!pe.percolates()) { int x = StdRandom.uniform(n) + 1; int y = StdRandom.uniform(n) + 1; pe.open(x,y); } staT[times]=(double)pe.numberOfOpenSites()/((double)n*(double)n); times++; } this.sta_mean = StdStats.mean(staT); this.sta_stddev = StdStats.stddev(staT); } public double mean() // sample mean of percolation threshold { return this.sta_mean; } public double stddev() // sample standard deviation of percolation threshold { return this.sta_stddev; } public double confidenceLo() // low endpoint of 95% confidence interval { return this.sta_mean-1.96*this.sta_stddev/Math.sqrt(trials); } public double confidenceHi() // high endpoint of 95% confidence interval { return this.sta_mean+1.96*this.sta_stddev/Math.sqrt(trials); } public static void main(String[] args) // test client (described below) { PercolationStats percolationStats = new PercolationStats(20, 100); System.out.println("mean="+ percolationStats.mean()); System.out.println("stddev="+ percolationStats.stddev()); System.out.println("95%% confidence Interval="+percolationStats.confidenceLo()+" "+ percolationStats.confidenceHi()); } }
相关文章推荐
- princeton algorithms part 1 第二周作业题
- 【Java、算法】Princeton Algorithms Part I Week 1 Exercise
- princeton algorithms part 1 第三周作业题
- princeton algorithms part 1 第四周作业题
- Week 1 Assignment - Wordnet - Princeton - Algorithms Part II
- Week3 Assignment - Princeton-Algorithms-PartI
- Stanford: Algorithms: Design and Analysis, Part 1 [Week 2]
- Coursera_Algorithms,Part I,第一周笔记
- 解决“This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms”
- Algorithms: Design and Analysis, Part 1Problem Set #5
- Stanford - Algorithms: Design and Analysis, Part 2 - Week 1 Assignment: Greedy and Prim
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 6 Assignment: hash table and heap
- Stanford - Algorithms: Design and Analysis, Part 2 - Week 2 Assignment: Clustering
- 异常:System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms 这个实现是不是Windows平台FIPS验证的加密算法。解决方法
- Some thought about "The Parallel ADT and Algorithms"(part one)
- Internal Sorting Algorithms Part 1/2: Elementary Sorts
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 2 Assignment: QuickSort
- Stanford - Algorithms: Design and Analysis, Part 1 - Week 4 Assignment: strongly connected component
- Week4 Assignment - Without Full Score - Princeton-Algorithms-PartI
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms