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
(x-y)^2 (x^2-2xy+y^2)
Andrei [34K]
What are u asking me too do factor simplify??
5 0
2 years ago
100 POINTS ANSWER FAST NO BEGINNERS OR AMBITIOUS, VIRTUOSO AND UP ONLY
Flauer [41]
I hope this helps you

8 0
3 years ago
Read 2 more answers
Find the value of x (4x-10) (x)
mote1985 [20]
The is value =4x^3-10x^2
3 0
3 years ago
Of the 36 students in the book club .five ninths Of the students prefer adventure books to historical books. How many of the stu
Effectus [21]
You have to make a tape diagram and divide it in to 9 parts because it is 5 out of 9 then you will make a bracket pointing to only 4 parts of the tape diagram then you do 36/9 and it will equal 4 so put 4 in to each part and so 4 x 4 is 16 so the answer is 16 people prefer historical books
8 0
3 years ago
Read 2 more answers
Given the following functions f(x) and g(x), solve (f+g)(2) and select the correct answer below:
Anika [276]

Answer:

B

Step-by-step explanation:

In the picture above

Hope this helps.

3 0
3 years ago
Other questions:
  • Most _____ jobs are worked 10 to 20 hours per week.
    5·1 answer
  • What is another word for mean?
    5·2 answers
  • What are the solutions of 2x^2 +16+34=0?
    13·1 answer
  • The Pythagorean theorem, also known as the pythagoras’ theorem, is a fundamental relation in the geometry among the three sides
    7·1 answer
  • GEOMETRY - PLEASE HELP - WILL MARK BRAINLIEST
    14·2 answers
  • I need help to solve this question(offering 43 points)
    13·1 answer
  • Find the next term of the sequence.<br> 24, 17, 10, 3, ...<br><br> -7 <br> 0<br> -4<br> -5
    9·1 answer
  • Find N and explain how
    15·1 answer
  • Plz help due today this is one question plz show work
    9·1 answer
  • 7th-grade easy math PLEASE Answer correctly this is for a grade
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!