Question
using java create a UnionFind object variable, initialize it and update&access it to 'solve' the percolation problem. the UF object will be initialized in the
using java create a UnionFind object variable, initialize it and update&access it to 'solve' the percolation problem. the UF object will be initialized in the Percolate constructor, updated in the openmethod, and accessed in the isFull and percolates method.
public class Percolation {
int N;
boolean[] open;
private int top = 0;
private int bottom;
private int size;
private QuickUnionUF qf;
public Percolation(int N) {
this.N = N;
this.open = new boolean[N*N];
bottom = size * size + 1;
qf = new QuickUnionUF(size * size + 2);
}
public void open(int i, int j) {
open[i*N+j] = true;
if (i == 1) {
qf.union(getQFIndex(i, j), top);
}
if (i == size) {
qf.union(getQFIndex(i, j), bottom);
}
if (j > 1 && isOpen(i, j - 1)) {
qf.union(getQFIndex(i, j), getQFIndex(i, j - 1));
}
if (j < size && isOpen(i, j + 1)) {
qf.union(getQFIndex(i, j), getQFIndex(i, j + 1));
}
if (i > 1 && isOpen(i - 1, j)) {
qf.union(getQFIndex(i, j), getQFIndex(i - 1, j));
}
if (i < size && isOpen(i + 1, j)) {
qf.union(getQFIndex(i, j), getQFIndex(i + 1, j));
}
}
public boolean isOpen(int i, int j) {
return open[i*N+j];
}
public boolean isFull(int i, int j) {
if (0 < i && i <= size && 0 < j && j <= size) {
return qf.connected(top, getQFIndex(i , j));
} else {
throw new IndexOutOfBoundsException();
}
}
public boolean percolates() {
return qf.connected(top, bottom);
}
private int getQFIndex(int i, int j) {
return size * (i - 1) + j;
}
}
TODO QUICKFIND Object
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started