[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. ...
随机推荐
- Linux 浅谈Linux 操作系统的安全设置
如今linux系统安全变的越来越重要了,这里我想把我平时比较常使用的一些linux下的基本的安全措施写出来和大家探讨一下,让我们的linux系统变得可靠. 1.BIOS的安全设置 这是最基本的了,也是 ...
- win7系统扩展双屏幕时,如何在两个屏幕下都显示任务栏
扩展屏幕下都显示任务栏!!! win7系统本身无法设置该功能(目前我是不知道) 但可以下载第三方软件来解决该问题. 第一步:Dual Monitor Taskbar 下载软件 下载链接:http:// ...
- vs2013秘钥
Ultimate:BWG7X-J98B3-W34RT-33B3R-JVYW9 Premium:FBJVC-3CMTX-D8DVP-RTQCT-92494 YKCW6-BPFPF-BT8C9-7DCT ...
- 4.在二元树中找出和为某一值的所有路径[FindPathsInBinaryTree]
[题目]: 输入一个整数和一棵二元树.从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径.打印出和与输入整数相等的所有路径. 例如输入整数22和如下二元树 10 ...
- Android 和iOS中 Gesture 和 Touch
先谈谈在开发中遇到的手势问题: 今天在开发android程序时,在 View.OnTouchListener 的 onTouch(View view, MotionEvent motionEvent) ...
- Java for LeetCode 191 Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
- google maps js v3 api教程(1) -- 创建一个地图
原文地址 google maps javascript官方文档:https://developers.google.com/maps/documentation/javascript/ 在创建地图之前 ...
- 水果姐逛水果街Ⅰ(codevs 3304)
题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...
- java单例,懒汉&饿汉
* 单例模式Singleton * 应用场合:有些对象只需要一个就足够了,如皇帝 * 作用: 保证整个应用程序中某个实例有且只有一个 * 区别: 饿汉模式的特点是加载类时比较慢,但运行是比较快 ...