1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Nesterboy [21]
3 years ago
9

Using linked lists or a resizing array; develop a weighted quick-union implementation that removes the restriction on needing th

e number of objects ahead of time. Add a method newSite() to the API, which returns an int identifier.
Mathematics
1 answer:
balu736 [363]3 years ago
8 0

Answer:

Step-by-step explanation:

package net.qiguang.algorithms.C1_Fundamentals.S5_CaseStudyUnionFind;

import java.util.Random;

/**

* 1.5.20 Dynamic growth.

* Using linked lists or a resizing array, develop a weighted quick-union implementation that

* removes the restriction on needing the number of objects ahead of time. Add a method newSite()

* to the API, which returns an int identifier

*/

public class Exercise_1_5_20 {

public static class WeightedQuickUnionUF {

private int[] parent; // parent[i] = parent of i

private int[] size; // size[i] = number of sites in subtree rooted at i

private int count; // number of components

int N; // number of items

public WeightedQuickUnionUF() {

N = 0;

count = 0;

parent = new int[4];

size = new int[4];

}

private void resize(int n) {

int[] parentCopy = new int[n];

int[] sizeCopy = new int[n];

for (int i = 0; i < count; i++) {

parentCopy[i] = parent[i];

sizeCopy[i] = size[i];

}

parent = parentCopy;

size = sizeCopy;

}

public int newSite() {

N++;

if (N == parent.length) resize(N * 2);

parent[N - 1] = N - 1;

size[N - 1] = 1;

return N - 1;

}

public int count() {

return count;

}

public int find(int p) {

// Now with path compression

validate(p);

int root = p;

while (root != parent[root]) {

root = parent[root];

}

while (p != root) {

int next = parent[p];

parent[p] = root;

p = next;

}

return p;

}

// validate that p is a valid index

private void validate(int p) {

if (p < 0 || p >= N) {

throw new IndexOutOfBoundsException("index " + p + " is not between 0 and " + (N - 1));

}

}

public boolean connected(int p, int q) {

return find(p) == find(q);

}

public void union(int p, int q) {

int rootP = find(p);

int rootQ = find(q);

if (rootP == rootQ) {

return;

}

// make smaller root point to larger one

if (size[rootP] < size[rootQ]) {

parent[rootP] = rootQ;

size[rootQ] += size[rootP];

} else {

parent[rootQ] = rootP;

size[rootP] += size[rootQ];

}

count--;

}

}

public static void main(String[] args) {

WeightedQuickUnionUF uf = new WeightedQuickUnionUF();

Random r = new Random();

for (int i = 0; i < 20; i++) {

System.out.printf("\n%2d", uf.newSite());

int p = r.nextInt(i+1);

int q = r.nextInt(i+1);

if (uf.connected(p, q)) continue;

uf.union(p, q);

System.out.printf("%5d-%d", p, q);

uf.union(r.nextInt(i+1), r.nextInt(i+1));

}

}

}

You might be interested in
FIND THE DISTANCE BETWEEN THE POINTS. ROUND THE ANSWER TO TWO DECIMAL POINTS (3,2) (8,2)
defon

Answer:5

Step-by-step explanation:

If you are looking for the distance then start counting on the first decimal and stop at the second

5 0
3 years ago
Use long division to express each fraction as a decimal
lawyer [7]

Answer:

Sorry what fraction

Step-by-step explanation:

ez matth

6 0
3 years ago
Read 2 more answers
Which is an equation of the line perpendicular to y = -3/4x + 1 and passes through (3, 4)?
topjm [15]

Answer:

Step-by-step explanation:

perp. 4/3

y - 4 = 4/3(x - 3)

y - 4 = 4/3x - 4

y = 4/3x

5 0
3 years ago
What is the cell doing during the Sphase of interphase?
Studentka2010 [4]

Answer:

Synthesis or replication of DNA

Step-by-step explanation:

You can remember that because synthesis starts with an S

3 0
3 years ago
Joe is building a fort for his little brother out of boxes. He is wondering how much room there will be inside the fort.
Levart [38]
The answer is 0.5m cubed.
8 0
2 years ago
Other questions:
  • In two days, 48,000 gallons of oil gushed out of a well. At what rate did the oil flow from the well?
    14·2 answers
  • How would i write 2.318 with the 18 repeating? and how would i show my work?
    7·1 answer
  • Unit 6 Practice Test: Arc Length and Sectors and Inscribed Angles
    6·1 answer
  • Do i divide 65 by 40? ima little confused plz help ASAP
    8·1 answer
  • The population of a town increased from 3450 in 2006 to 4650 in 2011. Find the absolute and relative (percent) increase.
    14·1 answer
  • Solve the equation x=5/3πr^3 for r.​
    12·1 answer
  • Please help me get the answers
    7·1 answer
  • Can someone plz help
    12·2 answers
  • 2/3 divided by 1/4 ill give brainliest
    7·2 answers
  • Here is a rectangle work out the size of angle x and y
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!