Question: Needs to be implemented in Java A disjoint sets data structure can be implemented by trees. In this problem, the universal set is equal to

Needs to be implemented in Java

A disjoint sets data structure can be implemented by trees. In this problem, the universal set is equal to U = {0, 1, . . . , N 1}, and trees are stored in an array id of parents: the parent of element i is equal to id(i); if id(i) = i, this means that i is in the root of the tree and is thus a representative of the set.

You need to implement disjoint sets data structure (that is, Union-Find data structure). Follow the following instructions:

- In class UnionFind add a constructur UnionFind(int N), which makes N oneelement sets, that is, id(i) = i for each index i.

- In class UnionFind implement the following methods:

(i) find, which takes an integer i and returns the representative of the set which contains the element i. You also need to consider path compression, that is, the method makes the direct reference from each element traversed during the search to the root. 3

(ii) unite, which takes two integers p and q and makes the union between the set containing the element p and the set containing the element q. As for the representative of the new set it takes p.

(iii) isInSameSet, which takes two integer p and q and returns true, if the elements p and q are in the same set, and false otherwise.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To implement the UnionFind Disjoint Set Union data structure in Java we need to create a class UnionFind with the following functionalities as describ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!