Question

Using java Create a DisjointSetADT class with “Union” and “Find” operations. In the union operation consider...

Using java Create a DisjointSetADT class with “Union” and “Find” operations. In the union operation consider the weight of the parents and make the heavier set as the parent set of the lighter set

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class DisjointSetADT {
    int[] rank, parent;
    int n;
    // Constructor
    public DisjointUnionSets(int n)
    {
        rank = new int[n];
        parent = new int[n];
        this.n = n;
        makeSet();
    }
    void makeSet() {
        for (int i = 0; i < n; i++) {
            parent[i] = i;
        }
    }
    // Returns representative of x's set
    int find(int x) {
        // Finds the representative of the set
        // that x is an element of
        if (parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }
    // Unites the set that includes x and the set
    // that includes x
    void union(int x, int y) {
        // Find representatives of two sets
        int xRoot = find(x), yRoot = find(y);
        // Elements are in the same set, no need
        // to unite anything.
        if (xRoot == yRoot)
            return;
        if (rank[xRoot] < rank[yRoot])
            parent[xRoot] = yRoot;
        // Else if y's rank is less than x's rank
        else if (rank[yRoot] < rank[xRoot])
            parent[yRoot] = xRoot;
        else // if ranks are the same
        {
            parent[yRoot] = xRoot;
            rank[xRoot] = rank[xRoot] + 1;
        }
    }
}

This is only the class of disjoint set class, I have wriiten whatever Ih ave done. I hope it helps you. Thank you

Add a comment
Know the answer?
Add Answer to:
Using java Create a DisjointSetADT class with “Union” and “Find” operations. In the union operation consider...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this problem, we would like to show the amortized time of a union operation when...

    In this problem, we would like to show the amortized time of a union operation when union by-weight on linked-lists is used is Ω(log n). For that, we need to come up with a sequence of Θ(n) operations for which the amortized cost per operation is Ω(log n). We start with make-set(xi) for i ∈ {1, 2, . . . n} where n is a power of 2. Provide a consequent sequence of Θ(n) union operations so that the total...

  • create a class in Java for a Plane the class will include String Data Fields for...

    create a class in Java for a Plane the class will include String Data Fields for the make, model, and year. (as well as mutators and accessors) Defined constructor and copy constructor. there must be a toString method as well as a copy method. runner program the program will create an array for Plane class. The size of the array will be set to user input. then the user will input the information into each plane object in the array....

  • Using the IST Linux system create the following Java command line inheritance application Lab4. Create the...

    Using the IST Linux system create the following Java command line inheritance application Lab4. Create the following project Java files: Point.java, Shape.java, Circle.java, Triangle.java, Rectangle.java, and Lab4.java Point.java will contain two coordinates x and y. This will be reused to create point objects to draw all the shapes. Shape.java will be the parent class and will contain a Point type. Circle.java, Triangle.java, Rectangle.java will all be children of Shapes.java class Each class (Circle, Triangle, Rectangle) will contain the private class...

  • Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet...

    Using Java programming language, build a class called IntegerSet. Instructions - Create class IntegerSet An IntegerSet object holds integers in the range 0-100 Represented by an array of booleans, such that array element a[i] is set to true if integer i is in the set, and false otherwise Create these constructors and methods for the class IntegerSet() public IntegerSet union(IntegerSet iSet) public IntegerSet intersection(IntegerSet iSet) public IntegerSet insertElement(int data) public IntegerSet deleteElement(int data) public boolean isEqualTo(IntegerSet iSet) public String toString()...

  • Write a program to create a set operation calculator applying object oriented programming principles discussed in...

    Write a program to create a set operation calculator applying object oriented programming principles discussed in the class so far. The set operation calculator need to be enclosed inside one class with different methods responsible for performing the different operations listed below. When the constructor (def __init__() ) is called during object instantiation, two separate objects of this class need to be instantiated two sets S1 and S2. These two set objects can be instantiated from two python lists taken...

  • The question exercises the union, intersection, and the intersection operations of two set of strings (names)....

    The question exercises the union, intersection, and the intersection operations of two set of strings (names). For this type of problem, you need to preserve the original sets from being modified by some of the set method. You can used the clone( ) method to copy the sets or simply create another set using the same array contents as the original. You can find an example of the clone() for the LinkedHashSet set in   the provided “skeleton” program (assign8_Q3.java) code...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • Matches based on preferred criteria (Matching application java based) Create a PersonProfile java class with the...

    Matches based on preferred criteria (Matching application java based) Create a PersonProfile java class with the following attributes firstName | LastName | Nickname | age | genre |weight | height | picture | Preferences Create a preferences java class with the following attributes: minAge | maxAge | genre | minWeight | maxWeight | minHeight | maxHeight Create a StackLinkedListPerson class (P.S. The object we use in the node is a person) {Node(PersonProfile)}) StackedLinkedList Male StackedLinkedList Female Create a MatchesProfile class...

  • JAVA Programming MagicSet. Write a class that represents a set. Recall that a set has the...

    JAVA Programming MagicSet. Write a class that represents a set. Recall that a set has the following properties it does not contain duplicates and order is not important. If you are unable to make a class that can use any type (generics), make your set handle whole numbers. You must implement your set using an array. The amount of storage used should grow and shrink as needed: it should be halved if the array is 25% full and doubled when...

  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT