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
Alyssa is paid a 5 percent commission on her total sales each month. Last month, she sold $30,000 worth of items. After a state
grigory [225]
$1,365 is the answer, (the third choice)
3 0
3 years ago
Which of the following expressions have a quotient of −17?
beks73 [17]

Answer:

C = -6

Step-by-step explanation:

1 +-7 =-6:::::::::::::::::::

5 0
3 years ago
Which property justifies the following equation?
Leviafan [203]
1. . D . Identity , when you odd both properties it will always to be true on both sides
5 0
3 years ago
1. The figure shows a graph of the function of f(x) in the xy-coordinate plane, with the vertex at (1,9) and the zeros
never [62]

Answer:

f{0) is greater than g(0) and f(2) is greater than g(2).

Step-by-step explanation:

f{0) is greater than g(0) = f(0)=8  and g(0)=2

f(2) is greater than g(2) = f(2)=8 and g(2)= -4

3 0
3 years ago
What is hydrogen and oxygen classified as: minerals, energy, organisms or elements
OLga [1]
A. Hydrogen and oxygen are classified as elements.

Explanation: they are from the periodic table (which is a table of elements) and it wouldn’t make sense for them to be minerals, energy or organisms. They aren’t living things.
7 0
3 years ago
Read 2 more answers
Other questions:
  • Trey reads 10 pages in 25 minutes. At the rate, how many pages will he read in 45 minutes?
    11·1 answer
  • Simplify the product. (5 − 6)(2 + 7)
    7·1 answer
  • Which is one reason why this expression is equal to 0?
    15·2 answers
  • Find the measure of x. Line PU has points R and S between points P and U, lines QR and ST are parallel, line QR intersects line
    13·2 answers
  • Calcula el volumen en metros cúbicos de una piscina que tiene 8 m de largo 2 m de ancho y 1.5 m de profundidad
    14·1 answer
  • 10(3.25-1.25)+3/4 help please brain brain no work
    13·2 answers
  • Kevin paid $2.52 for 6 juice boxes. How much should Kevin expect to pay for 18 juice boxes?
    14·2 answers
  • La derivada de f(x)=3x^2-5x+2
    8·2 answers
  • Meryl needs to add enough water to 11 gallons of an 18% detergent solution to make a 12% detergent solution. Which
    8·1 answer
  • 15x-6y=48 in simplest form
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!