[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. ...
随机推荐
- First Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- [] ubuntu 14.04 搜狗拼音输入法安装
1.需要现在ubuntu下安装fcitx和libssh2-1命令如下 sudo apt-get install fcitx sudo apt-get install libssh2-1 注意最好是先卸 ...
- 水果姐逛水果街Ⅰ(codevs 3304)
题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...
- vector容器+iterator迭代器
关于vector容器的详细描述,可参考:http://www.jb51.net/article/41648.htm 关于iterator迭代器的描述,可参考http://www.cppblog.c ...
- Material Design入门(二)
本文主要包括以下内容 侧滑菜单DrawerLayout实现 CardView实现 DrawerLayout介绍 drawerLayout是Support Library包中实现了侧滑菜单效果的控件,可 ...
- PW试验-----verilog
PWM,脉冲宽度调制.顾名思义,是通过调制脉冲的宽度,即占空比,来实现的.可是使占空比逐渐由最小增加到最大,也可以使占空比由最大减少到最小来实现不同的现象.若用LED灯来显示现象,则可以称作:LED呼 ...
- Sonar+Hudson+Maven构建系列之二:迁移Sonar
摘要:由于昨天在一台机器上安装的东西太多了,导致Linux机器上非常卡,一台Linux负担了jira, fisheye, confluence, sonar, hudson, mysql 等等,本来已 ...
- keytool生成证书与Tomcat SSL配置
转自:http://tomhat.iteye.com/blog/2087673 一.Keytool介绍 Keytool是一个Java数据证书的管理工具.Keytool将密钥(key)和证书(certi ...
- SQLite使用方法 SQLiteOpenHelper操作(转)
SQLiteOpenHelper主要用于 创建数据库 SQLiteDatabase 主要用于 执行sql语句 程序内使用SQLite数据库是通过SQLiteOpenHelper进行操作 1. ...
- Kinect学习笔记(五)——更专业的深度图
这一节的内容就是把深度图转换为彩色图,然后不再使用硬解码,而是继续采用sdk里面封装好的功能来减少测量的误差,以及避免转换为灰度图时,出现绿巨人时候的掉针的bug. 下面直接 ...