[Algorithms(Princeton)] Week1 - PercolationStats

 1 public class PercolationStats {
 2
 3     private int N;
 4     private int T;
 5     private double[] results;
 6
 7     public PercolationStats(int N, int T) {
 8         if (N <= 0 || T <= 0) {
 9             throw new java.lang.IllegalArgumentException(
10                     "N or T must be greater than 0");
11         }
12
13         this.N = N;
14         this.T = T;
15         results = new double[T];
16
17         for (int t = 0; t < T; t++) {
18             results[t] = run();
19         }
20     }
21
22     private double run() {
23         Percolation percolation = new Percolation(N);
24         double count = 0;
25
26         while (!percolation.percolates()) {
27             count++;
28
29             // pick a random site
30             // (N+1 because second value to uniform is exclusive)
31             int i = StdRandom.uniform(1, N + 1);
32             int j = StdRandom.uniform(1, N + 1);
33
34             // generate new random sites until a blocked one is found
35             while (percolation.isOpen(i, j)) {
36
37                 i = StdRandom.uniform(1, N + 1);
38                 j = StdRandom.uniform(1, N + 1);
39
40             }
41
42             // open that site
43             percolation.open(i, j);
44
45         }
46         return count / (N * N); // percolation threshold estimate
47     }
48
49     public double mean() {
50         return StdStats.mean(results);
51     }
52
53     public double stddev() {
54         return StdStats.stddev(results);
55     }
56
57     public double confidenceHi() {
58         return mean() - 1.96 * stddev() / Math.sqrt(T);
59     }
60
61     public double confidenceLo() {
62         return mean() + 1.96 * stddev() / Math.sqrt(T);
63     }
64
65     public static void main(String[] args) {
66
67         int N;
68         int T;
69
70         if (args.length == 0) {
71             N = 100;
72             T = 10;
73         } else {
74             N = Integer.parseInt(args[0]);
75             T = Integer.parseInt(args[1]);
76         }
77
78         // double startTime = System.nanoTime();
79         PercolationStats stats = new PercolationStats(N, T);
80
81         double confidenceLow = stats.confidenceHi();
82         double confidenceHigh = stats.confidenceLo();
83
84         System.out.println("mean                    = " + stats.mean());
85         System.out.println("stddev                  = " + stats.stddev());
86         System.out.println("95% confidence interval = " + confidenceLow + ", "
87                 + confidenceHigh);
88
89         // performance measuring
90         // double endTime = System.nanoTime();
91         // System.out.println("time cost: " + (endTime - startTime));
92
93     }
94 }

[Algorithms(Princeton)] Week1 - PercolationStats

时间: 2024-10-10 17:25:10

[Algorithms(Princeton)] Week1 - PercolationStats的相关文章

[Algorithms(Princeton)] Week1 - Percolation

1 public class Percolation { 2 private boolean[] openSites; 3 private int gridN; 4 private WeightedQuickUnionUF UF; 5 private WeightedQuickUnionUF UFfull; 6 7 public Percolation(int N) { 8 if (N <= 0) { 9 throw new java.lang.IllegalArgumentException(

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 the connected component containing i. The operations, union(), connected(), and find() should all take logarithmic time or better. 1 import edu.princ

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: Remove x from S Find the successor of x: the smallest y in S such thaty>=x design a data type so that all operations(except construction) take logarithmi

Coursera Algorithms week1 Interview Questions: 3Sum in quadratic time

题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case. You may assume that you can sort the n integers in time proportional to n2 or better. 分析: <算法4>这本书提供的TwoSumFast解法为NlogN,ThreeSumFast解法为N2logN,根据课后练

Princeton Algorithms week3 Assignment

这周编程作业是实现检测点共线的算法.和排序算法有关系的地方在于,对斜率排序后可以很快的检测出来哪些点是共线的,另外这个算法的瓶颈也在于排序的性能. 一点收获: java传参数时传递的是值,这很多人都知道,只不过特别要注意的是对于引用类型,传递的是"引用的值",也就是说传递的是指针的副本,因此虽然似乎把一个数组传递进去了,但只要其他地方改动了这个数组,函数内部也会被影响.同理,返回值也是指针,如果不拷贝一份再返回,很有可能你在修改返回值的时候改动到了类的内部变量. 自己还不太明白的地方:

Coursera Algorithms week1 Interview Questions: Search in a bitonic array

题目要求: An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of n distinct integer values, determines whether a given integer

《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ

命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动元素,而路径的长度不超过lgN.对于路径上的每个节点,删除最大元素需要两次比比较(除了堆底元素),一次用来找出较大的子节点,一次用来确定该子节点是否需要上浮. 对于需要大量混杂的插入和删除最大元素操作的典型应用来说,命题Q意味着一个重要的性能突破(详见优先队列增长数量级表).使用有序或是无序数组的优

Algorithms(Sedgewick)配套Java源码配置教程

最近正在学习Coursera的算法课,打算跟着Sedgewick大神重新过一遍算法这一核心课程.这门课程的配套教材是Algorithms 4th Edition,官方网站为http://algs4.cs.princeton.edu/,这篇文章主要介绍本书源码的配置过程. 1,jar包的导入 作为小白,之前我使用Eclipse比较少,所以在配置初期遇到了不少问题,首先便是如何导入他的jar包(我居然被这个艹了一小时..),一般来说,jar包的导入直接通过build path进行就行了,可是我在导入

AlgorithmsI Programming Assignment 1: PercolationStats.java

import edu.princeton.cs.algs4.StdOut; import edu.princeton.cs.algs4.StdRandom; import edu.princeton.cs.algs4.StdStats; /* *How do I generate a site uniformly at random among all blocked sites for use in PercolationStats? * Pick a site at random (by u