In this task, a disjoint sets data structure (that is, Union-Find data structure) should be implemented. 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. 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. (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 the representative of the set containing 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.
In this task, a disjoint sets data structure (that is, Union-Find data structure) should be
implemented. 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. 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.
(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 the representative of the set containing 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
Solved in 4 steps with 2 images