-
Notifications
You must be signed in to change notification settings - Fork 2
/
PercolationStats.java
112 lines (101 loc) · 3.11 KB
/
PercolationStats.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
/*----------------------------------------------------------------
* Author: Mike Milonakis
* Written: 12/10/2016
* Last updated: 12/10/2016
*
* Compilation: javac PercolationStats.java
* Execution: java Perolation.class < input.txt
*
* (Needs to have algs4.jar in classpath)
*
* This is the first programming assignment of
* princeton - algorithms course in coursera
*
*----------------------------------------------------------------*/
/**
* The PercolationStats class estimates the percolation Threshold by
* executing (T) experiments on N-by-N grid
*
* @author Mike Milonakis
*/
public class PercolationStats {
private double[] x;
private double mean;
private double stddev;
private double confidenceLo;
private double confidenceHi;
/**
* Default Constructor
* @param N The size of the N-by-N grid
* @param T The number of the experiments
*/
public PercolationStats(int N, int T) {
if (N <= 0 || T <= 0)
throw new IllegalArgumentException();
x = new double[T];
runExperiments(N, T);
}
private void runExperiments(int n, int t) {
for (int i = 0; i < t; i++) {
Percolation grid = new Percolation(n);
int openSites = 0;
while (!grid.percolates()) {
int y = StdRandom.uniform(1, n+1);
int z = StdRandom.uniform(1, n+1);
if (grid.isOpen(z, y))
continue;
grid.open(z, y);
openSites++;
}
x[i] = (double) openSites / (n*n);
mean = StdStats.mean(x);
stddev = StdStats.stddev(x);
confidenceLo = mean - (1.96 * stddev / (Math.sqrt(x.length)));
confidenceHi = mean + (1.96 * stddev / (Math.sqrt(x.length)));
}
}
/**
* Computes the sample mean of percolation threshold
* @return mean the mean
*/
public double mean() {
return mean;
}
/**
* Computes the standard deviation of percolation threshhold
*
* @return stdev the sample standard deviation
*/
public double stddev() {
return stddev;
}
/**
* Computes the low endpoint of 95% confidence interval
*
* @return the low endpoint
*/
public double confidenceLo() {
return confidenceLo;
}
/**
* Computes the high endpoint of 95% confidence interval
*
* @return the high endpoint
*/
public double confidenceHi() {
return confidenceHi;
}
public static void main(String[] args) {
int N = StdIn.readInt();
int T = StdIn.readInt();
PercolationStats stats = new PercolationStats(N, T);
StdOut.println("mean\t\t\t= " + stats.mean());
StdOut.println("stddev\t\t\t= " + stats.stddev());
StdOut.println("95% confidence interval\t= " + stats.confidenceLo()
+ ", " + stats.confidenceHi());
}
}