Algorithms I: Percolation Assignment (Princeton University)
This repository contains the solution for the Percolation programming assignment, part of Princeton University's Algorithms, Part I course available on Coursera. The assignment involves modeling a system to determine when it "percolates" and using simulations to estimate the percolation threshold.
Assignment Goal
The core task is to create a data type (Percolation.java) that models an n-by-n grid where sites can be open or blocked. The system percolates if there's a path of open sites from the top row to the bottom row. A second data type (PercolationStats.java) uses this model to run Monte Carlo simulations, estimating the fraction of open sites needed for percolation (the percolation threshold, p*).
For the full details, problem description, and API requirements, please see the Official Percolation Assignment Specification.
Repository Structure
/src: Contains the Java source code:
Percolation.java: Models the percolation system. (Note: Different implementations addressing the 'backwash' problem exists.)
PercolationStats.java: Runs Monte Carlo simulations.
PercolationVisualizer.java: Visualizes the process.
/inputs-visualizer: Contains sample input text files used for testing and visualization (e.g., input20.txt).
Core Concept
The implementation relies heavily on the Weighted Quick Union-Find algorithm (provided in algs4.jar) to efficiently track the connectivity of open sites in the grid.
How to Compile and Run the Visualizer
These commands assume you are running them from the root directory of this repository.
Choose Implementation: The visualizer uses Percolation.java. If you have multiple versions (e.g., Percolation-noBackwash.java), rename the one you want to use to Percolation.java inside the src directory. The -noBackwash version is recommended for correct visualization of isFull().
Compile:
Download algs4.jar to, say ~/algs4/algs4.jar.
# On Linux/macOS
javac -cp .:~/algs4/algs4.jar src/Percolation.java src/PercolationVisualizer.java
Download algs4.jar to, say C:\Users\username\algs4\algs4.jar
# On Windows
javac -cp .;C:\Users\username\algs4\algs4.jar src/Percolation.java src/PercolationVisualizer.java
This command tells javac where to find the algs4.jar library (dependencies/algs4.jar) and the source files (src), then specifies which source files within the src directory to compile.
Run Visualizer:
# On Linux/macOS
java -cp .:/dependencies/algs4.jar PercolationVisualizer inputs/<input-file.txt>
# On Windows
java -cp .;\dependencies\algs4.jar PercolationVisualizer inputs/<input-file.txt>
Replace <input-file.txt> with an actual filename from the /inputs directory (e.g., input20.txt).
This command tells java where to find the compiled classes (src) and the library (dependencies/algs4.jar), which program to run (PercolationVisualizer), and passes the path to an input file as an argument.
Expected output
Assessment Results:
This implementation achieved a maximum score (101.25%), ensuring correctness by solving the backwash problem and also passing the bonus memory usage requirement.
- Correctness: 38/38 tests passed
- Memory: 9/8 tests passed
- Timing: 20/20 tests passed
- Aggregate score: 101.25%