[Algorithms(Princeton)] Week1 - PercolationStats
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的更多相关文章
- [Algorithms(Princeton)] Week1 - Percolation
public class Percolation { private boolean[] openSites; private int gridN; private WeightedQuickUnio ...
- 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 ...
- 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 ...
- Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity
题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...
- Princeton Algorithms week3 Assignment
这周编程作业是实现检测点共线的算法.和排序算法有关系的地方在于,对斜率排序后可以很快的检测出来哪些点是共线的,另外这个算法的瓶颈也在于排序的性能. 一点收获: java传参数时传递的是值,这很多人都知 ...
- Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题
题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...
- 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 ...
- Algorithms, Part I by Kevin Wayne, Robert Sedgewick
Welcome to Algorithms, Part I 前言 昨天在突然看到了Coursera上Robert Sedgewick讲的Algorithms,Part II看了一些,甚是爽快,所以又去 ...
- AlgorithmsI Programming Assignment 1: PercolationStats.java
import edu.princeton.cs.algs4.StdOut; import edu.princeton.cs.algs4.StdRandom; import edu.princeton. ...
随机推荐
- The Perfect Stall (incomplete)
恩,一看就知道是一道二分图最大匹配的题. 感动得发现自己不会做..果然我是太弱了.学校里真是麻烦死,根本没有时间好吗. (NOIP)会不会感动地滚粗啊? 然后稍微看看,恩,匈牙利算法. 真是感动得落泪 ...
- Java字符串split函数的注意事项
Java字符串的split方法可以分割字符串,但和其他语言不太一样,split方法的参数不是单个字符,而是正则表达式,如果输入了竖线(|)这样的字符作为分割字符串,会出现意想不到的结果, 如, Str ...
- JS的trim()方法
去除字符串左右两端的空格,在vbscript里面可以轻松地使用 trim.ltrim 或 rtrim,但在js中却没有这3个内置方法,需要手工编写.下面的实现方法是用到了正则表达式,效率不错,并把这三 ...
- linux 下查看某个端口是否被占用
lsof -i:端口号 转自: http://my.oschina.net/u/193184/blog/146885
- There is no getter for property named 'purchaseApplyId' in 'class java.lang.Long'
mapper.xml: <delete id="deleteByPurchaseAppyId" parameterType="Long"> < ...
- 38.输出1到最大的N位数[Print 1 to max number of N bits]
[题目] 输入数字n,按顺序输出从1最大的n位10进制数.比如输入3,则输出1.2.3一直到最大的3位数即999. [分析] 这是一道很有意思的题目.看起来很简单,其实里面却有不少的玄机. [常规思路 ...
- 2013 ACM/ICPC 长沙网络赛J题
题意:一个数列,给出这个数列中的某些位置的数,给出所有相邻的三个数字的和,数列头和尾处给出相邻两个数字的和.有若干次询问,每次问某一位置的数字的最大值. 分析:设数列为a1-an.首先通过相邻三个数字 ...
- iOS 中的第三方库管理工具
xcode没有android studio中的gradle进行第三方库管理,但是有第三方的库管理工具CocoaPods,https://github.com/CocoaPods/CocoaPods/w ...
- Myeclipse中全部文件设置成UTF-8
如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简 ...
- mysql 建表语句
修改id为自动增长: alter table book b_ISBN int(11) auto_increment; 自动增长要加的:auto_increment 基本的sql语句: 选择:sel ...