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
I need to figure out how many solutions does the system of equations below have.
Aleonysh [2.5K]

Answer:

The system has one solution.

Step-by-step explanation:

To find the number of solutions of the system, we equal both equations for y.

If we have ax = b, in which both a and b are different of 0, we have one solution.

If both a and b are 0, we have infinite solutions.

If a is 0 and b is not, there are no solutions.

y=6/7x-8 and y=7/9x+10/9

So

\frac{6x}{7} - 8 = \frac{7x}{9} + \frac{10}{9}

\frac{6x}{7} - \frac{7x}{9} = \frac{10}{9} + 8

\frac{54x - 49x}{63} = \frac{10}{9} + \frac{72}{9}

\frac{5x}{63} = \frac{82}{9}

45x = 63*82

Both a and b are different of 0, so the system has one solution.

6 0
3 years ago
Which of these is not an integer?<br> 5 points<br> 6/3<br> 3/6<br> 6/2
laiz [17]

Answer:

3/6

Step-by-step explanation:

4 0
3 years ago
Read 2 more answers
How do I find the midpoint
Hunter-Best [27]

To find the midpoint of a line use the midpoint formula which is M = (x1+x2/2 , y1 +y2/2) After plugging in the coordinates for your variables you should get (-1+5/2, -1+2/2) This simplifies to (2, 1/2) which is the coordinates for the middle point of the base XY.

5 0
3 years ago
There are 24 hours in a day, At 7 a.m. how many hours are left in the day?
mariarad [96]

Answer:

17

Step-by-step explanation:

4 0
3 years ago
Read 2 more answers
What is (+12) – (-8)?
Sergio [31]

Answer:

+12+8=+20

Step-by-step explanation:

Hope it helps you!

3 0
3 years ago
Read 2 more answers
Other questions:
  • A computer is on sale for $1600, which is a 20% discount off the regular price. what is the regular price
    14·1 answer
  • Which represents the inverse of the function f(x) = 4x?
    14·1 answer
  • How to write 3,516.568 in expanded form
    10·2 answers
  • Tom want to buy a sweater for $30.in which city will it be the most expensive
    5·1 answer
  • Which of the following choices shows the complete factorization of 50?
    15·2 answers
  • A recipe calls for 2 cups of milk for each batch of pancakes. Jamie would like to triple her recipe. How many cups of milk will
    7·1 answer
  • Which equation has the steepest parabola?
    6·1 answer
  • Entry function to model the situation. Then find the value of the function after the given amount of time. Use T as your variabl
    12·1 answer
  • Please help me i do not understand this
    14·1 answer
  • HELP I NEED THE ORDER
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!