Percolation
Model percolation with disjoint sets: open sites in a grid, decide which are connected to the top, detect percolation with virtual sites, avoid backwash, estimate the threshold with concurrent Monte Carlo trials, and watch it in a SwiftUI app.
This is CS61B’s Project 3, in Swift. Scientists use a simple model, called percolation, for questions like these: if a material is a random mix of insulating and conducting parts, what fraction must conduct before electricity can get through? If rock has random pores, when can water drain from the surface to the bottom? You will build the model with Lesson 35’s disjoint sets, and then use it to estimate the answer.
The model
An N-by-N grid of sites starts with every site blocked. Sites are then opened one at a time. An open site is full if a chain of open sites connects it to the top row, moving up, down, left, or right (not diagonally). The system percolates when a site in the bottom row is full: imagine water poured on top, flowing through the open sites.
If each site is open independently with probability p, whether the system percolates depends on p. For large grids there is a sharp threshold: below about 0.593 it almost never percolates, and above it almost always does. No formula for that number is known. The only way to find it is to simulate, which is the last part of this project.
Getting started
Work in the percolation folder of the
swift-projects repository:
cd swift-projects/percolation
swift test
UnionFind.swift, provided, is weighted quick union from Lesson 35. Itsfinddoes not compress paths, so it never changes the structure, andisConnectedworks in code that only reads, like a SwiftUI view drawing the grid.Percolation.swiftis yours.PercolationStats.swift, provided, runs the Monte Carlo experiment.PercolationAppdraws the grid, lets you open sites by clicking, fills the grid at random, and runs the experiment. It needs macOS 27:swift run -c release PercolationApp.
Task 1: Opening sites
Choose your stored properties, write their invariants in a comment, and
implement openSites, open(row:column:), and isOpen(row:column:). Opening
an open site again changes nothing. Every method checks with a precondition
that the site is inside the grid.
Task 2: Fullness and percolation
Use a UnionFind with one item per site. When a site opens, connect it to each
of its open neighbors. Then a site is full if it is connected to any open
site in the top row, and the system percolates if any site in the bottom row
is full.
Checking every top-row site for each question would take time proportional to
N. The standard trick is two virtual sites: an extra item connected to every
open site in the top row, and another connected to every open site in the bottom
row. Now “is this site full?” is one isConnected with the virtual top, and
“does it percolate?” is one isConnected between the virtual top and the virtual
bottom. Every method uses a constant number of union-find operations, as the
requirements say.
Task 3: Backwash
Once the grid percolates, the virtual bottom is connected to the virtual top.
Every open site in the bottom row is connected to the virtual bottom, so every
one of them now looks full, including sites that no path of open sites reaches
from the top. That is backwash, and the bottomSitesAreNotFullThroughTheBottom
test checks for it.
Fix it without giving up constant-time operations. One way is a second
UnionFind that has a virtual top but no virtual bottom, used only for isFull.
It costs memory, but keeps every answer correct.
Task 4: Speed
Opening every site of a 300 × 300 grid should take a moment, not minutes. If
your open loops over a row or the grid, that is the problem. Time it with a
release build, swift test -c release, since debug builds are several times
slower.
Task 5: Your own tests
Write tests in MyTests.swift. The strongest one compares isFull with a
direct computation on random grids: a breadth-first search from the open sites of
the top row (Lesson 43 covers breadth-first search in detail; a queue of sites
to visit is all it takes). If the two ever disagree, you have found a bug.
Task 6: The threshold
Read PercolationStats. Each trial opens random blocked sites of an empty
grid until it percolates, and records the fraction of open sites. estimate runs
many trials concurrently, one child task each in a task group (Lesson 22), and
reports their mean with a 95% confidence interval.
Run it from the app’s Estimate button, or write a test that calls it and prints the result. With 200 trials on a 50 × 50 grid, the interval should contain 0.593. Try several grid sizes: small grids give noticeably different, more spread-out answers, and the threshold is sharp only in the limit of large grids.