This assignment is due before 11:00PM on Wednesday, September 13, 2017. There are two parts to this homework assignment:
You’ll need to submit your solutions to both parts of the homework before the deadline.
Write a program to estimate the value of the percolation threshold via Monte Carlo simulation.
The goals of this part of the assignment are:
You should install Java and the Eclipse IDE on your computer for your operating system. You should also download algs4.jar, which contains Java classes for I/O and all of the algorithms in the textbook, and import the jar file in your Eclipse project.
To access a class in algs4.jar from a Java class that you write, you will need to include import statements, such as the ones below:
Note that your code must be in the default package; if you use a package statement, the autograder will not be able to assess your work.
Given a composite system comprised of randomly distributed insulating and metallic materials: what fraction of the materials need to be metallic so that the composite system is an electrical conductor? Given a porous landscape with water on the surface (or oil below), under what conditions will the water be able to drain through to the bottom (or the oil to gush through to the surface)? Scientists have defined an abstract process known as percolation to model such situations.
We model a percolation system using an -by- grid of sites. Each site is either open or blocked. A full site is an open site that can be connected to an open site in the top row via a chain of neighboring (left, right, up, down) open sites. We say the system percolates if there is a full site in the bottom row. In other words, a system percolates if we fill all open sites connected to the top row and that process fills some open site on the bottom row. (For the insulating/metallic materials example, the open sites correspond to metallic materials, so that a system that percolates has a metallic path from top to bottom, with full sites conducting. For the porous substance example, the open sites correspond to empty space through which water might flow, so that a system that percolates lets water fill open sites, flowing from top to bottom.)
In a famous scientific problem, researchers are interested in the following question: if sites are independently set to be open with probability (and therefore blocked with probability ), what is the probability that the system percolates? When equals 0, the system does not percolate; when equals 1, the system percolates. The plots below show the site vacancy probability p versus the percolation probability for 20-by-20 random grid (left) and 100-by-100 random grid (right).
When is sufficiently large, there is a threshold value such that when a random -by- grid almost never percolates, and when , a random -by- grid almost always percolates. No mathematical solution for determining the percolation threshold has yet been derived. Your task is to write a computer program to estimate .
To model a percolation system, create a data type
Percolation with the following API:
By convention, the row and column indices are integers between 1 and , where (1, 1) is the upper-left site: Throw a
java.lang.IllegalArgumentException if any argument to
isFull() is outside its prescribed range. The constructor should throw a
java.lang.IllegalArgumentException if ≤ 0.
The constructor should take time proportional to ; all methods should take constant time plus a constant number of calls to the union–find methods
To estimate the percolation threshold, consider the following computational experiment:
For example, if sites are opened in a 20-by-20 lattice according to the snapshots below, then our estimate of the percolation threshold is 204/400 = 0.51 because the system percolates when the 204th site is opened.
By repeating this computation experiment times and averaging the results, we obtain a more accurate estimate of the percolation threshold. Let be the fraction of open sites in computational experiment . The sample mean provides an estimate of the percolation threshold; the sample standard deviation ; measures the sharpness of the threshold.
Assuming is sufficiently large (say, at least 30), the following provides a 95% confidence interval for the percolation threshold:
To perform a series of computational experiments, create a data type PercolationStats with the following API.
The constructor should throw a
java.lang.IllegalArgumentException if either ≤ 0 or trials ≤ 0.
Also, include a
main() method that takes two command-line arguments and , performs independent computational experiments (discussed above) on an -by- grid, and prints the sample mean, sample standard deviation, and the 95% confidence interval for the percolation threshold. Use
StdRandom to generate random numbers; use
StdStats to compute the sample mean and sample standard deviation.
Percolation data type using the quick find algorithm in
Stopwatch to measure the total running time of
PercolationStats for various values of and . How does doubling change the total running time? How does doubling change the total running time? Give a formula (using tilde notation) of the total running time on your computer (in seconds) as a single function of both and .
Using the 64-bit memory-cost model from lecture, give the total memory usage in bytes (using tilde notation) that a
Percolation object uses to model an n-by-n percolation system. Count all memory that is used, including memory for the union–find data structure.
Now, implement the Percolation data type using the weighted quick union algorithm in
WeightedQuickUnionUF. Answer the questions in the previous paragraph.
Percolation.java should use the weighted quick-union algorithm from the
WeightedQuickUnionUF class in the
algs4.jar. Your submission may not call library functions except those in
WeightedQuickUnionUF, and packages and methods under
The goals of this assignment are to test your understanding of the material covered in sections 1.4 and 1.5 of the textbook, and the lecture and recitation materials. You should read the textbook chapters before doing this part of the assignment.
Written homeworks must be typeset in LaTeX and submitted in PDF format.
Develop a UF implementation that uses the same basic strategy as weighted quick-union but keeps track of the tree height and always links the shorter tree to the taller one. Prove a logarithmic upper bound on the height of the trees with N nodes with your algorithm.
For the following diagram:
For each of the statements below, please say whether it is true or false, and give a 1 sentence explanation of your answer.
Facebook has hired you as a special advisor to Mark Zuckerberg. Congrats - taking CIS 121 really paid off. Mark considers acquiring a startup in stealth mode. The company claims to have invented a new algorithm that will use polynomial time to solve a problem previously thought to be solvable in exponential time. The company refuses to release its code because it is worried that it will be stolen. They will allow you to do black box testing by sending whatever inputs you want to their server. You get back the output, and you can time how long it takes to run. Mark asks you to test it on the double, so you sketch out this chart.
|Input size||Response time|