UNION-FIND
Union find problem consists of two problems
- Union => Connecting the given two elements by edge
- Find => Finding out if given two elements are somehow connected to each other
- connect(4, 3)
- connect(3, 8)
- connect(6, 5)
- connect(9, 4)
- connect(2, 1)
- isConnected(8, 9) // true
- isConnected(5, 7) // false
- connect(5, 0)
- connect(7, 2)
- connect(6, 1)
- connect(1, 0)
- isConnected(5, 7) // true
After the running the commands below:
- connect(4, 3)
- connect(3, 8)
- connect(6, 5)
- connect(9, 4)
- connect(2, 1)
- isConnected(8, 9) // true
- isConnected(5, 7) // false
- connect(5, 0)
- connect(7, 2)
- connect(6, 1)
- connect(1, 0)
- isConnected(5, 7) // true
Applications require the modulation of all forms of elements
- Pixels in a digital picture
- Computers on a network
- Friends on a web network
- Transistors on a computer chip
- Elements of a mathematical collection
- Names of variables in the Fortran program
- Metallic sites in composite structures
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
- Integer array id[] of length N
- Interpretation: we can say that p and q are connected if their ids are same
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
- We will create an array which has the length N, in this case we can say that parent[i] is the node which parent if o in our tree.
- Interpretation => we will create a set consisting of all elements in the tree
- Root of i is id[id[id[...]
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
- Array accesses are too high
- It's hard to keep the tree structure flat
Quick-union problem
- There is possibility of tree to get very tall
- Find operations is costly caused by lots of array accesses
Weighted quick-union
To avoid the drawbacks caused by quick-find and quick-union, we implement weighting strategy
- Update quick-union to get shorter trees
- Save the size of each tree
- Parent of the smaller tree will be connected to the parent of larger tree
Weighted quick-union
To avoid the drawbacks caused by quick-find and quick-union, we implement weighting strategy
- Update quick-union to get shorter trees
- Save the size of each tree
- Parent of the smaller tree will be connected to the parent of larger tree
Running time
- Find => the time to find an element increases with the depth of tree
- Union => Merging two nodes takes constant 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 |
- Sedgewick, R., Wayne, K. (2011). Algorithms, 4th Edition.. Addison-Wesley. ISBN: 978-0-321-57351-3
- https://cse.taylor.edu/~jdenning/classes/cos265/slides/01_UnionFind.html
- https://www.cs.cmu.edu/~avrim/451f13/lectures/lect0912.pdf