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
What does it Mean to have the same size shape and measure
kiruha [24]

When something has the same size,shape and measure it is congruent

5 0
4 years ago
Jasper needs 180 of string for his project how many yards did he buy.
andrew-mc [135]

Answer:

5 yards

Step-by-step explanation:

The measurements of the 180 should be Inches

Length of string Jasper needs = 180 inches

how many yards did he buy?

1 yard = 36 inches

Number of yards he needs = Length of string Jasper needs / inches per yard

= 180 inches / 36 inches

= 5 yards

Number of yards he needs = 5 yards

3 0
3 years ago
You can buy a ten pound bag of apples for 12.70 express this situation in as a ratio
Sav [38]
What does it want a unit rate? or?

7 0
3 years ago
Graph the equation. Label the points where the line crosses the axes. What are the points How do i even get the points? y = x +
gladu [14]
Hello, 

I'm not going to do it for you, I'll explain you how to do it, so you can LEARN something. Like that is the equation of a line, we only need two points to draw it, they can be the x-intercept an y-intercept.

When we are looking for the x-intercept, y=0
And when we are looking for the y-intercept,  x=0

Like you can see in the picture, so replace x=0 to find the y-intrcept and y=0 to find the x-intercept and join them, as you saw in the picture and there you have the graph.

If you have any trouble don't hesitate to tell me.

3 0
3 years ago
If you gave to pay $14,000 and you pay 75% of it how much do you have left to pay?
Mars2501 [29]
So, what you do for this problem is that you multiply $14,000 x 0.75
The answer would be $ 10500. You would need to pay $ 10,500.
Hope this helps!
8 0
3 years ago
Read 2 more answers
Other questions:
  • Answer the question please for 10 points
    14·1 answer
  • If cos(θ)=2853 with θin Q IV, what is sin(θ)?
    12·1 answer
  • A contractor is building a circular swimming pool. The radius of the circle 12 feet. He needs to know the circumference in order
    8·1 answer
  • What is the greatest common factor and least common multiple of 20, and 22
    12·2 answers
  • Help please
    15·1 answer
  • Your answer should be a polynomial in standard form. ( − 2 h + 9 ) ( 9 h − 2 ) = (−2h+9)(9h−2)=
    12·1 answer
  • Which factors are not included in the prime factorisation of a compisite number​
    8·1 answer
  • Two coins are tossed. Assume that each event is equally likely to occur. ​a) Use the counting principle to determine the number
    12·1 answer
  • Can someone please find the slope, no link answers pls
    14·1 answer
  • Write the equation of the line with a slope of 2/3 and y-intercept of 1. Write your answer in slope-intercept form
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!