Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

public class UnionFind { public int[] id;

public UnionFind(int N) { throw new UnsupportedOperationException("need to implement"); }

/* method which recieve index and return repesentative of set * */ public int find(int i) { throw new UnsupportedOperationException("need to implement"); }

/* method which recieve two indexes and join * */ public void unite(int p, int q) { throw new UnsupportedOperationException("need to implement"); } /* method which return true if p and q in same set * */ public boolean isInSameSet(int p, int q) { throw new UnsupportedOperationException("need to implement"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions