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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question


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.


Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Types of trees
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education