[Algorithms(Princeton)] Week1 - Percolation

  1 public class Percolation {
  2     private boolean[] openSites;
  3     private int gridN;
  4     private WeightedQuickUnionUF UF;
  5     private WeightedQuickUnionUF UFfull;
  6
  7     public Percolation(int N) {
  8         if (N <= 0) {
  9             throw new java.lang.IllegalArgumentException(
 10             "N must be greater than 0");
 11         }
 12         openSites = new boolean[N * N];
 13         gridN = N;
 14         for (int i = 0; i < N * N; ++i) {
 15             openSites[i] = false;
 16         }
 17         // add 2 virtual sites
 18         UF = new WeightedQuickUnionUF(N * N + 2);
 19         UFfull = new WeightedQuickUnionUF(N * N + 1);
 20     }
 21
 22     private void indexChecker(int i, int j) {
 23         if (i < 1 || j < 1 || i > gridN || j > gridN)
 24             throw new java.lang.IndexOutOfBoundsException();
 25     }
 26
 27     public void open(int i, int j) {
 28         indexChecker(i, j);
 29
 30         int indexI = i - 1;
 31         int indexJ = j - 1;
 32
 33         int osIndex = indexI * gridN + indexJ;
 34         if (openSites[osIndex])
 35             return;
 36         openSites[osIndex] = true;
 37
 38         int ufIndex = indexI * gridN + indexJ + 1;
 39         if (indexI == 0) {
 40             UF.union(0, ufIndex);
 41             UFfull.union(0, ufIndex);
 42         }
 43         if (indexI == gridN - 1) {
 44             UF.union(ufIndex, gridN * gridN + 1);
 45         }
 46
 47         boolean bOpen = false;
 48
 49         // union adjacent open sites
 50         int leftIndexI = indexI;
 51         int leftIndexJ = indexJ - 1;
 52         if (leftIndexJ >= 0) {
 53             bOpen = isOpen(leftIndexI + 1, leftIndexJ + 1);
 54             if (bOpen) {
 55                 int leftUFIndex = leftIndexI * gridN + leftIndexJ + 1;
 56                 UF.union(leftUFIndex, ufIndex);
 57                 UFfull.union(leftUFIndex, ufIndex);
 58             }
 59         }
 60
 61         int rightIndexI = indexI;
 62         int rightIndexJ = indexJ + 1;
 63         if (rightIndexJ < gridN) {
 64             bOpen = isOpen(rightIndexI + 1, rightIndexJ + 1);
 65             if (bOpen) {
 66                 int rightUFIndex = rightIndexI * gridN + rightIndexJ + 1;
 67                 UF.union(ufIndex, rightUFIndex);
 68                 UFfull.union(ufIndex, rightUFIndex);
 69             }
 70         }
 71
 72         int upIndexI = indexI - 1;
 73         int upIndexJ = indexJ;
 74         if (upIndexI >= 0) {
 75             bOpen = isOpen(upIndexI + 1, upIndexJ + 1);
 76             if (bOpen) {
 77                 int upUFIndex = upIndexI * gridN + upIndexJ + 1;
 78                 UF.union(upUFIndex, ufIndex);
 79                 UFfull.union(upUFIndex, ufIndex);
 80             }
 81         }
 82
 83         int downIndexI = indexI + 1;
 84         int downIndexJ = indexJ;
 85         if (downIndexI < gridN) {
 86             bOpen = isOpen(downIndexI + 1, downIndexJ + 1);
 87             if (bOpen) {
 88                 int downUFIndex = downIndexI * gridN + downIndexJ + 1;
 89                 UF.union(ufIndex, downUFIndex);
 90                 UFfull.union(ufIndex, downUFIndex);
 91             }
 92         }
 93     }
 94
 95     public boolean isOpen(int i, int j) {
 96         indexChecker(i, j);
 97         return (openSites[(i - 1) * gridN + j - 1]);
 98     }
 99
100     public boolean isFull(int i, int j) {
101         indexChecker(i, j);
102         int indexI = i - 1;
103         int indexJ = j - 1;
104
105         int osIndex = indexI * gridN + indexJ;
106         int ufIndex = osIndex + 1;
107
108         boolean bOpen = isOpen(i, j);
109         boolean isFull = UFfull.connected(0, ufIndex);
110         return (bOpen && isFull);
111     }
112
113     public boolean percolates() {
114         if (gridN == 1)
115             return (openSites[0]);
116         return UF.connected(0, gridN * gridN + 1);
117     }
118 }

You can see Percolation problem here.

http://coursera.cs.princeton.edu/algs4/assignments/percolation.html

This problem is something related to Union-Find.

[Algorithms(Princeton)] Week1 - Percolation

时间: 2024-10-05 05:04:57

[Algorithms(Princeton)] Week1 - Percolation的相关文章

[Algorithms(Princeton)] Week1 - PercolationStats

1 public class PercolationStats { 2 3 private int N; 4 private int T; 5 private double[] results; 6 7 public PercolationStats(int N, int T) { 8 if (N <= 0 || T <= 0) { 9 throw new java.lang.IllegalArgumentException( 10 "N or T must be greater t

Coursera Algorithms Programming Assignment 1: Percolation

题目来源http://coursera.cs.princeton.edu/algs4/assignments/percolation.html 作业分为两部分:建立模型和仿真实验. 最关键的部分就是建立模型对象.模型对象要求如下: 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 s

coursera Algorithms week1 练习测验2:Union-find with specific canonical element

题目原文: Add a method find() to the union-find data type so that find(i) returns the largest element in the connected component containing i. The operations, union(), connected(), and find() should all take logarithmic time or better. 1 import edu.princ

Coursera Algorithms week1 练习测验3:Successor with delete

题目原文: Given a set of n integers S = {0,1,-,N-1}and a sequence of requests of the following form: Remove x from S Find the successor of x: the smallest y in S such thaty>=x design a data type so that all operations(except construction) take logarithmi

Coursera Algorithms week1 Interview Questions: 3Sum in quadratic time

题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case. You may assume that you can sort the n integers in time proportional to n2 or better. 分析: <算法4>这本书提供的TwoSumFast解法为NlogN,ThreeSumFast解法为N2logN,根据课后练

Princeton Algorithms week3 Assignment

这周编程作业是实现检测点共线的算法.和排序算法有关系的地方在于,对斜率排序后可以很快的检测出来哪些点是共线的,另外这个算法的瓶颈也在于排序的性能. 一点收获: java传参数时传递的是值,这很多人都知道,只不过特别要注意的是对于引用类型,传递的是"引用的值",也就是说传递的是指针的副本,因此虽然似乎把一个数组传递进去了,但只要其他地方改动了这个数组,函数内部也会被影响.同理,返回值也是指针,如果不拷贝一份再返回,很有可能你在修改返回值的时候改动到了类的内部变量. 自己还不太明白的地方:

Coursera Algorithms week1 Interview Questions: Search in a bitonic array

题目要求: An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of n distinct integer values, determines whether a given integer

AlgorithmsI Programming Assignment 1: Percolation

3种版本的答案,第一种使用virtual top and bottom site, 但有backwash的问题,解决这个问题有两种方法: 1. 使用2个WQUUF, 但会增加memory. One for checking if the system percolates(include virtual top and bottom), and the other to check if a given cell is full(only include virtual top). 而且要注意,

Programming Assignment 1: Percolation

问题描述可以详见:http://coursera.cs.princeton.edu/algs4/assignments/percolation.html 关于QuickFindUF的javadoc:http://algs4.cs.princeton.edu/15uf/QuickFindUF.java.html 关于WeightedQuickUnionUF的javadoc:http://algs4.cs.princeton.edu/15uf/WeightedQuickUnionUF.java.ht