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
Marianna Martinez sells copy paper to local schools. She earns a 13 percent straight commission on all sales . In November her s
faltersainse [42]

Answer:

13%=0.13

28540*0.13=3710.2

8 0
3 years ago
40 points” help HLP HL
cestrela7 [59]

Answer:

D: {(-5, -4, 2, 2, 5)}

R: {(-6, 3, 4, 1, 5)}

The relation is NOT a function.

Step-by-step explanation:

By definition:

A relation is any set of ordered pairs, which can be thought of as (input, output).

A function is a <em><u>relation</u></em> in which no two ordered pairs have the same first component (domain/input/x value) and different second components  (range/output/y value).

Looking at the given points in your graph, and in listing down the domain and range, we can infer that the relation is not a function because there is an x-value (2) that has two corresponding y-values: (2, 4) (2, 1).

Another way to tell if a given set of points in a graph represents a function by doing the "Vertical line test." The graph of an equation represents y as a function of x if and only if no vertical line intersects the graph more than once. Looking at the attached image, I drew a vertical line over points (2, 4) (2, 1). The vertical line intersects the two points, which fails the vertical line test. This is an indication that the given relation is not a function.

8 0
2 years ago
Shaun had a collection of 840 matchbox vehicles. Of these vehicles, 15% are four-wheelers and 30% are convertibles. The rest are
lakkis [162]

Answer:

<h2>462</h2>

Step-by-step explanation:

15% of 840 is 126

30% of 840 is 252

 252

<u>+  126 </u>

<em> 378</em>

<em></em>

840 - 378 = <em>462</em>

or

--------------------------------------------------------------------------------------------------------------

55% of 840 is...

<em>462</em>

--------------------------------------------------------------------------------------------------------------

<em>Hope this helps! <3</em>

5 0
3 years ago
Read 2 more answers
Picture attached below. Please help.
Anuta_ua [19.1K]

There isnt a picturee but when you add another question I will hep you the best I can :)

4 0
3 years ago
Read 2 more answers
9.84615384615 in a fraction
choli [55]

Answer:

Step-by-step explanation:

128/13

3 0
3 years ago
Other questions:
  • What is the value of fraction 1 over 2x3 3.4y when x = 4 and y = 2? 12.8 14.8 25.8 38.8
    7·1 answer
  • Please help homework
    12·2 answers
  • Do lines that intersect have a solution ? &amp; how many
    11·1 answer
  • How do you find The area of an irregular shape
    5·1 answer
  • 5 weeks = how many days
    15·2 answers
  • Nancy Stone has a small company and has negotiated a special rate for rental cars when she and other employees take business tri
    7·1 answer
  • Calculate a 40% increase from 120.
    13·1 answer
  • Suppose that QRS is isosceles with base SQ . Suppose also that =m∠R=(2x+29)° and =m∠Q=(4x+9)° . Find the degree measure of each
    14·1 answer
  • which pair of terms has 4a as the greatest common factor ? 8a and 18a , 8b and 16a , 12a and 16ab , 20a and 27a
    8·1 answer
  • Using the same coordinate plane from question 1, What is the value of n? Explain how you determined the distance between P and Q
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!