public class PercolationStats {

     private int N;
private int T;
private double[] results; public PercolationStats(int N, int T) {
if (N <= 0 || T <= 0) {
throw new java.lang.IllegalArgumentException(
"N or T must be greater than 0");
} this.N = N;
this.T = T;
results = new double[T]; for (int t = 0; t < T; t++) {
results[t] = run();
}
} private double run() {
Percolation percolation = new Percolation(N);
double count = 0; while (!percolation.percolates()) {
count++; // pick a random site
// (N+1 because second value to uniform is exclusive)
int i = StdRandom.uniform(1, N + 1);
int j = StdRandom.uniform(1, N + 1); // generate new random sites until a blocked one is found
while (percolation.isOpen(i, j)) { i = StdRandom.uniform(1, N + 1);
j = StdRandom.uniform(1, N + 1); } // open that site
percolation.open(i, j); }
return count / (N * N); // percolation threshold estimate
} public double mean() {
return StdStats.mean(results);
} public double stddev() {
return StdStats.stddev(results);
} public double confidenceHi() {
return mean() - 1.96 * stddev() / Math.sqrt(T);
} public double confidenceLo() {
return mean() + 1.96 * stddev() / Math.sqrt(T);
} public static void main(String[] args) { int N;
int T; if (args.length == 0) {
N = 100;
T = 10;
} else {
N = Integer.parseInt(args[0]);
T = Integer.parseInt(args[1]);
} // double startTime = System.nanoTime();
PercolationStats stats = new PercolationStats(N, T); double confidenceLow = stats.confidenceHi();
double confidenceHigh = stats.confidenceLo(); System.out.println("mean = " + stats.mean());
System.out.println("stddev = " + stats.stddev());
System.out.println("95% confidence interval = " + confidenceLow + ", "
+ confidenceHigh); // performance measuring
// double endTime = System.nanoTime();
// System.out.println("time cost: " + (endTime - startTime)); }
}

[Algorithms(Princeton)] Week1 - PercolationStats的更多相关文章

  1. [Algorithms(Princeton)] Week1 - Percolation

    public class Percolation { private boolean[] openSites; private int gridN; private WeightedQuickUnio ...

  2. Coursera Algorithms week1 查并集 练习测验:3 Successor with delete

    题目原文: Given a set of n integers S = {0,1,…,N-1}and a sequence of requests of the following form: Rem ...

  3. Coursera Algorithms week1 查并集 练习测验:2 Union-find with specific canonical element

    题目原文: Add a method find() to the union-find data type so that find(i) returns the largest element in ...

  4. Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity

    题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...

  5. Princeton Algorithms week3 Assignment

    这周编程作业是实现检测点共线的算法.和排序算法有关系的地方在于,对斜率排序后可以很快的检测出来哪些点是共线的,另外这个算法的瓶颈也在于排序的性能. 一点收获: java传参数时传递的是值,这很多人都知 ...

  6. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  7. Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time

    题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...

  8. Algorithms, Part I by Kevin Wayne, Robert Sedgewick

    Welcome to Algorithms, Part I 前言 昨天在突然看到了Coursera上Robert Sedgewick讲的Algorithms,Part II看了一些,甚是爽快,所以又去 ...

  9. AlgorithmsI Programming Assignment 1: PercolationStats.java

    import edu.princeton.cs.algs4.StdOut; import edu.princeton.cs.algs4.StdRandom; import edu.princeton. ...

随机推荐

  1. 使用twisted.web实现代理服务器

    简单的实现谷歌的代理: 架构就是下面这么简单. ================= my server outside GFW  |    <----------------------> ...

  2. SphinxSE的安装

    SphinxSE 的使用 SphinxSE 的使用 :wiki SphinxSE是一个可以编译进MySQL 5.x版本的MySQL存储引擎,尽管被称作“存储引擎”,SphinxSE自身其实并不存储任何 ...

  3. Group Shifted Strings

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  4. Cocos2d-x 学习资料推荐

    最近在看Cocos2d-x ,官网的资料太少了,下面推荐一些比较好的教程,不断更新中. 1. cocos2d-x高级开发教程 如果你懂得objective-c 那么一定要看看这本书,这里面有许多C++ ...

  5. -fomit-frame-pointer 编译选项在gcc 4.8.2版本中的汇编代码研究

    #include void fun(void) { printf("fun"); } int main(int argc, char *argv[]){ fun(); return ...

  6. BFS:Meteor Shower(POJ 3669)

         奔跑吧,傻牛 题目大意:这只Bessie的牛又要来闹事了,这次她的任务就是来躲流星雨,流星雨就是在一定时间会从天上砸到Bessie的所在的正方形区域内(Bessie在0,0的位置),然后砸下 ...

  7. CSS对字体单位的总结

    国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的 ...

  8. 网络流-最大流 模板(poj 1273)

    #include<cstdio> #include<iostream> #include<cstring> #include<queue> #defin ...

  9. OC内存管理(ARC)

    1.什么是ARC Automatic Reference Counting,自动引用计数,即ARC,可以说是WWDC2011和iOS5所引入 的最大的变革和最激动人心的变化.ARC是新的LLVM 3. ...

  10. 使用MySQL索引的几个问题

    1.索引不存储null值 更准确的说,单列索引不存储null值,复合索引不存储全为null的值.索引不能存储Null,所以对这列采用is null条件时,因为索引上根本 没Null值,不能利用到索引, ...