Problem 2. (Union Find Percolation) Develop a data type called UFPercolation that implements the Percolation interface...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Problem 2. (Union Find Percolation) Develop a data type called UFPercolation that implements the Percolation interface using a WeightedQuickUnionUF object as the underlying data structure. UFPercolation implements Percolation UFPercolation(int n) constructs an n x n Corner cases: • UFPercolation() should throw an IllegalArgumentException("Illegal n") if n ≤ 0. • open(), isOpen(), and isFul1() should throw an IndexOutOfBounds Exception ("Illegal i or j") ifi or j is outside the interval [0, n-1]. Performance requirements: isOpen() and number0fOpenSites() should run in time T(n)~ 1. • open(), isFull(), and percolates () should run in time T(n) ~ logn. • UFPercolation() should run in time T(n) ~ n². > "/workspace/project1 $ java UFPercolation data/input10.txt 10 x 10 system: Open sites = 56 Percolates = true percolation system, with all sites blocked $ java UFPercolation data/input 10-no.txt 10 x 10 system: Open sites = 55 Percolates = false Directions: • Model percolation system as an n x n array of booleans (true open site and false blocked site). • Create an UF object with n²+2 sites and use the private encode() method to translate sites (0,0), (0, 1),..., (n-1, n-1) of the array to sites 1, 2,..., n² of the UF object; sites 0 (source) and n² + 1 (sink) are virtual, ie, not part of the percolation system. 5 / 10 • A 3 x 3 percolation system and its UF representation 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 Project 1 (Percolation) Instance variables: - Percolation system size, int n. - Percolation system, boolean [] [] open. - Number of open sites, int openSites. Union-find representation of the percolation system, WeightedQuickUnionUF uf. - Initialize instance variables. 1 4 7 0 2 8 10 private int encode(int i, int j) - Return the UF site (1,2,..., n²) corresponding to the percolation system site (i, j). • public UFPercolation (int n) 3 6 9 void open (int i, int j) - If site (i, j) is not open: * Open the site * Increment openSites by one. * If the site is in the first (or last) row, connect the corresponding uf site with the source (or sink). * If any of the neighbors to the north, east, west, and south of site (i, j) is open, connect the uf site corresponding to site (i, j) with the uf site corresponding to that neighbor. boolean isFull (int i, int j) Return whether site (i, j) is full or not a site is full if it is open and its corresponding uf site is connected to the source. int number Of OpenSites () - Return the number of open sites. boolean percolates () Return whether the system percolates or not a system percolates if the sink is connected to the source. • Using virtual source and sink sites introduces what is called the back wash problem. 0,0 0,1 0,2 1,0 1,1 1,2 Project 1 (Percolation) • In the 3 x 3 system, consider opening the sites (0,1), (1, 2), (1, 1), (2,0), and (2, 2), and in that order; the system percolates once (2, 2) is opened. 2,0 2,1 2,2 1 4 7 0 2 5 8 10 3 6 6 / 10 9 • The site (2, 0) is technically not full since it is not connected to an open site in the top row via a path of neighboring (north, east, west, and south) open sites, but the corresponding uf site (7) is connected to the source, so is incorrectly reported as being full this is the back wash problem. • To receive full credit, you must resolve the back wash problem. UFPercolation - Notepad File Edit Format View Help import dsa.WeightedQuickUnionUF; import stdlib.In; import stdlib.stdout; // An implementation of the Percolation API using the UF data structure. public class UFPercolation implements Percolation { // Constructs an n x n percolation system, with all sites blocked. public UFPercolation (int n) { } // Opens site (i, j) if it is not already open. public void open(int i, int j) { } // Returns true if site (i, j) is open, and false otherwise. public boolean isOpen(int i, int j) { } // Returns true if site (i, j) is full, and false otherwise. public boolean isFull(int i, int j) { } // Returns the number of open sites. public int numberOfOpenSites () { } // Returns true if this system percolates, and false otherwise. public boolean percolates () { } // Returns an integer ID (1...n) for site (i, j). private int encode(int i, int j) { } I X } // Unit tests the data type. [DO NOT EDIT] public static void main(String[] args) { String filename = args [0]; In in new In (filename); int n = in.readInt (); UFPercolation perc = new UFPercolation (n); while (!in.isEmpty()) { } } int i in.readInt (); int j = in.readInt (); perc.open(i, j); stdout.printf("%d x %d system: \n", n, n); stdout.printf(" Open sites = %d\n", perc.numberOfOpenSites ()); stdout.printf(" Percolates = %b\n", perc.percolates()); if (args.length == 3) { int i = Integer.parseInt(args[1]); int j = Integer.parseInt (args[2]); stdout.printf(" is Full (%d, %d) = %b\n", i, j, perc.isFull(i, j)); Problem 2. (Union Find Percolation) Develop a data type called UFPercolation that implements the Percolation interface using a WeightedQuickUnionUF object as the underlying data structure. UFPercolation implements Percolation UFPercolation(int n) constructs an n x n Corner cases: • UFPercolation() should throw an IllegalArgumentException("Illegal n") if n ≤ 0. • open(), isOpen(), and isFul1() should throw an IndexOutOfBounds Exception ("Illegal i or j") ifi or j is outside the interval [0, n-1]. Performance requirements: isOpen() and number0fOpenSites() should run in time T(n)~ 1. • open(), isFull(), and percolates () should run in time T(n) ~ logn. • UFPercolation() should run in time T(n) ~ n². > "/workspace/project1 $ java UFPercolation data/input10.txt 10 x 10 system: Open sites = 56 Percolates = true percolation system, with all sites blocked $ java UFPercolation data/input 10-no.txt 10 x 10 system: Open sites = 55 Percolates = false Directions: • Model percolation system as an n x n array of booleans (true open site and false blocked site). • Create an UF object with n²+2 sites and use the private encode() method to translate sites (0,0), (0, 1),..., (n-1, n-1) of the array to sites 1, 2,..., n² of the UF object; sites 0 (source) and n² + 1 (sink) are virtual, ie, not part of the percolation system. 5 / 10 • A 3 x 3 percolation system and its UF representation 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 Project 1 (Percolation) Instance variables: - Percolation system size, int n. - Percolation system, boolean [] [] open. - Number of open sites, int openSites. Union-find representation of the percolation system, WeightedQuickUnionUF uf. - Initialize instance variables. 1 4 7 0 2 8 10 private int encode(int i, int j) - Return the UF site (1,2,..., n²) corresponding to the percolation system site (i, j). • public UFPercolation (int n) 3 6 9 void open (int i, int j) - If site (i, j) is not open: * Open the site * Increment openSites by one. * If the site is in the first (or last) row, connect the corresponding uf site with the source (or sink). * If any of the neighbors to the north, east, west, and south of site (i, j) is open, connect the uf site corresponding to site (i, j) with the uf site corresponding to that neighbor. boolean isFull (int i, int j) Return whether site (i, j) is full or not a site is full if it is open and its corresponding uf site is connected to the source. int number Of OpenSites () - Return the number of open sites. boolean percolates () Return whether the system percolates or not a system percolates if the sink is connected to the source. • Using virtual source and sink sites introduces what is called the back wash problem. 0,0 0,1 0,2 1,0 1,1 1,2 Project 1 (Percolation) • In the 3 x 3 system, consider opening the sites (0,1), (1, 2), (1, 1), (2,0), and (2, 2), and in that order; the system percolates once (2, 2) is opened. 2,0 2,1 2,2 1 4 7 0 2 5 8 10 3 6 6 / 10 9 • The site (2, 0) is technically not full since it is not connected to an open site in the top row via a path of neighboring (north, east, west, and south) open sites, but the corresponding uf site (7) is connected to the source, so is incorrectly reported as being full this is the back wash problem. • To receive full credit, you must resolve the back wash problem. UFPercolation - Notepad File Edit Format View Help import dsa.WeightedQuickUnionUF; import stdlib.In; import stdlib.stdout; // An implementation of the Percolation API using the UF data structure. public class UFPercolation implements Percolation { // Constructs an n x n percolation system, with all sites blocked. public UFPercolation (int n) { } // Opens site (i, j) if it is not already open. public void open(int i, int j) { } // Returns true if site (i, j) is open, and false otherwise. public boolean isOpen(int i, int j) { } // Returns true if site (i, j) is full, and false otherwise. public boolean isFull(int i, int j) { } // Returns the number of open sites. public int numberOfOpenSites () { } // Returns true if this system percolates, and false otherwise. public boolean percolates () { } // Returns an integer ID (1...n) for site (i, j). private int encode(int i, int j) { } I X } // Unit tests the data type. [DO NOT EDIT] public static void main(String[] args) { String filename = args [0]; In in new In (filename); int n = in.readInt (); UFPercolation perc = new UFPercolation (n); while (!in.isEmpty()) { } } int i in.readInt (); int j = in.readInt (); perc.open(i, j); stdout.printf("%d x %d system: \n", n, n); stdout.printf(" Open sites = %d\n", perc.numberOfOpenSites ()); stdout.printf(" Percolates = %b\n", perc.percolates()); if (args.length == 3) { int i = Integer.parseInt(args[1]); int j = Integer.parseInt (args[2]); stdout.printf(" is Full (%d, %d) = %b\n", i, j, perc.isFull(i, j));
Expert Answer:
Answer rating: 100% (QA)
import dsaWeightedQuickUnionUF import stdlibIn import stdlibStdOut An implementation of the Percolation API using the UF data structure public class U... View the full answer
Related Book For
Applied Regression Analysis and Other Multivariable Methods
ISBN: 978-1285051086
5th edition
Authors: David G. Kleinbaum, Lawrence L. Kupper, Azhar Nizam, Eli S. Rosenberg
Posted Date:
Students also viewed these programming questions
-
Zanthum Company uses job-order costing. At the end of the month, the following information was gathered: The beginning balance of Finished Goods was $300, consisting of Job 300 which was not sold by...
-
In Problem 12.4 on page 423, you used the percentage of alcohol to predict wine quality. The data are stored in VinhoVerde. From the results of that problem, b 1 = 0.5624 and Sb 1 = 0.1127. a. At the...
-
A Poisson distribution might be a good approximation for which of the following? a. Runners crossing the finish line in the Boston Marathon b. Arrival times of the students in your OSCM class c....
-
Corpus Christi Bicycle Company manufactures commuter bicycles from recycled materials. Corpus Christi Bicycle Company records standard costs in its accounts. The following data for March are...
-
Under the assumptions of the linear model, the residual plot will exhibit a linear pattern. In Exercises 9 and 10, determine whether the statement is true or false. If the statement is false, rewrite...
-
On January 1, 2017, Schipper Ltd. had the following shareholders' equity accounts: Common shares (1,000,000 issued)......................$1,500,000 Retained...
-
Several of JJJ Company's important ratios are given here for the years 2018, 2019 and 2020 with the industry ratios for 2020. The company is considering borrowing more funds for expansion. JJJ...
-
A village has six residents, each of whom has accumulated savings of $100. Each villager can use this money either to buy a government bond that pays 10 percent interest per year or to buy a...
-
Do you think the insolvency of Social Security can occur sooner as the result of the COVID-19 pandemic?
-
Please describe what a Galera cluster is and how it works. In your description, compare a Galera cluster to a Windows server cluster and the strengths and weaknesses of each synchronous and...
-
Ivanhoe Inc. made a $23000 sale on account with the following terms: 1/15, n/30. If the company uses the net method to record sales made on credit, how much should be recorded as revenue?
-
What is a product that is characterize as being 'out of control' from a statistical process control perspective. How could statistical process control be used to assign the cause of this quality...
-
Three capacitors are connected in parallel. Each has plate area A = 6.0010 -2 m 2 and plate spacing d = 2.6010 -3 m . What must be the spacing of a single capacitor of plate area A if its capacitance...
-
A company sold a machine to B, the price was 100,000, and there were no special concessions or discounts associated with the sale. As this was the first transaction with B, B asked for and was...
-
Calculating Limits using the Limit Laws (a) (b) (c) (d) (e) 0 lim [f(x) + g(x)] X-2 y = f(x) lim [f(x) = g(x)] X40 lim [f(x)g(x)] x-1 lim f(x) x-3 g(x) lim [xf(x)] X-2 (f) f(-1) + lim g(x) x-1 48 0...
-
The following T-accounts show postings of selected transactions. Indicate the journal used in recording each of these postings a through e. Cash Accounts Receivable Inventory (d) 500 (e) 300 (b)...
-
Use the computer output for the radial keratotomy data of Problem 12 in Chapter 8, along with the additional output here, to answer the following questions. a. Perform the overall F test for the...
-
An advertising company evaluated three types of television ads for a new low-cost subcompact automobile: visual-appeal ads, budget-appeal ads, and feature-appeal ads. To control for age differences,...
-
Consider a different (fictitious) study to compare two treatments for the relief of heartburn. This new study involves a two-period "cross-over" design in which each subject is given two...
-
Justify the statement: Osmosis is of paramount importance in biological systems.
-
The freezing point of pure benzene is \(5.44^{\circ} \mathrm{C}\) and that of the solution containing \(2.092 \mathrm{~g}\) of benzaldehyde in \(100 \mathrm{~g}\) of benzene is \(4.44^{\circ}...
-
The molality of dissolved gases in water at \(0^{\circ} \mathrm{C}\) and \(1 \mathrm{~atm}\) is \(1.29 \times 10^{-3}\). The decrease in volume during melting of ice is \(0.0907 \mathrm{cc} /...
Study smarter with the SolutionInn App