Question: This was the code given by the professor package algs15.perc; //import stdlib.*; import algs15.*; // Uncomment the import statements above. // You can test this

This was the code given by the professor

package algs15.perc;

//import stdlib.*;

import algs15.*;

// Uncomment the import statements above.

// You can test this using InteractivePercolationVisualizer and PercolationVisualizer

// All methods should make at most a constant number of calls to the UF data structure,

// except percolates(), which may make up to N calls to the UF data structure.

public class Percolation {

int N;

boolean[] open;

// tells me if the slot open[k] is open

// declare a UF object here

UF u;

// TODO: more fields to add here

public Percolation(int N) {

this.N = N;

this.open = new boolean[N*N];

// this.u = new CompressionUF(); of suitable size... n

// N^2 or N^2 +2 depending on what you do

// TODO: more to do here

}

// open site (row i, column j) if it is not already

public void open(int i, int j) {

open[i*N+j] = true;

// TODO: more to do here.

// union to the neighbors who are open

// neighbors of i,j (i+1,j), (i-1,j), (i,j+1), (i,j-1)

// but be careful of boundary cases

}

// is site (row i, column j) open?

public boolean isOpen(int i, int j) {

return open[i*N+j];

}

// is site (row i, column j) full?

public boolean isFull(int i, int j) {

// TODO

// is the slot not open

// opposite of isOpen

return !isOpen(i,j);

}

// does the system percolate?

public boolean percolates() {

// TODO

// N^2 +2: just check whether top is connected to bottom

// N^ 2:

// check for all in 0th row, whether there is any in last row

// who is connected.

// OR simplify with the idea of adding dummy top and botoom

return false;

}

}

// N^2..

// slots at the top: (0,0)... (o,N-1)

// slots at the bottom: (N-1,0,..N-1,N-1)

// j: loop through the slots at the top

// j: loops through

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!