Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello! This is a basic percolation program that I've coded for my homework in Java. The program mostly works, however, when I run it the

Hello! This is a basic percolation program that I've coded for my homework in Java. The program mostly works, however, when I run it the square at position 0,1 is ALWAYS opened. Can you help me figure out why? I'm not sure what I'm doing wrong. I've included WEIGHTEDUF.java and INTERACTIVEPERCOLATIONVISUALIZER.java which are the related files for the PERCOLATION.java assignment.

image text in transcribed

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.

// You can use a second UF to answer percolates. For the second UF, you can connect // all of the elements of the top row and separately all of the elements of the bottom row. // Then you can implement percolates as a single call to "connected".

public class Percolation { int N; boolean[] open; // TODO: more fields to add here int top = N*N; WeightedUF WUF; public Percolation(int N) { this.N = N; this.open = new boolean[N*N]; // TODO: more to do here this.WUF = new WeightedUF(N*N); } // open site (row i, column j) if it is not already public void open(int i, int j) { int position = i*N+j; open[position] = true; // TODO: more to do here. 4 cases. (Is is connect on both the top and the bottom?) if (i == 0) { WUF.union(position, top); } // Check left if (i > 0 && isOpen(i-1, j)) { WUF.union(position, (i-1)*N+j); } // Check right if (i 0 && isOpen(i, j-1)) { WUF.union(position, i*N+(j-1)); } // Check below if (j

WEIGHTEDUF.java

package algs15; import java.util.Arrays; import stdlib.*;

public class WeightedUF implements UF { private int[] id; // id[i] = parent of i private int[] sz; // sz[i] = number of objects in subtree rooted at i private int count; // number of components

/** * Create an empty union find data structure with N isolated sets. */ public WeightedUF(int N) { if (N

/** * Return the id of component corresponding to object p. */ public int find(int p) { if (p = id.length) throw new IndexOutOfBoundsException(); while (p != id[p]) { p = id[p]; } return p; }

/** * Return the number of disjoint sets. */ public int count() { return count; }

/** * Are objects p and q in the same set? */ public boolean connected(int p, int q) { return find(p) == find(q); }

/** * Replace sets containing p and q with their union. */ public void union(int p, int q) { int pid = find(p); int qid = find(q); if (pid == qid) return;

// make smaller root point to larger one // in the case of a tie, p is the champion if (sz[pid]

public String toString() { return Arrays.toString (id); }

public static void main(String[] args) { StdIn.fromFile ("data/tinyUF.txt"); //StdIn.fromFile ("data/mediumUF.txt"); //StdIn.fromFile ("data/largeUF.txt"); //StdIn.fromFile ("/tmp/quiz1UF.txt");

int N = StdIn.readInt(); WeightedUF uf = new WeightedUF(N);

// read in a sequence of pairs of integers (each in the range 0 to N-1), // calling find() for each pair: If the members of the pair are not already // call union() and print the pair. Stopwatch sw = new Stopwatch (); while (!StdIn.isEmpty()) { int p = StdIn.readInt(); int q = StdIn.readInt(); if (uf.connected(p, q)) continue; uf.union(p, q); StdOut.println(p + " " + q); GraphvizBuilder.ufToFile (uf.id); } StdOut.format("UF # components: %d [%f] ", uf.count(), sw.elapsedTime ()); StdOut.println (uf);

}

}

INTERACTIVEPERCOLATIONVISUALIZER.java

package algs15.perc; import stdlib.*;

/* ************************************************************************** * This program takes the grid size N as a command-line argument. * Then, the user repeatedly clicks sites to open with the mouse. * After each site is opened, it draws full sites in light blue, * open sites (that aren't full) in white, and blocked sites in black. * ****************************************************************************/ public class InteractivePercolationVisualizer { private static final int delay = 1;

public static void main(String[] args) { args = new String[] { "4" };

// N-by-N percolation system (read from command-line, default = 10) int N = 10; if (args.length == 1) N = Integer.parseInt(args[0]);

// turn on animation mode StdDraw.show(0);

// repeatedly open site specified my mouse click and draw resulting system StdOut.println(N);

Percolation perc = new Percolation(N); PercolationVisualizer.draw(perc, N); StdDraw.show(delay); while (true) {

// detected mouse click if (StdDraw.mousePressed()) {

// screen coordinates double x = StdDraw.mouseX(); double y = StdDraw.mouseY();

// convert to row i, column j int i = (int) (N - Math.floor(y) - 1); int j = (int) (Math.floor(x));

// open site (i, j) provided it's in bounds if (i >= 0 && i = 0 && j

// draw N-by-N percolation system PercolationVisualizer.draw(perc, N); } StdDraw.show(delay); } } }

Standard Draw File 1 open sites does not percolate

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

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions