At last, I have finally made up my mind to write my first blog post that truly explains an algorithm!
Let’s applaud and celebrate first~
But this definitely won’t be written well in one go~ hahaha, I’ll keep adding to it slowly
Why the Union-Find algorithm exists (its significance)
In some set application problems with N elements, we usually start by making each element form a single-element set, then merge the sets containing elements that belong to the same group in a certain order, and during this process we need to repeatedly find which set an element is in. This type of problem has appeared repeatedly in international and domestic informatics contest problems in recent years. Its characteristic is that it seems not complicated, but the amount of data is huge. If described with ordinary data structures, the space required is often too large for a computer to bear; even if it barely fits in memory, the running time complexity is extremely high, and it is simply impossible to compute the required result within the contest time limit (1–3 seconds). It can only be described with Union-Find.
Union-Find is a tree-type data structure used to handle merging and query problems of disjoint sets (Disjoint Sets).
That is, it describes the connectivity relationships among some sets
Operations of the Union-Find algorithm (what it can do)
Initialization
Initialize the set that each point belongs to as itself.
Generally, this step only needs to be executed once each time this data structure is used, and regardless of implementation, the time complexity is O(N).
Find
Find the set an element belongs to, i.e., the root node.
Merge
Merge the sets containing two elements into one set.
Generally, before merging, you should first determine whether the two elements belong to the same set, which can be done using the “Find” operation above.
Check
Determine whether two elements are in the same set
Understanding the meaning of the operations
By analyzing Union-Find operations, you can better understand the idea behind Union-Find
That is, when you encounter problems that require these operations, you can think in terms of Union-Find
Example problem for Union-Find
Simplest example:
If a family is too large, it is indeed not easy to determine whether two people are relatives. Given a kinship graph, determine whether any two given people are relatives. Rule: if x and y are relatives, and y and z are relatives, then x and z are also relatives. If x and y are relatives, then all relatives of x are also relatives of y, and all relatives of y are also relatives of x.
Based on the given correspondence, determine whether the two given people are relatives
At this point, this involves
Merging sets (merging sets with kinship relationships)
Finding sets (finding whether the two people are in the same set)
Detailed solution process
Union-Find_Baidu Baike
Points to note for Union-Find
Degeneration of tree data structures
If the rules for inserting nodes are not controlled
it easily leads to tree degeneration
For example, in a binary search tree, insert nodes in the order 1-2-3-4-5
the height of the tree becomes n, and then all operations require O(n) time to complete
So similarly, in Union-Find, to avoid degeneration
you can perform the following operations
- For each tree, record the height of this tree
- When merging, if the heights of the two trees are different, connect an edge from the one with smaller height to the one with larger height
Path compression
For each node
- Once you have walked upward to the root node, change the edge from this node to its parent to connect directly to the root
- During a query, all nodes passed while going upward are changed to connect directly to the root
(There is no need to modify the height rank)
The height record rank[m] is the height of the tree rooted at m
References:
《Challenge Programming Contest》
Union-Find - Baidu Baike