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
Please help Quickly
attashe74 [19]

Answer:

i gess alice 22 year old

8 0
3 years ago
At blood drive, 5 donors with type O+ blood, 5 donors with type A+ blood, and 2 donors with type B+ blood in line. In how many d
neonofarm [45]

Answer: I think 8

I am Not sure hope this helps though

Step-by-step explanation:

4 0
3 years ago
Find the sell tax to complete the table
poizon [28]
The answer would be $3.2
Do ya want me to explain my answer
4 0
3 years ago
Only answer if you are sure!!
bezimeni [28]
Answer

standard deviation

Explanation

Standard deviation is the measure of spread of data around the mean.

Mode is the most common value - which would be the mean in a normal-like distribution.

Median is the middle value when the data is ranked in ascending order (or descending even)

Skewness, is when the mode of the data falls behind the mean by a large margin.

An example is GDP, a few billionaires increase the number number and distort it, even though most people are of low or middle based income.
8 0
3 years ago
If a person weighs 180 pounds on the surface of the earth and the radius of the earth is 3900 miles, what will the person weigh
emmasim [6.3K]
Gravity is inversely proportional to the square of the distance.
In English:
Your weight at the Earth's surface and your weight 850 miles above the Earth's surface decreases by a factor of

(4,750 / 3,900) ^ 2 = <span> <span> <span> 1.483399080 </span> </span> </span>

So your weight would decrease by (your weight / <span> <span> <span> 1.483399080 </span> </span> </span> )
If you weigh 180 pounds, at 850 miles above the Earth your weight is
180 / <span> <span> <span> 1.483399080 </span> </span> </span> which equals
<span> <span> <span> 121.3429362515 </span> </span> </span>  pounds.


7 0
3 years ago
Other questions:
  • Consider these three numbers written in scientific notation.
    15·2 answers
  • The equation of a circle is given below (x+7) 2 +(y+8)^2 = 4/9
    7·1 answer
  • Angles one, two, and three please! I can't figure it out
    6·2 answers
  • Can someone explain how you step by step find the slope to questions like this along with the answer? I forget how but I also ca
    14·1 answer
  • The ordered pairs model an exponential function, where p is the function name and t is the input variable.
    15·1 answer
  • Amelia has a savings account that has an annual yield of 5.2%. Find the balance of the account after each of the first five year
    13·1 answer
  • The assignment is about : Subtract Polynomials What i have to do : Subtract the polynomials, showing all the steps necessary to
    6·1 answer
  • Ami and her friends went for a dance lesson. It cost $242 before taxes. Tax and gratuity added 23% in additional charges. Ami ca
    8·2 answers
  • Which equation has a vertex of (-3, 4)?
    14·1 answer
  • Determine the equation of the line that passes through the given points (if you have a graphing calculator, you can use the tabl
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!