COSC 2010 / 2100 Data Structures

Fall 2018

Homework Assignment #4: Good to the Last Byte

Due: Wednesday, September 26th, 11:59PM CST
Submit: Turn in all both of your Java source files using the turnin command on Morbius, "turnin RandomGrid.java Percolate.java".
Work may be completed in teams of two. The names of both partners must appear in a comment block at the top of your files for credit to be given.

Background

Physics, Chemistry, Geology, and Material Science, among other fields, all can include the study of Percolation Theory. Put simply, percolation concerns the movement and filtering of fluids through porous materials. (Thank you, Wikipedia.) Other applications of percolation modeling can examine movement of electricity or fire, but the idea is the same.

Given a grid consisting of open squares and closed squares, (think of the game "Minesweeper",) a simple percolation model checks whether there is a path through the open squares from the top of the grid to the bottom, without passing through closed squares or moving diagonally.

To make things more interesting, the selection of which blocks are closed is typically generated using some random probability distribution. This technique belongs to a much larger class of computational algorithms called Monte Carlo methods, widely used to simulate complex physical and mathematical systems that cannot otherwise be well understood.

RandomGrid

Create a class RandomGrid that generates a randomized grid of integers of size n. Your class's main method should read two initial values from System.in: a positive integer grid size, "n", and a probability threshhold, "p", in the range 0.0 - 1.0, inclusive. Use some simple error checking logic to verify that the provided values are in the required range.

Once you have valid values for n and p, create an n x n grid of integers by calling the constructor for your class. Use the class java.util.Random to generate a random value between 0.0 and 1.0 for each element in the grid; if the random number is less than or equal to the threshhold p, close that grid square off.

Please declare the following methods for the RandomGrid class. I may replace your RandomGrid class with my own, non-random version during testing, so it is important that you conform to this API.

Percolate

Create a Percolate class that uses the RandomGrid class to run a percolation simulation on a random grid. Percolation starts with any open squares in the top row of the grid, and continues to all adjacent squares that can be reached vertically and horizontally without passing though a blocked square.

A recursive exploration of the grid will be more elegant than an iterative algorithm.

Print out the final grid with percolation noted, and a final judgement at the bottom showing whether flow is possible from the top row through to the bottom row.

Notes

  • Test both of your classes with a wide variety of grid sizes and probability threshholds. A later project will build a more complex simulation on top of this one.
  • The Professor has provided a reference implementation for you to compare against. Change to directory ~brylow/cosc2100/Projects/Percolate on Morbius, and run ./RandomGrid or ./Percolate. Note that as grids are randomized, answers will differ.

  • Back
    [Revised 2018 Sep 21 10:39 DWB]