[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. ...
随机推荐
- Tomcat打包时多项目共享jar和精确指定jar版本
在产品打包发布时一个tomcat中如果存在多个war,部署的一般方式是部署到%TOMCAT_HOME%/webapps目录下,目录结构遵循J2EE规范,把引用的jar放到%TOMCAT_HOME%/w ...
- 关于DCMTK3.6.1源代码编译的总结
1.字符集不匹配 解决方法:更改Unicode字符集为多字节字符集 2.oflog.lib(winsock.obj) : error LNK2019: 无法解析的外部符号 错误. 解决方法:更改附加依 ...
- 【Web】关于URL中文乱码问题
关于URL编码 一.问题的由来 URL就是网址,只要上网,就一定会用到. ...
- 《ASP.NET1200例》ListView控件之修改,删除与添加
aspx <body> <form id="form1" runat="server"> <div> <asp:Lis ...
- Python OpenSource Project
http://www.oschina.net/project/lang/25/python
- Java for LeetCode 043 Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 重温CSS之背景、文本样式
CSS背景样式: 背景色:background-color属性,设置元素的背景色,如:div {background:blue;}--设置所有div元素的背景为蓝色: 背景图像:background- ...
- Extjs读取更改或者发送ajax返回请求的结果简单封装
Extjs的submit()方法提交的数据:如下: this.formPanel.getForm().submit({ url:this.saveUrl, ...
- “破锣摇滚”乐队(codevs 1444)
题目描述 Description 你刚刚继承了流行的“破锣摇滚”乐队录制的尚未发表的N(1 <= N <= 20)首歌的版权.你打算从中精选一些歌曲,发行M(1 <= M <= ...
- register
register:这个关键字请求编译器尽可能的将变量存在CPU内部寄存器中,而不是通过内存寻址访问,以提高效率.注意是尽可能,不是绝对.你想想,一个CPU 的寄存器也就那么几个或几十个,你要是定义了很 ...