Question
I need this code to be converted from java to c++ please! The code is supposed to be converted exactly how its given, thank you!
I need this code to be converted from java to c++ please! The code is supposed to be converted exactly how its given, thank you!
public class QuickFindUF {
private int[] id; // id[i] = component identifier of 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 QuickFindUF(int n) {
count = n;
id = new int[n];
for (int i = 0; i < n; i++)
id[i] = i;
}
/**
* 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);
return id[p];
}
// validate that p is a valid index
private void validate(int p) {
int n = id.length;
if (p < 0 || p >= n) {
throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n-1));
}
}
/**
* 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) {
validate(p);
validate(q);
return id[p] == id[q];
}
/**
* 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) {
validate(p);
validate(q);
int pID = id[p]; // needed for correctness
int qID = id[q]; // to reduce the number of array accesses
// p and q are already in the same component
if (pID == qID) return;
for (int i = 0; i < id.length; i++)
if (id[i] == pID) id[i] = qID;
count--;
}
public void displaySubsets(){
System.out.print("[");
for (int i = 0; i < id.length; i++)
System.out.print(id[i]+" ");
System.out.println("] ");
}
/*
* replacement main( ) for QuickFindUF.java
* Given a set of n = 64 objects
* Test QuickFindUFin three different ways and print the results
* Test 1(a): u(0, 1), u(1, 2), u(2, 3) ... u(62, 63)
* Test 1(b): u(0, 63), u(1, 63), u(2, 63) ... u(62, 63)
* Test 1(c): Merge into 16 subsets each of size 4, then 4 subsets each of size 16,
* then one subset of all 64 elements
*/
public static void main(String[] args) {
int n = 64;
QuickFindUF uf = new QuickFindUF(n);
for ( int k = 0; k < n-1; k++ ) uf.union(k, k+1);
uf.displaySubsets( ); // Test 1(a)
uf = new QuickFindUF(n); // equivalent to reset, old gets garbage collected
for ( int k = 0; k < n-1; k++ ) uf.union(k, n-1);
uf.displaySubsets( ); // Test 1(b)
uf = new QuickFindUF(n); // equivalent to reset, old gets garbage collected
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 1(c)}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started