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]
2 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]2 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
Chuck and Dana agree to meet in Chicago for the weekend.chuck travels 60 miles in the same time that Dana travels 52 miles. if c
Katarina [22]
Chuck travels at A) 30 mph

since chuck and dana travelled at the same amount of time (2 hours), dana travels at 26 mph, which is 4 mph less, and chuck travels at 30.
8 0
2 years ago
Is parallel to 3x-5y=7 and passes through (0,-6)
oksian1 [2.3K]
Yes yes it isssssssssss
5 0
2 years ago
-4.5x-2y=-12.5 3.25x-y=-0.75<br> solve by elimination
creativ13 [48]
-4.5x-2y=-12.5 and 3.25x-y=-0.75
we first multiply the second equation by -2
(-2)*(3.25x-y=-0.75)=-6.5x+2y=-1.5
add the equation to eliminate the y
so yo get (-4.5x-6.5x)=(-12.5-1.5)
-11x=-11
x=1
now we plug in x into any equation to solve for y
-45x-2y=-12.5 and x=1
(-45*1)-2y=-12.5
-4.5-2y=-12.5
-2y=-12.5+4.5
-2y=-8
y=4
then your answer is x=1 and y=4
5 0
3 years ago
Derrick's dad bought new tires for $900
weeeeeb [17]

Answer:

171

Step-by-step explanation:

9 * 19 is 171 dollars so that's what he'll owe

8 0
2 years ago
Hawaii 0.288% = what fraction
Sindrei [870]

Answer:

36⁄125 or if we are talking about the lowest term it is 9 /3125


Step-by-step explanation:

Step 1: 0.288 = 288⁄1000  

Step 2: Simplify 288⁄1000 = 36⁄125 Percent" or "%" means "out of 100" or "per 100",



<em>Or for the lowest term:</em>

Therefore 0.288% can be written as  

0.288

100

.

Next, we can multiply by  

1

in the form of  

1000

1000

to eliminate the decimal in the numerator:

1000

1000

×

0.288

100

⇒

1000

×

0.288

1000

×

100

⇒

288

100000

.

We can now reduce the fraction as:

288

100000

⇒

32

×

9

32

×

3125

⇒

32

×

9

32

×

3125

⇒

9

3125

.

4 0
2 years ago
Other questions:
  • ‼️‼️‼️‼️‼️‼️‼️‼️‼️‼️‼️‼️PLEASE PLEASE HELP!!!!!! I CANT FAIL AGAIN!!!!! ONLY ANSWER IF YOU KNOW YOU HAVE THE RIGHT ANSWER!!!!!!!
    7·1 answer
  • Find the sum and express it in simplest form (8a^2 + 6a + 5) + (-3a^2 - 2a)
    12·2 answers
  • Put the following values in order from least<br> to greatest.<br> 20, 22, (-2)2 -22 -20
    7·2 answers
  • I've done the sketch but am unsure as what my next steps should be?
    12·1 answer
  • What is the value of x- This is for brainliest!
    6·1 answer
  • Let f(x) = ............
    11·1 answer
  • Each of the options below describes a relationship that you need to graph in a coordinate plane. which of the options will have
    14·1 answer
  • Jessica conducts an experiment. She flips a coin 100 times. In her experiment she gets heads 40 times and tails 60 times. Based
    12·2 answers
  • I don’t get this problem:// I’m so confused. I need help asap!!!
    8·1 answer
  • Conditionals: photo attached
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!