SWE 596 FINAL ASSIGNMENT
Author: Mustafa Orkun Muti
Instructor: Prof. Dr. O. Haluk Bingöl
Topic: UNION FIND
Date: 10.02.2021

Union find problem consists of two problems
After the running the commands below:
Applications require the modulation of all forms of elements
Is there a path connecting cyan and pink elements?
Yes, but finding the path explicitly is a harder problem
Find query. Check if there are two items in the same component.
Union command. Replace modules comprising two objects by combining them
Running the union(2,5) command on the 3 disjointed components would lead to 2 disjointed components after the process.
Data structure
implement union(6,1)
Find => Firstly we check the ids of p and q to determine whether they are same or not
id[6] = 0; id[1] = 1. In this case, 6 and 1 are not connected
Union => To create the merge operation between components of p and q, we will change all of the entries whose id equals id[p] to id[q].
Data Structure
Find => We check if p and q has the same parent.
Union => In order to merge p and q, we will set id of p's parent to the q's parent id
Let's implement union(3,5)
Algorithm cost is high in terms of array reads and writes
Quick-find problems
Quick-union problem
Weighted quick-union
To avoid the drawbacks caused by quick-find and quick-union, we implement weighting strategy
Weighted quick-union
To avoid the drawbacks caused by quick-find and quick-union, we implement weighting strategy
Running time
Assumption: max length of tree is at max logN
algorithm | initialize | union | find |
quick-find | N | N | 1 |
quick-union | N | N | N |
weighted QU | N | logN | logN |
Weighted quick union algorithm dramatically improves the union-find operation in terms of time and space complexity and makes the problem approachable
algorithm | worst case time |
quick-find | MN |
quick-union | MN |
weighted QU | N + M*logN |