Question

In: Computer Science

Hi. Are you familiar with deunions? The objective of the task is to undo the last...

Hi. Are you familiar with deunions? The objective of the task is to undo the last union operation in a weighted quick-union find implementation, of which I have partly written here (see below). I push the unions on a stack. The code is not finished though, and far from perfect. Can you point out my mistakes and figure out how to create the deunion-operation correctly?

Best regards,

Eirik

public class UFwithDeunion {

    private int[] id; //parent link (site indexed)
    private int[] sz; // size of component for roots (site indexed)
    private int count; //number of components
    Stack stack = new Stack();

    //maa bruke weighted-quick union for at det skal kjoere i ¨log N kjoeretid

    public UFwithDeunion(int n) {
        count = n;
        id = new int[n];
        for (int i = 0; i < n; i++)
            id[i] = i;
    }

    //set the total number of elements
    public static int setSize(int n){
        sz = new int[n];
        for (int i = 0; i < n; i++)
            sz[i] = 1;
    }

    //return an identifier for the set containing a
    public static int find(int a){ //follow links to find a root
        while (a != id[a])
            a = id[a]; //pop
        return a;


    //merge the set containing a with the set containing b
    public static void union(int a, int b){

        int i = find(a);
        int j = find(b);
        if (i == j) return;
        //make smaller root point to larger one
        if (sz[i] < sz[j]) {
            id[i] = j; sz[j] += sz[i];
            stack.push(id[i]);
        }
        else {
            id[j] = i; sz[i] += sz[j];
            stack.push(id[j]);
        }
    }

    // undo the last union operation
    public static void deUnion(){
        stack.pop();
        //sz--;
    }

    //Return the number of elements in the set containing a
    public static int elementsInSet(int a){
        int sum = 0;
        for (int i : id) {
            if (a == id[i])
                sum++;
        }
        return sum;
    }

Solutions

Expert Solution

The idea to solve this question is maintain a stack of user created class that stores the information of groups before the union
operation and push it onto the stack and when we need to do deunion operation, we pop this object and use its information to reset the group information to the state it was before the union operation. I have mentioned outside each function to the changes I made and new statements have explanation along with them. I have demo run this code with 10 elements and I have used find operation on each element before and after the deunion operation to show the difference and show the deunion was succesful and a screenshot has been attached. If you face any problem, let me know in the comments.

Code:

import java.util.*;
class UnionInfo //Created a new Class for storing info about the union operations we are doing
{
int l1, l2; //l1 and l2 are leaders of the two groups on which we applied union operation
int size1, size2;// We will store the size of both groups before the union operation
}
class UFwithDeunion { //Changed static methods into non static, private to public for easy access

int[] id; //parent link (site indexed)
int[] sz; // size of component for roots (site indexed)
int count; //number of components
Stack<UnionInfo> stack = new Stack<UnionInfo>(); //Created a stack with UnionInfo objects which store
//all information about union operations done

//maa bruke weighted-quick union for at det skal kjoere i ¨log N kjoeretid

public UFwithDeunion(int n) {//Called setSize function in the constructor itself,other things are same
count = n;
id = new int[n];
for (int i = 0; i < n; i++)
id[i] = i;
setSize(n);
}

//set the total number of elements
public void setSize(int n) { //No change
sz = new int[n];
for (int i = 0; i < n; i++)
sz[i] = 1;
}

//return an identifier for the set containing a
public int find(int a){ //follow links to find a root
while (a != id[a])
a = id[a]; //pop
return a;
}//No change in this function as well

//merge the set containing a with the set containing b
public void union(int a, int b)
{

int i = find(a);
int j = find(b);
if (i == j) return;
/*
After finding the group leaders, we will create a UnionInfo object and contain
information about the groups and their sizes before the union operation so they can be reverted
back using deUnion operation
*/
UnionInfo x=new UnionInfo();
//make smaller root point to larger one
if (sz[i] < sz[j]) {
  
x.l1=j; //Setting values for UnionInfo object
x.l2=i;
x.size1=sz[j];
x.size2=sz[i];
id[i] = j; sz[j] += sz[i]; //Unchanged union statement
}
else {
x.l1=i; //Setting values for UnionInfo object
x.l2=j;
x.size1=sz[i];
x.size2=sz[j];
id[j] = i; sz[i] += sz[j]; //Unchanged union statement
}
stack.push(x); //Now we push this Union operation info object to stack
}

// undo the last union operation
public void deUnion(){
/*
In this function, we will create a UnionInfo object
and assign the top element from the stack to it.
Now, we will assign the leaders contained in this UnionInfo object
to themselves and reset their size to their previous size given by size1 and
size2
*/
UnionInfo a=new UnionInfo();
a=stack.peek();
id[a.l1]=a.l1; //Setting the leader of 1st group leader to itself
id[a.l2]=a.l2; //Setting the leader of 2nd group leader to itself
sz[a.l1]=a.size1; //Resetting the size of both the groups to their original
sz[a.l2]=a.size2; //size stored in the UnionInfo stack object
  
stack.pop(); //Pop the top element as this union operation has been undoed
}

//Return the number of elements in the set containing a
public int elementsInSet(int a){ //Made changes so that this function works as inteded
int sum = 0;
for (int i : id) {
if (find(i) == find(a)) //Used find instead of using the id array as we need to compare the group leader
sum++;
}
return sum;
}
  
}


public class Main //Main function to test functionality of deUnion
{
   public static void main(String[] args) {
       UFwithDeunion obj=new UFwithDeunion(10); //Created object for Union operation with 10 element
obj.union(1,2); //Grouping 1 and 2
obj.union(2,3); //Grouping 2 and 3 and the group is now(1,2,3)
obj.union(4,5); //Grouping 4 and 5
obj.union(4,6); //Grouping 4 and 6 and the group is now (4,5,6)
obj.union(3,4); //Grouping 3 and 4 and the group is now (1,2,3,4,5,6)
for(int i=0;i<10;i++) //Loop to view the leader of the group the elements are contained in
{
System.out.println(i+" "+obj.find(i));
//Would display element from 1 to 6 have leader set to 1
}
obj.deUnion(); //Calling the deUnion operation
//Now the groups should be (1,2,3) and (4,5,6)
for(int i=0;i<10;i++)
{
System.out.println(i+" "+obj.find(i));
//Now we would see that the latest deUnion operation is succesful
// as only 1,2 and 3 have their leaders set to 1 and
//4,5 and 6 have their leader set to 4
}
   }
}

Output:


Related Solutions

Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str -- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents of another string...
Hi Tutor, nice to meet you. This is the task B of my (Entreprenuership and small...
Hi Tutor, nice to meet you. This is the task B of my (Entreprenuership and small business )assignment which is also divide into two parts. BExplore the similarities and differences between entrepreneurial ventures Part 1 -What are the Roles and characteristics of micro, small and medium-sized organisations? Please, help me answer this Part1 questions accurately with reference. Thanks Tutor.
Hi Tutor, nice to meet you. This is the task C of my (Entreprenuership and small...
Hi Tutor, nice to meet you. This is the task C of my (Entreprenuership and small business )assignment which is divided into several parts as well. 1 Definitions of creativity and innovation. 2 The main sources of generating business and entrepreneurial ideas. 3 How businesses protect intellectual property rights.
Task 1: Getting familiar with Unix commands Questions: How do you remove a directory and all...
Task 1: Getting familiar with Unix commands Questions: How do you remove a directory and all of its contents, including any subdirectories and files, without using rmdir repeatedly?         [5 Points] Go to your home directory (i.e. /home/) and enter the following command: ls -ali Explain the output. You need to clearly what each column specifies, what are the values in each column means. Also explain how it is different from: ls -li    [5 Points] How will you copy directory (not...
Hi, can you answer this question in more detail? Subject: Quality Management and Practices Task 1...
Hi, can you answer this question in more detail? Subject: Quality Management and Practices Task 1 - Prepare a Project Definition (approximately 500 - 1000 words). This is the “who, why, and what” part of the project. It defines all major aspects of the project and forms the basis for its planning and management: Topic: Western restaurant
Hi, I would be grateful for some helot with this Python problem. Your task is to...
Hi, I would be grateful for some helot with this Python problem. Your task is to implement the simple elevator in Python using classes. The default strategy is the simple "start at the bottom, go to the top, then go to the bottom". Can you write a better strategy, one that is more efficient? Description / Specification Create three classes: Building, Elevator, and Customer. Equip the building with an elevator. Ask user to customize the number of floors and the...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
Hi, I would like the numerical solution for this task so I can be able to...
Hi, I would like the numerical solution for this task so I can be able to model it in a kinetic modeling programme like Berkely Madonna (BM). This was the reason why I started my membership with Chegg Study but unfortunately, the solution to this problem was not available. I'm looking forward to hearing from you. you will find the task in de link below. With kind regards, Ahmed https://www.chegg.com/homework-help/comprehensive-problem-multiple-reactions-heat-effects-styren-chapter-12-problem-24qp-solution-9780132317160-exc in J.Snyder en B.Subramaniam, Chem. Eng. Sci., 49, 5585 (1994)....
Using the case study below or your own health issue with goal and objective, your task...
Using the case study below or your own health issue with goal and objective, your task is to develop three outputs and one indicator for each output, considering the means of verification and assumptions for your indicators through completing the table below Goal: Decrease the complications of Hyperglycemia Objective: To maintain the blood glucose level within normal range Output Indicators (Measure to verify to what extent the goal is fulfilled – include targets and baseline data where possible) Means of...
Java Program to Fully Parenthesize an expression. Hi guys. I have a small task which I...
Java Program to Fully Parenthesize an expression. Hi guys. I have a small task which I cannot find the solution for. It involves expressions with Infix I'll need a Java Program to convert an expression, eg. 3+6*(x-y)+x^2 to ((3 + (6 * (x-y))) + (x ^ 2)) Kindly assist. Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT