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. MYSQL随机抽取查询 MySQL Order By Rand()效率问题

    MYSQL随机抽取查询:MySQL Order By Rand()效率问题一直是开发人员的常见问题,俺们不是DBA,没有那么牛B,所只能慢慢研究咯,最近由于项目问题,需要大概研究了一下MYSQL的随机 ...

  2. LInux 安全测试 2

    Centos/CentOS 6.4 linux内核2.6.3.2本地提权exp代码 jincon 发表于 2014-05-31 08:25:00 发表在: 代码审计 最近我接手的一台centos 服务 ...

  3. PHP中通过加号合并数组

    通常,我们合并多个数组用的是array_merge()函数,其实,PHP手册中关于数组操作符的介绍给了我们更简单的方法,那就是"+"号,看看下面的例子就明白了(详细了解) 代码: ...

  4. 经典的SQL面试题

    SQL中 inner join. left join .right join. outer join之间的区别 A表(a1,b1,c1) B表(a2,b2) a1 b1 c1 a2 b2 01 数学 ...

  5. How can I pretty-print JSON in python?

    python -m json.tool my_json.json 转自: http://stackoverflow.com/questions/352098/how-can-i-pretty-prin ...

  6. 【转】如何用 Chrome for Android 做远程遥控 debugging

    http://blog.csdn.net/wuchengzhi82/article/details/22190435

  7. Solr5.3.1整合IKAnalyzer

    由于solr5.3.1本身不支持中文分词,而msseg4j的分词效果不明显.因而采用IK进行分词,然而参考http://www.superwu.cn/2015/05/08/2134/在google上下 ...

  8. iOS CoreData 的级联删除等操作

    关于CoreData 的基本操作在网上有一些中文资料,但是这些资料大多没有涉及CoreData的详细操作,只是简单的演示了最基本用法.像级联删除这种最基本的数据库操作都没有提到.今天在网上看到了一些英 ...

  9. Servlet之Cookie操作

    Java对cookie的操作比较简单,主要介绍下建立cookie和读取cookie,以及如何设定cookie的生命周期和cookie的路径问题. 1,建立一个无生命周期的cookie,即随着浏览器的关 ...

  10. fork

    #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> ...