HW 2: Percolation

An exercise in using Disjoined Sets. Unfortunately, this is the only homework self-studying students are able to take - though I suspect that's because the other 3 are non-coding.

Prerequisites to running the code.

Instructions: https://fa22.datastructur.es/materials/hw/hw2/

Solution: https://github.com/tomthestrom/cs61b/tree/master/hw2/hw2

As per the instructions, I implemented 2 classes in this assignment:

Introduction

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.)

In the diagrams below, you can see that in the system on the left, the water is able to start in a site on the top row and trickle down through empty sites until it reaches an empty site on the bottom row.

Whereas on the right, the water in the site on the top row has no way of trickling down to an open site on the bottom row.

Source: UC Berkeley CS61B Fa22 - homework 2 (https://fa22.datastructur.es/materials/hw/hw2/images/percolates.png)

Percolation

Requirements

To model a percolation system, in the hw2 package, complete the Percolation data type with the following API:

Corner cases. By convention, the row and column indices are integers between 0 and N − 1, where (0, 0) is the upper-left site: Throw a java.lang.IndexOutOfBoundsException if any argument to open(), isOpen(), or isFull() is outside its prescribed range. The constructor should throw a java.lang.IllegalArgumentException if N ≤ 0.

Performance requirements. Your code must use the WeightedQuickUnionUF class! Do not reimplement the Union Find ADT. The constructor should take time proportional to N^2, all methods should take constant time plus a constant number of calls to the union-find methods union(), find(), connected(), and count().

Your numberOfOpenSites() method must take constant time. Part of the goal of this assignment is to learn how to cast one problem (Percolation) in terms of an already solved problem (Disjoint Sets, a.k.a Union Find).

The problem with percolation. 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 equals 0 (no site is open), the system does not percolate; when p equals 1 (all sites are open), the system percolates. The plots below show the site vacancy probability p versus the percolation probability for 20-by-20 random grid (top) and 100-by-100 random grid (bottom).

When is sufficiently large, there is a threshold value such that when a random N-by-N grid almost never percolates, and when , a random N-by-N 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.

Implementation

About

This was basically about solving a connectedness problem. The system percolates in case the top and bottom are connected, thus we can also say that they are in the same set. The fastest datastructure to check for connectedness is a Weighted Quick Union (with path compression).

At the beginning, all sites (x, y positions on the grid) are closed and all of them are in their own set (disjoined sets). To keep track of each site, I have a private instance attribute representing my (disjoined sets) of type WeightedQuickUnionUF called grid.

As the sites are gradually, randomly, opening - in case two open sites are next to each other, I union them.

To know whether a system percolates, I need to find out, whether a site at the top and a site at the bottom are connected.

To help with this, I created 2 virtual "sentinel" sites, one at the top, the other one at the bottom, so to check whether a system percolates, I just need to check whether they're within the same set.

To meet the performance requirements on isOpen(int row, int col) - to return in constant time, I have another instance attribute called valGrid, which is a boolean array that mirrors the grid. Sites that are open are assigned the value true. I decided to use a one-dimensional array for this, though the API to the end-user of the Percolation class asks for row, col attributes, I later convert it into 1 dimension - this way I have the exact mirror in both the disjoined set grid and the valGrid. To ensure both arrays are mirrored, grid's size is larger by 2 to accomodate for the virtual sites that are stored at the last 2 indexes.

Code

PercolationStats

Requirements

Monte Carlo simulation. To estimate the percolation threshold, consider the following computational experiment:

  • Initialize all sites to be blocked.

  • Repeat the following until the system percolates:

    • Choose a site uniformly at random among all blocked sites.

    • Open the site.

  • The fraction of sites that are opened when the system percolates provides an estimate of the percolation threshold.

To perform a series of computational experiments, in the hw2 package, complete the PercolationStats data type with the following API:

Implementation

I think the code below is fairly straightforward, this was the easy part of the homework. :)

Last updated