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
1. (5x2-2x+3) + (x2+x+2)
antiseptic1488 [7]
The answer is: x^2-x+15
3 0
2 years ago
You are selling tomatoes. You have already earned $16 today. How many additional pounds of tomatoes do you need to sell to earn
DIA [1.3K]
Six in total darling.
7 0
2 years ago
How can you find the area of a polygon that is not for which you know an area formula
kipiarov [429]

Answer:

See below.

Step-by-step explanation:

If it is a regular polygon then you can look up the formula or break it into shapes you do know the formula.

If it is irregular or a composite figure, break it into regular polygons which do have formulas to find the area. Here is an example:

Example:

Find the area of the composite figure by splitting the figure into two shapes - two rectangles.

One rectangle is 2 x 9 = 18.

The other rectangle is 2 x 4 = 8.

Together the area is 18 + 8 = 26.

6 0
3 years ago
33x-21x=-24+12 <br> I need help on this math problem. Thanks
lions [1.4K]
X=3. 33x-21x equals 12x. 24+12=36. 12x divided by 36 equals 3
8 0
3 years ago
4<br> Solve the system of equations using the substitution method.<br> y = 2<br> 2x+y=8
Sindrei [870]

Answer:  2

Step-by-step explanation:

5 0
2 years ago
Other questions:
  • A regular octagon has what type of symmetry?
    12·1 answer
  • Ryan is buying a new car for his mom. This car costs x dollars, and sales tax is 7%. Write two expressions that describe the tot
    8·1 answer
  • Is 0.135135135135...... a irrational number?
    10·2 answers
  • Missing product homework 9x2=i<br>i=
    9·1 answer
  • What's 3/7 times 3/7?
    15·2 answers
  • Choose the answer that validates that the rate of change is constant by showing that the ratios of the two quantities are propor
    7·1 answer
  • Find the measure of y.<br><br><br><br><br> 145°<br><br><br> 130°<br><br><br> 129°<br><br><br> 134°
    8·1 answer
  • I needa know how to do this
    5·1 answer
  • sarah uses 2/3 of her supply of cheese to make pizza and 1/9 of her supply of cheese to make lasagna. If Sarah uses 2 1/3 pounds
    7·1 answer
  • Help please give answer please
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!