Duke CS Logo CompSci 100e: Program Design & Analysis II
(Fall 2009)
Home   Assignments   Percolation   How To  

Percolation

Taken from Percolation Assignment by Robert Sedgewick & Kevin Wayne.
You should snarf assignment percolation or browse the code directory for code files provided. See the Percolation How To file for help and more instructions.

Problem Statement

Write a program to estimate the value of the percolation threshold via Monte Carlo simulation.

Percolation. Given a composite systems 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.

The model. We model a percolation system using an N-by-N 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.)

Percolates

The problem. In a famous scientific problem, researchers are interested in the following question: if sites are independently set to be open with probability p (and therefore blocked with probability 1 − p), what is the probability that the system percolates? When p is 0, the system does not percolate; when p is 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).


Percolation threshold for 20-by-20 grid                Percolation threshold for 100-by-100 grid          

When N is sufficiently large, there is a threshold value p* such that when p < p* a random N-by-N grid almost never percolates, and when p > p*, a random N-by-N grid almost always percolates. No mathematical solution for determining the percolation threshold p* has yet been derived. Your task is to write a programs to:

  1. visualize the percolation process
  2. estimate p*
  3. compare brute force (depth-first search) to union-find for finding connected open sites

Visualizing the Percolation Process

Your completed PercolationVisualizer should prompt the user for N and display the percolation process starting with a N-by-N grid of sites (initially all blocked and black). After each site is opened, display full sites in cyan, open sites (that aren't full) in white, and blocked sites in black using princeton.StdDraw. Here is an example of steps in a visualization on a 20x20 grid.
      Percolation 25% done
50 open sites
Percolation 50% done
100 open sites
Percolation 75% done
150 open sites
Percolation 100% done
204 open sites

Percolation data type

To model a percolation system, you will create different implementations of the IPercolate interface.
public interface IPercolate { public abstract void draw(); // Draw the current state of the grid public abstract void step(); // Find a blocked site and mark it as open public abstract boolean percolates(); // Returns true iff open path from to bottom }
You will complete brute-force (PercolationDFS) and union-find (PercolationUF) versions of the the IPercolate data type.

Estimating p*

To estimate the percolation threshold, perform the following computational experiment:

  • Initialize all sites to be blocked.

  • Repeat the following until the system percolates:

    • Choose a blocked site (row i, column j) uniformly at random among all blocked sites.

    • Open the site (row i, column j).

  • The fraction of sites that are opened until the system percolates provides an estimate of the percolation threshold.
In the 20-by-20 example above, our estimate of the percolation threshold is 204/400 = 0.51 because the system percolates when the 204th site is opened.

To obtain an accurate estimate of the percolation threshold, repeat the experiment T times and average the results. Let xt be the fraction of open sites in experiment t. The mean μ provides an estimate of the percolation threshold. The standard deviation σ measures the sharpness of the threshold.

Estimating the mean and variance
Assuming T is sufficiently large (say, at least 30), the following provides a 95% confidence interval for the percolation threshold:

95% confidence interval for percolation threshold
Write a client program PercolationStats that takes prompts the user for N and T, performs T independent experiments on an N-by-N grid, and prints out the 95% confidence interval for the percolation threshold. Use java.util.Random to generate random numbers and follow steps above to to compute the mean and standard deviation. Below is example run with N=200 and T=100.
mean percolation threshold  = 0.5920965000000004
stddev                      = 0.009811413646870666
95% confidence interval     = [0.5901734629252137, 0.594019537074787]

total time                  = 2.074
mean time per experiment    = 0.02073999999999999
stddev                      = 0.0037646248153036512

Comparing IPercolate Implementations

Implement PercolationUF using the quick find data structure (QuickFind.java) and by creating a version using the weighted quick union with path compression data structure. In your README, you will answer the following questions.
  • How does doubling N affect the running time?
  • How does doubling T affect the running time?
  • Measure running time (using calls to System.currentTimeMillis) of the 3 versions of your program (DFS, Quick Find, and weighted quick union with path compression).
  • Give a formula (using Big-Oh notation) of the running time on your computer (in seconds) as a function of N and T.
  • Give a formula (using Big-Oh notation) that describes the amount of memory (in bytes) that your program consumes as a function of N.


Extra Credit

  1. Write a program UnionFindVisualizer.java to visualize the average path length from each node to the root using the quick union and weighted quick union with path compression data structures. Organize your program so that it takes N from the user, performs one experiment on an N-by-N grid, and plots the average path length for each of the two data structures. Explain your results.

  2. Sedgewick & Wayne: Problem 2.4.17

  3. Sedgewick & Wayne: Problem 2.4.18

  4. Sedgewick & Wayne: Problem 2.4.19

Grading

This assignment is worth 32 points.

Percolation Grading
description points
Implementation of PercolationStats and analysis 12 points (4 for implementation, 8 for README/compare)
Implement PercolationVisualizer 6 points
Implement PercolationDFS 6 points (please include README information on efficiency compared
Implement PercolationUF (QuickFind and weighted quick union with path compression) 8 points (please include README information on efficiency)

Last updated Fri Oct 30 17:13:15 EST 2009