This section is reproduced and adapted from Time Complexity — A Brief Discussion of Potential Analysis, with the original author's permission.
Definitions
The Ackermann Function
We first define . To give this definition, we first define .
Define as follows:
That is, the Ackermann function.
Here, denotes applying to consecutively times; that is, and .
Now define as the smallest integer such that . Note that we previously described it as ; in any case, they grow so slowly that their values do not exceed 4.
Basic Definitions
Every node has a rank. Here rank does not mean the number of nodes, but the depth. The initial rank of a node is 0. During a merge, if two nodes have different ranks, merge the lower-rank node into the higher-rank node without updating the higher-rank node's rank. Otherwise, randomly merge one node into the other and increase the root's rank by 1. The rank of the root gives the height of the tree. Let the rank of x be , and similarly let the parent of x be . We always have .
To define the potential function, we first need to define an auxiliary function . Let . When , also define an auxiliary function . The covered by these definitions always satisfies and this is not the root of a tree.
The definitions above may be a little dizzying. Let us sort them out. For an and , if , we can always find a pair such that , while and, under this premise, . describes the maximum iteration level of , while describes the maximum number of iterations at that maximum level.
For these two functions, always increases or remains unchanged as operations proceed. If does not increase, also only increases or remains unchanged. They always satisfy the following two inequalities:
Considering the definitions of , , and , these facts are easy to prove; we leave them to the reader as practice with the definitions.
Define the potential function , where denotes an entire disjoint-set union and is a node in the disjoint-set union. Define as follows:
We can now prove that the amortized time complexity is through the change in potential caused by operations. Note that the operation considered here guarantees that and are both roots of trees, so it does not need to additionally execute and .
It can be seen that the potential is always nonnegative. Also, at the beginning, the potential of the disjoint-set union is .
Proof
The union(x,y) Operation
Its cost is , so we consider the change in potential that it causes.
Here, assume , meaning that is attached to . Thus, the only nodes whose potential increases are (which changes from a tree root to a non-root), (whose rank may increase), and the children of before the operation (whose parent's rank may increase). We first prove that the potential of a child of before the operation cannot increase, and if it decreases, it decreases by at least .
Let the potential of before the operation be and after the operation be . Here can be any non-root node with , and the operation can be any operation, including the find operation below. We discuss four cases.
- and do not increase. Clearly, .
- increases while does not. Here increases by at least one, so ; the potential decreases by at least 1.
- increases, while may decrease. Since , can decrease by at most , while increases by at least . By the definition , we obtain .
- Any other case. Since does not change and does not decrease, this case cannot occur.
Therefore, the only nodes whose potential can increase are and . When changes from a tree root to a non-root, if , then always. Otherwise, we always have . Hence, .
Thus, the only point whose potential may increase is . The potential of can increase by at most . Therefore, the amortized time complexity of the operation is .
The find(a) Operation
If the search path contains nodes, its search time complexity is clearly . If the find operation increases the potential of no node and decreases the potential of at least nodes by at least , then it can be proved that the amortized time complexity of is . To avoid confusion, we use as the parameter here, while every that appears refers generically to a node in a disjoint-set union.
First, prove that no node's potential increases. It is obvious: as proved above, the potential of every non-root node does not increase, and the rank of a root does not change, so no node's potential increases.
Next, prove that the potential of at least nodes decreases by at least . We proved above that if or changes, their potential decreases by at least . Thus, it is enough to prove that or changes for at least nodes.
Recall the definition of the potential of a non-root node, , where and are the maximum values such that .
Therefore, if represents the root of the tree containing , it is enough to prove that . By the definition of , .
We may use to represent and to represent to avoid making the formulas too long. In this notation, .
When you reach this point, you may feel like “what on earth is this?”. This means that you may need to read it several more times, or skip some of the content and return later.
Here, we need an outer , which means that we may need to find another point . Let be the point after on the search path that satisfies ; here, “after the search path” is equivalent to “an ancestor of ”. Clearly, not every has such a . It is easy to prove that there are at most values of without such a , because only the last for each , along with and , has no such .
Let us emphasize once again that refers to the parent of before path compression; after path compression, the parent of is always denoted by . For every for which exists, we have . At the same time, we have . Since , use as shorthand for both; that is, . We need to construct one , so we can ignore the value of and directly use the weakened form .
If we combine the inequalities, something magical happens. We find that . In other words, to iterate from to , we can iterate at least times without exceeding .
Clearly, , and does not change during path compression. Therefore, we obtain . In other words, the value of increases by at least 1; if does not increase, then must increase.
Therefore, decreases by at least 1. Since there are at least such nodes, ultimately decreases by at least , and the amortized time complexity is therefore .
Why Disjoint-Set Union Can Be Broken
This question is asking what properties are destroyed if we do not use union by rank, causing the time complexity of disjoint-set union to no longer be guaranteed to be .
If, during merging, the node with larger is merged into the node with smaller , we set the -smaller node's value to the other node's value plus one. This guarantees , so we avoid a situation where the properties fail in a way resembling a floor covered in compile errors.
Clearly, in this case, what we break is the statement in the function that “the potential of y can increase by at most ”.
There is a structure that can reduce the time complexity of path-compressed disjoint-set union to , defined as follows:
A binomial tree (which is actually somewhat different from an ordinary binomial tree), where j is a constant and consists of a with a added as a child of its root.
The boundary condition is that through are each a single point.
Let . Here we have (proof omitted). In each round of operations, we attach it to a single-node tree and then query the bottom nodes. In other words, when we attach it to the single node, the potential of the single node increases by . When , , and , the increase in potential is:
After transforming this and removing all floor symbols, we obtain an increase in potential of . Over m operations, this is .
About Union by Size
Since union by rank is harder to write than union by size, many experts choose to use union by size when implementing disjoint-set union. Specifically, maintain a for each root, and each time merge the smaller into the larger one.
So, can union by size be broken?
First, this can be explained from the properties of rank used in the proof. If can replace , then union by size can be used. To summarize, the properties involving rank in the proof are the following three:
- At most one node's rank increases on each merge, and it increases by at most 1.
- We always have .
- A node's rank never decreases.
The second and third properties are clearly satisfied by , but the first is not: if is merged into , then increases by as much as .
Therefore, we can consider using in place of .
For the first property, since a node's can increase by at most a factor of two, increases by at most 1. The conclusion for the second and third properties is fairly obvious, so their proofs are omitted.
Thus, if you do not want to implement union by rank, just use union by size; the time complexity remains .