Write a program to estimate the value of the percolation threshold via Monte Carlo simulation.
Assignment Description: http://coursera.cs.princeton.edu/algs4/assignments/percolation.html
Here are two tricks in this implementation.
- Using
virtualTopSite
andvirtualBottomSite
, thus the model percolates iff virtual top site is connected to virtual bottom site.
-
Using two
WeightedQuickUnionUF
objects. One isufWithTopAndBottom
, which contains bothvirtualTopSite
andvirtualBottomSite
. It is used to checkpercolates()
. Another isufWithTop
which only contiansvirtualTopSite
. This object is used to checkisFull()
. The reason of this design is to avoid backwash demonstrating as follows:
This program passed all test cases (excepting ungraded Checkstyle) in Coursera. And you can use provided PercolationVisualizer.java
and InteractivePercolationVisualizer.java
to test it.
ASSESSMENT SUMMARY
Compilation: PASSED
API: PASSED
Spotbugs: PASSED
PMD: PASSED
Checkstyle: FAILED (0 errors, 2 warnings)
Correctness: 33/33 tests passed
Memory: 8/8 tests passed
Timing: 20/20 tests passed
Aggregate score: 100.00%
[Compilation: 5%, API: 5%, Spotbugs: 0%, PMD: 0%, Checkstyle: 0%, Correctness: 60%, Memory: 10%, Timing: 20%]
This program is released under the MIT License.