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
Need help with two questions please
balandron [24]

Answer:

v is negative for every x, meaning the domain is all values in R, whatever goes under the squared sign will come out as positive and the left out -ve sign will make V negative for all x in R

range of f(x) is -inf to +inf

Step-by-step explanation:

the cubic power doesnt affect the sign

3 0
3 years ago
Lala how can you use a formula to find the perimeter of a polygon that has sides of equal length
goldfiish [28.3K]

if itis perimeter add all sides

if it is área multiply

4 0
3 years ago
The length of a side of a triangle is 26 in. A line, parallel to the given side ,divides the triangle into 2 parts of equal area
Ugo [173]

Answer:

13\sqrt{2}

7 0
3 years ago
Read 2 more answers
Is the following relation a function yes or no (9th-grade algebra 1)
solniwko [45]

Answer: its yes

Step-by-step explanation:

5 0
3 years ago
Read 2 more answers
Brandon buys a radio for $45.09 in a state where the sales tax is 7%. How much does he pay in taxes? If necessary round to the n
dusya [7]

Answer:

Step-by-step explanation:

tax = 7% of $45.09 = 0.07×$45.09 ≅ $3.16

Total cost = $45.09 + $3.16 = $48.25

5 0
2 years ago
Other questions:
  • ILL GIVE BRAINLIEST TO WHOEVER ANSWERS FIRST
    12·2 answers
  • What does 6 x 6+32 x 3=?<br><br>Show your work ​
    6·2 answers
  • Let f(x) = x^2 + 6 and g(x)= x+8/x . Find ( g o f)(­ -7)
    14·1 answer
  • Suppose has a solution. Explain why the solution is unique precisely when has only the trivial solution. Choose the correct answ
    10·1 answer
  • Dierdre’s work to solve a math problem is shown below.
    13·2 answers
  • ok my question is what is 2912 divided by 12 and i have to show my work so yeah so like do the whole equation please 13892 divid
    14·1 answer
  • (-9 +4 - -6)<br><br><br> please help me:)
    6·2 answers
  • What is your preferred method to construct parallel lines? Explain why your method works by describing what construction propert
    7·1 answer
  • Hi I need help plssssssss
    12·1 answer
  • Find the perimeter of the polygon with the given vertices. Round your answer to the nearest hundredth.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!