Algorithm Search⌘K

Proofs: Tarjan's union-find bound

These are the human proofs of Tarjan's union-find bound, at full length.

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))O(m\,\alpha(n)) for mm operations of which nn are MAKE-SET. Tarjan's sharper bound O(m α(m,n))O(m\,\alpha(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 mm operations becomes a sequence of at most 3m3m operations MAKE-SET, FIND and LINK, and it is enough to prove the bound for these. MAKE-SET and LINK take O(1)O(1) time, and a FIND on a path of ss nodes takes O(s)O(s) time. Let cc be a constant such that these costs are at most cc, cc and cscs.

The number nn is the number of MAKE-SET operations in the whole sequence, and α(n)\alpha(n) is fixed for the proof. The functions AkA_k and α\alpha are those of the inverse Ackermann function page: A0(j)=j+1A_0(j) = j + 1, Ak(j)=Ak−1(j+1)(j)A_k(j) = A_{k-1}^{(j+1)}(j), and α(n)=min⁡{k:Ak(1)≥n}\alpha(n) = \min\{k : A_k(1) \ge n\}. We use that Ak(j)>jA_k(j) > j, that AkA_k is increasing in jj, and that Ak+1(j)>Ak(j)A_{k+1}(j) > A_k(j).

Rank facts. From the union by rank page: (i) x.rank<x.p.rankx.rank < x.p.rank for each non-root xx; (ii) the rank of a node changes only while it is a root, and never falls; (iii) each rank is at most ⌊lg⁡n⌋≤n−1\lfloor \lg n \rfloor \le n - 1. We also need:

(iv) For each node xx, the rank of x.px.p never falls over time. The parent of xx changes in two ways. A LINK makes a root xx a child of yy; from then on xx is a non-root. A FIND sets x.px.p to the root rr of the path, and r.rank≥r.rank \ge the old x.p.rankx.p.rank by (i) along the path. Otherwise x.px.p stays, and its rank does not fall by (ii).

Level and iterate. For a non-root xx with x.rank≥1x.rank \ge 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)}.\mathrm{level}(x) = \max\{k \ge 0 : x.p.rank \ge A_k(x.rank)\}, \qquad \mathrm{iter}(x) = \max\{i \ge 1 : x.p.rank \ge A_{\mathrm{level}(x)}^{(i)}(x.rank)\}.

Ranges. x.p.rank≥x.rank+1=A0(x.rank)x.p.rank \ge x.rank + 1 = A_0(x.rank) by (i), so the set for the level contains 0. Also Aα(n)(x.rank)≥Aα(n)(1)≥n>x.p.rankA_{\alpha(n)}(x.rank) \ge A_{\alpha(n)}(1) \ge n > x.p.rank by (iii), so no k≥α(n)k \ge \alpha(n) is in the set, and 0≤level(x)<α(n)0 \le \mathrm{level}(x) < \alpha(n). For the iterate, i=1i = 1 is in the set by the definition of the level. And Aℓ(x.rank+1)(x.rank)=Aℓ+1(x.rank)>x.p.rankA_{\ell}^{(x.rank + 1)}(x.rank) = A_{\ell + 1}(x.rank) > x.p.rank for ℓ=level(x)\ell = \mathrm{level}(x), by the maximality of the level; the iterates of a point under AℓA_\ell increase, so 1≤iter(x)≤x.rank1 \le \mathrm{iter}(x) \le x.rank.

Potential. After the qq-th operation, give each node xx the potential

ϕq(x)={α(n)⋅x.rankif x is a root or x.rank=0,(α(n)−level(x))⋅x.rank−iter(x)if x is a non-root and x.rank≥1,\phi_q(x) = \begin{cases} \alpha(n) \cdot x.rank & \text{if } x \text{ is a root or } x.rank = 0, \\ (\alpha(n) - \mathrm{level}(x)) \cdot x.rank - \mathrm{iter}(x) & \text{if } x \text{ is a non-root and } x.rank \ge 1, \end{cases}

and let Φq=c∑xϕq(x)\Phi_q = c \sum_x \phi_q(x), over the nodes made so far. A non-root of rank 0 has potential 0. At the start Φ0=0\Phi_0 = 0.

Lemma 1. 0≤ϕq(x)≤α(n)⋅x.rank0 \le \phi_q(x) \le \alpha(n) \cdot x.rank for each node xx and each qq.

Proof. The root case and the rank 0 case are clear. For a non-root with x.rank≥1x.rank \ge 1: level(x)≤α(n)−1\mathrm{level}(x) \le \alpha(n) - 1 and iter(x)≤x.rank\mathrm{iter}(x) \le x.rank give ϕq(x)≥x.rank−x.rank=0\phi_q(x) \ge x.rank - x.rank = 0. And level(x)≥0\mathrm{level}(x) \ge 0 and iter(x)≥1\mathrm{iter}(x) \ge 1 give ϕq(x)≤α(n)⋅x.rank−1\phi_q(x) \le \alpha(n) \cdot x.rank - 1. □\square

So Φq≥0=Φ0\Phi_q \ge 0 = \Phi_0 for each qq.

Lemma 2. Let xx be a non-root before the qq-th operation, which is a LINK or a FIND. Then ϕq(x)≤ϕq−1(x)\phi_q(x) \le \phi_{q-1}(x). If also x.rank≥1x.rank \ge 1 and the operation changes level(x)\mathrm{level}(x) or iter(x)\mathrm{iter}(x), then ϕq(x)≤ϕq−1(x)−1\phi_q(x) \le \phi_{q-1}(x) - 1.

Proof. The node xx stays a non-root, so its rank does not change, by (ii), and α(n)\alpha(n) is fixed. If x.rank=0x.rank = 0, both potentials are 0. Else, by (iv), x.p.rankx.p.rank does not fall, so level(x)\mathrm{level}(x) does not fall. If the level stays the same, iter(x)\mathrm{iter}(x) does not fall, for the same reason, and if it grows, ϕ(x)\phi(x) falls by at least 1. If the level grows by d≥1d \ge 1, the term (α(n)−level(x))⋅x.rank(\alpha(n) - \mathrm{level}(x)) \cdot x.rank falls by d⋅x.rank≥x.rankd \cdot x.rank \ge x.rank, while iter(x)\mathrm{iter}(x) can fall by at most x.rank−1x.rank - 1, from at most x.rankx.rank to at least 1. So ϕ(x)\phi(x) falls by at least 1. □\square

Amortized costs. The amortized cost of operation qq is its actual cost plus Φq−Φq−1\Phi_q - \Phi_{q-1}.

MAKE-SET(x)(x). The new node xx has potential 0, and no other node changes. The amortized cost is at most cc.

LINK(x,y)(x, y). Let xx become the child of yy (the other case is symmetric). The nodes whose potential can change are xx, yy and the children of yy. A child of yy is a non-root, so by Lemma 2 its potential does not grow. Before the LINK, xx was a root with ϕ(x)=α(n)⋅x.rank\phi(x) = \alpha(n) \cdot x.rank; after it, ϕ(x)≤α(n)⋅x.rank\phi(x) \le \alpha(n) \cdot x.rank by Lemma 1, because the rank of xx does not change. The root yy keeps its status, and its rank grows by at most 1, so ϕ(y)\phi(y) grows by at most α(n)\alpha(n). The amortized cost is at most c+c α(n)c + c\,\alpha(n).

FIND(x)(x). Let the path be x=x1,x2,…,xsx = x_1, x_2, \ldots, x_s, with xsx_s the root. The actual cost is at most cscs. Only the parents of x1,…,xs−2x_1, \ldots, x_{s-2} change, to xsx_s, 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)\max(0, s - \alpha(n) - 2) nodes of the path lose at least 1.

Call a node xjx_j with j<sj < s good if xj.rank≥1x_j.rank \ge 1 and there is an index j<t<sj < t < s with level(xt)=level(xj)\mathrm{level}(x_t) = \mathrm{level}(x_j), both levels taken before the FIND. Let xjx_j be good, with k=level(xj)k = \mathrm{level}(x_j) and i=iter(xj)i = \mathrm{iter}(x_j), and let xtx_t be the later node with level kk. Before the FIND:

xj.p.rank≥Ak(i)(xj.rank),xt.rank≥xj.p.rank,xt.p.rank≥Ak(xt.rank).x_j.p.rank \ge A_k^{(i)}(x_j.rank), \qquad x_t.rank \ge x_j.p.rank, \qquad x_t.p.rank \ge A_k(x_t.rank).

The first is the definition of the iterate, the second holds because xtx_t is xj+1=xj.px_{j+1} = x_j.p or a later node of the path and ranks increase along the path, and the third is the definition of the level of xtx_t. Since AkA_k is increasing,

xt.p.rank≥Ak(xt.rank)≥Ak(xj.p.rank)≥Ak(Ak(i)(xj.rank))=Ak(i+1)(xj.rank).x_t.p.rank \ge A_k(x_t.rank) \ge A_k(x_j.p.rank) \ge A_k\big(A_k^{(i)}(x_j.rank)\big) = A_k^{(i+1)}(x_j.rank).

After the FIND, xj.p=xsx_j.p = x_s, and xs.rank≥xt.p.rankx_s.rank \ge x_t.p.rank, because xt.px_t.p is xsx_s or a node before it on the path. So after the FIND, xj.p.rank≥Ak(i+1)(xj.rank)x_j.p.rank \ge A_k^{(i+1)}(x_j.rank). Then either level(xj)\mathrm{level}(x_j) has grown, or it is still kk and iter(xj)≥i+1\mathrm{iter}(x_j) \ge i + 1. By Lemma 2, ϕ(xj)\phi(x_j) falls by at least 1.

The nodes of the path that are not good are: the root xsx_s; the nodes of rank 0, of which there is at most one, x1x_1, because ranks increase strictly along the path; and, for each level kk from 0 to α(n)−1\alpha(n) - 1, the last node among x1,…,xs−1x_1, \ldots, x_{s-1} with level kk, if there is one. That is at most α(n)+2\alpha(n) + 2 nodes. So at least s−α(n)−2s - \alpha(n) - 2 nodes are good, and Φ\Phi falls by at least c (s−α(n)−2)c\,(s - \alpha(n) - 2) when this is positive. The amortized cost is at most cs−c (s−α(n)−2)=c (α(n)+2)cs - c\,(s - \alpha(n) - 2) = c\,(\alpha(n) + 2), and if s≤α(n)+2s \le \alpha(n) + 2 it is at most cs≤c (α(n)+2)cs \le c\,(\alpha(n) + 2).

Theorem. A sequence of mm operations MAKE-SET, UNION and FIND, of which nn are MAKE-SET, on a disjoint-set forest with union by rank and path compression takes O(m α(n))O(m\,\alpha(n)) time.

Proof. The sequence becomes at most 3m3m operations MAKE-SET, LINK and FIND, each of amortized cost at most c (α(n)+2)c\,(\alpha(n) + 2). The sum of the actual costs is the sum of the amortized costs minus Φend−Φ0\Phi_{\text{end}} - \Phi_0, and Φend≥0=Φ0\Phi_{\text{end}} \ge 0 = \Phi_0 by Lemma 1. So the total time is at most 3m⋅c (α(n)+2)=O(m α(n))3m \cdot c\,(\alpha(n) + 2) = O(m\,\alpha(n)). ■\blacksquare

Back to Tarjan's union-find bound