Question: 1 Instructions Implement two algorithms 1 . UNION - FIND: You must implement this data structure where UNION operation is done by rank 2 .
Instructions
Implement two algorithms
UNIONFIND: You must implement this data structure where
UNION operation is done by rank
UNIONFINDWITHPATHCOMPRESSION: You must implement this data structure where
UNION operation is done by rank and
FINDSET is implemented with path compression
Test Cases
Each test case will be input in the form of a text file
Input format
First line n Number of elements
Number of UNION Operations will be n
Second line m Number of FINDSET Operations
Next m n lines will either be a
Union operation Uxy, where x y n
Find operation Fx where x n
Output format
m lines for each FINDSET operation, where each line outputs the rootrepresentative
of the set to which x belongs
Sample Input
F
U
F
U
F
F
F
U
F
Sample Output
Details
Uxy should merge the two sets containing x and y
Definition of Rank
For union by rank, a node stores its rank which is an upper bound for its height.
When a node is initialized, its rank is set to zero.
To merge trees with roots x and y first compare their ranks.
If the ranks are different, then the larger rank tree becomes the parent, and the
ranks of x and y do not change.
Tie Break
Let x in T y in T
If rankT rankT root of T must be made the root of the resultant
merged tree
Fx should output the root of the tree to which x belongs in the output file
Test Case Execution
The input file name will be given as a command line argument with the option i or
input See below for example
Assume the file exists in the current directory where the program is located
Output file must be named output.txt See below for example
Output file must be created in the current directory where the program is located
See the following commands for python but could be extrapolated for any other language
$ python unionf i n d py i t e s t t x t
$ python unionf i n d py i n p u t t e s t t x t
In this case, the output file must be created in the current directory with the name
test output.txt
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
