[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. ...
随机推荐
- Powershell常用命令
Powershell常用命令1.Get-Command 得到Powshell所有命令2.Get-Process 获取所有进程3.Set-Alias 给指定命令重命名 如:Set-Alias aaa G ...
- Linux 怎么查看服务的启动进程所占用的目录
lsof简介 lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以 ...
- django-cms 代码研究(二)bugs?
djangocms集成到现有项目中后,发现了几个问题: 1. 现有项目的url匹配失效,下面requests请求被交给djangocms处理了 url(r'^admin/', include(admi ...
- [转]sql语句中出现笛卡尔乘积 SQL查询入门篇
本篇文章中,主要说明SQL中的各种连接以及使用范围,以及更进一步的解释关系代数法和关系演算法对在同一条查询的不同思路. 多表连接简介 在关系数据库中,一个查询往往会涉及多个表,因为很少有数据库只有一个 ...
- sublime text多文件夹查找关键字
Ctrl+shift+F 快捷键在文件夹内查找,与普通编辑器不同的地方是sublime允许添加多个文件夹进行查找 转自:http://www.douban.com/note/362268947/
- 【leetcode】Binary Search Tree Iterator
Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator wil ...
- 【转】java反射详解
转自:http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html 本篇文章依旧采用小例子来说明,因为我始终觉的,案例驱动是最好的 ...
- 利用 FFmpeg 和 ImageMagick, AVI 转 GIF(不失真)
利用[TMPGEnc 4.0 XPress] 或 [TMPGEnc Video Mastering Works 5] 生成 AVI 这个视频编辑软件,可对每个帧进行操作 1.生成每个帧的 PNG ff ...
- Java 全半角转换
* 全角转半角的 转换函数* @return String*/public static final String full2HalfChange(String QJstr){StringBuffer ...
- JQuery函数库
核心Core 函数$()动态创建由 jQuery 对象包装的 DOM 元素$.unique()去重排序函数$.inArray()在数组中搜索指定的值并返回其索引$.merge()合并数组 属性Para ...