Union by rank with path compression runs in O(m alpha(n)) time full
This proof shows: statement.
This is the potential proof of CLRS, Section 19.4. We prove the bound O(mα(n)) for m operations of which n are MAKE-SET. Tarjan's sharper bound O(mα(m,n)) needs a finer argument, which is in his 1975 paper and in Tarjan and van Leeuwen (1984).
Setup. The operations are MAKE-SET, FIND and UNION on a disjoint-set forest with union by rank and path compression. A UNION is two FINDs and one LINK of two different roots (or nothing, when the roots are equal). So a sequence of m operations becomes a sequence of at most 3m operations MAKE-SET, FIND and LINK, and it is enough to prove the bound for these. MAKE-SET and LINK take O(1) time, and a FIND on a path of s nodes takes O(s) time. Let c be a constant such that these costs are at most c, c and cs.
The number n is the number of MAKE-SET operations in the whole sequence, and α(n) is fixed for the proof. The functions Ak and α are those of the inverse Ackermann function page: A0(j)=j+1, Ak(j)=Ak−1(j+1)(j), and α(n)=min{k:Ak(1)≥n}. We use that Ak(j)>j, that Ak is increasing in j, and that Ak+1(j)>Ak(j).
Rank facts. From the union by rank page: (i) x.rank<x.p.rank for each non-root x; (ii) the rank of a node changes only while it is a root, and never falls; (iii) each rank is at most ⌊lgn⌋≤n−1. We also need:
(iv) For each node x, the rank of x.p never falls over time. The parent of x changes in two ways. A LINK makes a root x a child of y; from then on x is a non-root. A FIND sets x.p to the root r of the path, and r.rank≥ the old x.p.rank by (i) along the path. Otherwise x.p stays, and its rank does not fall by (ii).
Level and iterate. For a non-root x with x.rank≥1, define
level(x)=max{k≥0:x.p.rank≥Ak(x.rank)},iter(x)=max{i≥1:x.p.rank≥Alevel(x)(i)(x.rank)}. Ranges. x.p.rank≥x.rank+1=A0(x.rank) by (i), so the set for the level contains 0. Also Aα(n)(x.rank)≥Aα(n)(1)≥n>x.p.rank by (iii), so no k≥α(n) is in the set, and 0≤level(x)<α(n). For the iterate, i=1 is in the set by the definition of the level. And Aℓ(x.rank+1)(x.rank)=Aℓ+1(x.rank)>x.p.rank for ℓ=level(x), by the maximality of the level; the iterates of a point under Aℓ increase, so 1≤iter(x)≤x.rank.
Potential. After the q-th operation, give each node x the potential
ϕq(x)={α(n)⋅x.rank(α(n)−level(x))⋅x.rank−iter(x)if x is a root or x.rank=0,if x is a non-root and x.rank≥1, and let Φq=c∑xϕq(x), over the nodes made so far. A non-root of rank 0 has potential 0. At the start Φ0=0.
Lemma 1. 0≤ϕq(x)≤α(n)⋅x.rank for each node x and each q.
Proof. The root case and the rank 0 case are clear. For a non-root with x.rank≥1: level(x)≤α(n)−1 and iter(x)≤x.rank give ϕq(x)≥x.rank−x.rank=0. And level(x)≥0 and iter(x)≥1 give ϕq(x)≤α(n)⋅x.rank−1. □
So Φq≥0=Φ0 for each q.
Lemma 2. Let x be a non-root before the q-th operation, which is a LINK or a FIND. Then ϕq(x)≤ϕq−1(x). If also x.rank≥1 and the operation changes level(x) or iter(x), then ϕq(x)≤ϕq−1(x)−1.
Proof. The node x stays a non-root, so its rank does not change, by (ii), and α(n) is fixed. If x.rank=0, both potentials are 0. Else, by (iv), x.p.rank does not fall, so level(x) does not fall. If the level stays the same, iter(x) does not fall, for the same reason, and if it grows, ϕ(x) falls by at least 1. If the level grows by d≥1, the term (α(n)−level(x))⋅x.rank falls by d⋅x.rank≥x.rank, while iter(x) can fall by at most x.rank−1, from at most x.rank to at least 1. So ϕ(x) falls by at least 1. □
Amortized costs. The amortized cost of operation q is its actual cost plus Φq−Φq−1.
MAKE-SET(x). The new node x has potential 0, and no other node changes. The amortized cost is at most c.
LINK(x,y). Let x become the child of y (the other case is symmetric). The nodes whose potential can change are x, y and the children of y. A child of y is a non-root, so by Lemma 2 its potential does not grow. Before the LINK, x was a root with ϕ(x)=α(n)⋅x.rank; after it, ϕ(x)≤α(n)⋅x.rank by Lemma 1, because the rank of x does not change. The root y keeps its status, and its rank grows by at most 1, so ϕ(y) grows by at most α(n). The amortized cost is at most c+cα(n).
FIND(x). Let the path be x=x1,x2,…,xs, with xs the root. The actual cost is at most cs. Only the parents of x1,…,xs−2 change, to xs, so the nodes off the path keep their parents, their parents keep their ranks, and their potentials do not change. The root keeps its potential. By Lemma 2, no node of the path gains potential. We show that at least max(0,s−α(n)−2) nodes of the path lose at least 1.
Call a node xj with j<s good if xj.rank≥1 and there is an index j<t<s with level(xt)=level(xj), both levels taken before the FIND. Let xj be good, with k=level(xj) and i=iter(xj), and let xt be the later node with level k. Before the FIND:
xj.p.rank≥Ak(i)(xj.rank),xt.rank≥xj.p.rank,xt.p.rank≥Ak(xt.rank). The first is the definition of the iterate, the second holds because xt is xj+1=xj.p or a later node of the path and ranks increase along the path, and the third is the definition of the level of xt. Since Ak is increasing,
xt.p.rank≥Ak(xt.rank)≥Ak(xj.p.rank)≥Ak(Ak(i)(xj.rank))=Ak(i+1)(xj.rank). After the FIND, xj.p=xs, and xs.rank≥xt.p.rank, because xt.p is xs or a node before it on the path. So after the FIND, xj.p.rank≥Ak(i+1)(xj.rank). Then either level(xj) has grown, or it is still k and iter(xj)≥i+1. By Lemma 2, ϕ(xj) falls by at least 1.
The nodes of the path that are not good are: the root xs; the nodes of rank 0, of which there is at most one, x1, because ranks increase strictly along the path; and, for each level k from 0 to α(n)−1, the last node among x1,…,xs−1 with level k, if there is one. That is at most α(n)+2 nodes. So at least s−α(n)−2 nodes are good, and Φ falls by at least c(s−α(n)−2) when this is positive. The amortized cost is at most cs−c(s−α(n)−2)=c(α(n)+2), and if s≤α(n)+2 it is at most cs≤c(α(n)+2).
Theorem. A sequence of m operations MAKE-SET, UNION and FIND, of which n are MAKE-SET, on a disjoint-set forest with union by rank and path compression takes O(mα(n)) time.
Proof. The sequence becomes at most 3m operations MAKE-SET, LINK and FIND, each of amortized cost at most c(α(n)+2). The sum of the actual costs is the sum of the amortized costs minus Φend−Φ0, and Φend≥0=Φ0 by Lemma 1. So the total time is at most 3m⋅c(α(n)+2)=O(mα(n)). ■