Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions

Question

3. A scholarship-holders base.

Answered: 1 week ago