**Complete the code of the attached file disjoint-set_incomplete.cpp. #Code will be C++   ***disjoint-set_incomplete.cpp:   #include using namespace std; /// Final task: implement path compression and union by rank void make_set(int p[], int x){ p[x] = x; cout << "making a one-element set of "<

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 18SA
icon
Related questions
Topic Video
Question

**Complete the code of the attached file disjoint-set_incomplete.cpp.

#Code will be C++

 

***disjoint-set_incomplete.cpp:

 

#include<bits/stdc++.h>
using namespace std;

/// Final task: implement path compression and union by rank
void make_set(int p[], int x){
p[x] = x;
cout << "making a one-element set of "<<x<< endl;
}

int find_set(int p[], int x){
if (x!=p[x])
return find_set(p, p[x]);
return p[x];
}

void _union(int p[], int x, int y){
int a = find_set(p, x);
int b = find_set(p, y);
p[a] = b;
cout << "union "<<a << " and "<<b<< endl;
}

int main(){


int N;
/// take N input from user

/// create the parent array for a disjoint set of N elements
int p[N];
for (int i=0;i<N;i++){
make_set(p, i);
}

while(1){
/// take an integer "option" as input.
int option;


/// if "option" is 1, take another integer x as input,
/// check if x is an element of the disjoint set or not, and
/// if it is then print the root/representative-element of x
if (option==1){

}

/// if "option" is 2, take integers x and y as input,
/// check if x and y are elements of the disjoint set or not, and
/// if they are check if they belong to the same set or not
else if (option==2){


}

/// if "option" is 3, take integers x and y as input,
/// check if x and y are elements of the disjoint set or not, and
/// if they are then union them
else if (option==3){

}

else {
return 0;
}
}

}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning