Advertisement
jules0707

Percolation

Nov 16th, 2017
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. /******************************************************************************
  2.  *  Author:       Jules Martel (julesmartel@gmail.com)
  3.  *  Date:         Nov. 2017
  4.  *  Task: http://coursera.cs.princeton.edu/algs4/assignments/percolation.html
  5.  *  This program estimates the percolation threshold of a size N grid.
  6.  *  ---------NO BACKWASH SOLUTION HERE ! -------------------
  7.  * Correctness:  27/30 tests passed (backwash sites)
  8.  * Memory:       8/8 tests passed
  9.  * Timing:       20/20 tests passed
  10.  * Aggregate score: 94.00%
  11.  * Estimated student memory = 9.00 n^2 + 0.00 n + 192.00   (R^2 = 1.000)
  12.  ******************************************************************************/
  13.  
  14. import edu.princeton.cs.algs4.WeightedQuickUnionUF;
  15.  
  16. public class Percolation {
  17.     private final int n;
  18.     private final int gridSize;
  19.     private final int bottom;
  20.     private final int top;
  21.     private final WeightedQuickUnionUF wquf;
  22.     private boolean[] isOpen;
  23.     private int openSites;
  24.  
  25.     public Percolation(int n) {
  26.         this.n = n;
  27.         if (n <= 0) {
  28.             throw new IllegalArgumentException("N must be > 0");
  29.         }
  30.         gridSize = n * n;
  31.         isOpen = new boolean[gridSize + 2];
  32.         /* we add two sites to the grind top=0
  33.          the grid will percolate when the top and bottom sites are connected*/
  34.         wquf = new WeightedQuickUnionUF(gridSize + 2);
  35.         /* we open top and bottom site which are always open because we cannot randomly
  36.          open them as they stand outside the bounds i and j in connectAdjacentSites */
  37.         openSites = 0;
  38.         bottom = gridSize + 1;
  39.         top = 0;
  40.         isOpen[bottom] = true;
  41.         isOpen[top] = true;
  42.     }
  43.  
  44.     // this maps the grid coordinates to a site number
  45.     private int xyTo1D(int i, int j) {
  46.         validate(i, j);
  47.         return (j + n * (i - 1));
  48.     }
  49.  
  50.  
  51.     private void validate(int i, int j) {
  52.         if (i <= 0 || i > n || j <= 0 || j > n) {
  53.             throw new java.lang.IllegalArgumentException("indices (" + i + ","
  54.                     + j + ") are not between 1 and " + (n));
  55.         }
  56.     }
  57.  
  58.     public void open(int i, int j) {
  59.         validate(i, j);
  60.         int site = xyTo1D(i, j);
  61.         if (!isOpen[site]) {
  62.             isOpen[site] = true;
  63.             openSites++;
  64.             connectAdjacentSites(i, j);
  65.         }
  66.     }
  67.  
  68.     private void connectAdjacentSites(int i, int j) {
  69.         int site = xyTo1D(i, j);
  70.         int adjacent;
  71.  
  72.         // connect to top sites on the first row
  73.         if (i == 1) wquf.union(site, top);
  74.  
  75.         // above site exits only if i>1
  76.         if (i > 1) {
  77.             adjacent = xyTo1D(i - 1, j);
  78.             if (isOpen[adjacent]) {
  79.                 wquf.union(site, adjacent);
  80.             }
  81.         }
  82.  
  83.         // left site
  84.         if (j > 1 && j <= n) {
  85.             adjacent = xyTo1D(i, j - 1);
  86.             if (isOpen[adjacent]) {
  87.                 wquf.union(site, adjacent);
  88.             }
  89.         }
  90.  
  91.         // right site
  92.         if (j >= 1 && j < n) {
  93.             adjacent = xyTo1D(i, j + 1);
  94.             if (isOpen[adjacent]) {
  95.                 wquf.union(site, adjacent);
  96.             }
  97.         }
  98.  
  99.         // below site exists only if i<n
  100.         if (i != n) {
  101.             adjacent = xyTo1D(i + 1, j);
  102.             if (isOpen[adjacent]) {
  103.                 wquf.union(site, adjacent);
  104.             }
  105.         }
  106.  
  107.         // all bottom sites are connected to bottom !
  108.         if (i == n) {
  109.             wquf.union(site, bottom);
  110.         }
  111.     }
  112.  
  113.  
  114.     public boolean isOpen(int i, int j) {
  115.         validate(i, j);
  116.         int site = xyTo1D(i, j);
  117.         return isOpen[site];
  118.     }
  119.  
  120.     public boolean isFull(int i, int j) {
  121.         validate(i, j);
  122.         int site = xyTo1D(i, j);
  123.         return wquf.connected(site, 0);
  124.     }
  125.  
  126.     public int numberOfOpenSites() {
  127.         return openSites;
  128.     }
  129.  
  130.     public boolean percolates() {
  131.         return wquf.connected(top, bottom);
  132.     }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement