Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

change the code from java to c++ just like how it is presented! public class WeightedQuickUnionUF { private int[] parent; // parent[i] = parent of

change the code from java to c++ just like how it is presented!

public class WeightedQuickUnionUF {

private int[] parent; // parent[i] = parent of i

private int[] size; // size[i] = number of elements in subtree rooted at i

private int count; // number of components

/**

* Initializes an empty union-find data structure with

* {@code n} elements {@code 0} through {@code n-1}.

* Initially, each elements is in its own set.

*

* @param n the number of elements

* @throws IllegalArgumentException if {@code n < 0}

*/

public WeightedQuickUnionUF(int n) {

count = n;

parent = new int[n];

size = new int[n];

for (int i = 0; i < n; i++) {

parent[i] = i;

size[i] = 1;

}

}

/**

* Returns the number of sets.

*

* @return the number of sets (between {@code 1} and {@code n})

*/

public int count() {

return count;

}

/**

* Returns the canonical element of the set containing element {@code p}.

*

* @param p an element

* @return the canonical element of the set containing {@code p}

* @throws IllegalArgumentException unless {@code 0 <= p < n}

*/

public int find(int p) {

validate(p);

while (p != parent[p])

p = parent[p];

return p;

}

/**

* Returns true if the two elements are in the same set.

*

* @param p one element

* @param q the other element

* @return {@code true} if {@code p} and {@code q} are in the same set;

* {@code false} otherwise

* @throws IllegalArgumentException unless

* both {@code 0 <= p < n} and {@code 0 <= q < n}

* @deprecated Replace with two calls to {@link #find(int)}.

*/

@Deprecated

public boolean connected(int p, int q) {

return find(p) == find(q);

}

// validate that p is a valid index

private void validate(int p) {

int n = parent.length;

if (p < 0 || p >= n) {

throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n-1));

}

}

/**

* Merges the set containing element {@code p} with the

* the set containing element {@code q}.

*

* @param p one element

* @param q the other element

* @throws IllegalArgumentException unless

* both {@code 0 <= p < n} and {@code 0 <= q < n}

*/

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 void displaySubsets(){

System.out.print("[");

for (int i = 0; i < size.length; i++)

System.out.print(size[i]+" ");

System.out.println("] ");

}

public static void main(String[] args) {

int n = 64;

WeightedQuickUnionUF uf= new WeightedQuickUnionUF(n);

for ( int k = 0; k < n-1; k++ ) uf.union(k, k+1);

uf.displaySubsets( ); // Test 3(a)

uf= new WeightedQuickUnionUF(n); // equivalent to reset...

for ( int k = 0; k < n-1; k++ ) uf.union(k, n-1);

uf.displaySubsets( ); // Test 3(b)

uf= new WeightedQuickUnionUF(n); // equivalent to reset...

for ( int k = 0; k < n-1; k += 4 )

{ uf.union(k, k+1); uf.union(k+2, k+3); uf.union(k, k+3); }

for ( int k = 0; k < n-1; k += 16 )

{ uf.union(k, k+4); uf.union(k, k+8); uf.union(k, k+12); }

uf.union(0, 16); uf.union(0, 32); uf.union(0, 48);

uf.displaySubsets( ); // Test 3(c)}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago