Repository navigation
Expand file tree
/
Copy pathPercolationStats.java
More file actions
139 lines (121 loc) · 2.81 KB
/
Copy pathPercolationStats.java
File metadata and controls
139 lines (121 loc) · 2.81 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/************************************************
* Author(s): Gavin Rosenvall, Spencer Rosenvall
* Class: CSIS 2420
* Professor: Frau Posch
* Assignment: A01_Percolation
************************************************/
package a01;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
/**
* Class PercolationStats generates statistics from class Percolation.java.
*
* @author GavinR, SpencerR
*
*/
public class PercolationStats
{
double[] averages;
/**
* Performs T independent experiments on an N x N grid.
*
* @param N
* @param T
*/
public PercolationStats(int N, int T)
{
if (N <= 0 || T <= 0)
{
throw new IllegalArgumentException();
} else
{
this.averages = new double[T];
startPercolation(N, T);
}
}
/**
* Starts the percolation from class Percolation.
*
* @param n
* @param t
* @return
*/
private double[] startPercolation(int N, int T)
{
int index = 0;
for (int i = 0; i < T; i++)
{
Percolation percolation = new Percolation(N);
while (!percolation.percolates())
{
int row = StdRandom.uniform(0, N);
int column = StdRandom.uniform(0, N);
if (!percolation.isOpen(row, column))
{
percolation.open(row, column);
index++;
}
}
averages[i] = ((double) index) / (N * N);
index = 0;
}
return averages;
}
/**
* Samples the mean of the percolation threshold.
*
* @return
*/
public double percolationMean()
{
return StdStats.mean(averages);
}
/**
* Samples the standard deviation of the percolation threshold.
*
* @return
*/
public double standarDeviation()
{
return StdStats.stddev(averages);
}
/**
* Formula for the half value needed for the 95% low and high confidence
* levels.
*
* @return
*/
private double calculateHalf()
{
return 1.96 * standarDeviation() / Math.sqrt(averages.length);
}
/**
* Provides the low end point of 95% confidence interval.
*
* @return
*/
public double confidenceLow()
{
return percolationMean() - calculateHalf();
}
/**
* Provides the high end point of 95% confidence interval.
*
* @return
*/
public double confidenceHigh()
{
return percolationMean() + calculateHalf();
}
public static void main(String[] args)
{
PercolationStats ps = new PercolationStats(200, 100);
StdOut.println("Test Results: ");
StdOut.println();
StdOut.printf("%-30s %.10f\n", "Mean:", ps.percolationMean());
StdOut.printf("%-30s %.10f\n", "Standard Deviation:", ps.standarDeviation());
StdOut.printf("%-30s %.10f\n", "Confidence Low:", ps.confidenceLow());
StdOut.printf("%-30s %.10f\n", "Confidence High:", ps.confidenceHigh());
}
}