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
Two triangles can be formed with the given information. Use the Law of Sines to solve the triangles. A = 56°, a = 16, b = 17 (5
Blizzard [7]

Answer:

B = 61.75°

Step-by-step explanation:

The formula for the Law of Sines is given as:

a/ sin A = b/ sin B

In the question, we are given the following values

A = 56°, a = 16, b = 17

We are to solve for B

Hence,

16/ sin 56° = 17/sin B

Cross Multiply

sin B × 16 = sin 56° × 17

sin B = sin 56° × 17/16

sin B = 0.88

B = arc sin(0.88)

B = 61.74536°

Approximately B = 61.75°

7 0
3 years ago
Neil goes to the pet shop and
umka2103 [35]

Answer:

b (1/15)

Step-by-step explanation:

6 0
3 years ago
Read 2 more answers
Write the equation of the line passing through the points (2,-5) and (7,-3).
Grace [21]

Answer:

5y·2x=21

<em>\frac{y - y1}{y2 - y1}  =  \frac{x - x1}{x2 - x1}</em>

Step-by-step explanation:

using the formula above,you will find the equation 5y–2x=21

7 0
3 years ago
0.729 rounded by the nearest tenth
katrin [286]
0.7 is rounded to the tenth place
4 0
3 years ago
Read 2 more answers
Two cars simultaneously left Points A and B and headed towards each other, and met after 2 hours and 45 minutes. The distance be
zheka24 [161]
<h2>Hello!</h2>

The answer is:

FirstCarSpeed=41mph\\SecondCarSpeed=55mph

<h2>Why?</h2>

To calculate the speed of the cars, we need to write two equations in order to create a relation between the two speeds and be able to isolate one in function of the other.

So, let be the first car speed "x" and the second car speed "y", writing the equations we have:

For the first car:

x_{FirstCar}=x_o+v*t

For the second car:

We know that the speed of the second car is the speed of the first car plus 14 mph, so:

x_{SecondCar}=x_o+(v+14mph)*t

Now, we already know that both cars met after 2 hours and 45 minutes, meaning that positions will be the same at that moment, and the distance between A and B is 264 miles,  so, we can calculate the relative speed between them:

If the cars are moving towards each other the relative speed will be:

RelativeSpeed=FirstCarSpeed-(-SecondCarspeed)\\\\RelativeSpeed=x-(-x-14mph)=2x+14mph

Then, since we know that they covered a combined distance which is equal to 264 miles of distance in 2 hours + 45 minutes, we  have:

2hours+45minutes=120minutes+45minutes=165minutes\\\\\frac{165minutes*1hour}{60minutes}=2.75hours

Writing the equation, we have:

264miles=(2x+14mph)*t\\\\264miles=(2x+14mph)*2.75hours\\\\2x+14mph=\frac{264miles}{2.75hours}\\\\2x=96mph-14mph\\\\x=\frac{82mph}{2}=41mph

We have that the speed of the first car is equal to 41 mph.

Now, for the second car we have that:

SecondCarSpeed=FirstCarSpeed+14mph\\\\SecondCarSpeed=41mph+14mph=55mph

Hence, we have that:

FirstCarSpeed=41mph\\SecondCarSpeed=55mph

Have a nice day!

4 0
3 years ago
Read 2 more answers
Other questions:
  • You have a normal model. The population mean is 500 and standard deviation is 100. Which of the following options would be the c
    9·1 answer
  • 2x - y - 4 = 0
    12·2 answers
  • Which function has a simplified base of 4? f(x) = 2 f(x) = 2 f(x) = 4 f(x) = 4
    14·2 answers
  • A sample of bacteria is decaying according to a half-life model. If the sample begins with 700 bacteria, and after 10 minutes th
    8·1 answer
  • I need help on these question ASAP!!!! This is Urgent
    7·1 answer
  • Add brackets to the following equation to make it time. 1 + 72 ÷4 × 2 =10
    11·1 answer
  • Find the system of linear equations represented by the augmented matrix. Then use back substitution to solve. (Use variables x,
    7·1 answer
  • Please help with questions. This is geometry btw
    7·1 answer
  • Can someone please help me with math.
    11·1 answer
  • Your grade.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!