Algorithm Search⌘K

Proofs: Calkin-Wilf tree

These are the human proofs of Calkin-Wilf tree, at full length.

The Calkin-Wilf tree lists each positive rational once in lowest terms, and its operations are correct full

This proof shows: correctness, time.children, time.parent, time.next, time.at, time.index_of.

The tree TT has root 1/11/1, and the node (a,b)(a, b) has left child (a,a+b)(a, a + b) and right child (a+b,b)(a + b, b). Nodes are numbered in breadth-first order: the root is 11, and the children of node mm are 2m2m (left) and 2m+12m + 1 (right). We write v(n)v(n) for the pair at node nn. The pseudocode is that of the page.

Lemma 1 (values are positive and reduced). Every v(n)=(a,b)v(n) = (a, b) has a,b≥1a, b \ge 1 and gcd⁡(a,b)=1\gcd(a, b) = 1. Every node other than the root has a≠ba \ne b; left children have a<ba < b and right children a>ba > b.

Proof. By induction on nn. The root (1,1)(1, 1) is reduced. If (a,b)(a, b) is positive and reduced, then gcd⁡(a,a+b)=gcd⁡(a,b)=1\gcd(a, a + b) = \gcd(a, b) = 1 and gcd⁡(a+b,b)=gcd⁡(a,b)=1\gcd(a + b, b) = \gcd(a, b) = 1, and both children are positive. In the left child a<a+ba < a + b, and in the right child a+b>ba + b > b. □\square

Theorem 1 (every positive rational once). For each pair a,b≥1a, b \ge 1 with gcd⁡(a,b)=1\gcd(a, b) = 1 there is exactly one nn with v(n)=(a,b)v(n) = (a, b).

Proof. By strong induction on s=a+bs = a + b. If s=2s = 2, then (a,b)=(1,1)(a, b) = (1, 1). It is v(1)v(1), and by Lemma 1 no other node has a=ba = b. Let s≥3s \ge 3, so a≠ba \ne b (since a=ba = b and gcd⁡(a,b)=1\gcd(a, b) = 1 give a=b=1a = b = 1).

Existence. If a<ba < b, the pair (a,b−a)(a, b - a) is positive, reduced (as gcd⁡(a,b−a)=gcd⁡(a,b)\gcd(a, b - a) = \gcd(a, b)) and has sum b<sb < s. By induction it is v(m)v(m) for some mm, and its left child is (a,b)=v(2m)(a, b) = v(2m). If a>ba > b, the same argument with (a−b,b)(a - b, b) and the right child gives (a,b)=v(2m+1)(a, b) = v(2m + 1).

Uniqueness. Let v(n)=(a,b)v(n) = (a, b). Then n≠1n \ne 1 since (a,b)≠(1,1)(a, b) \ne (1, 1), so v(n)v(n) is a child of v(⌊n/2⌋)v(\lfloor n/2 \rfloor). By Lemma 1, it is a left child if a<ba < b and a right child if a>ba > b; so nn is even exactly when a<ba < b, and the parent v(⌊n/2⌋)v(\lfloor n/2 \rfloor) is (a,b−a)(a, b - a) or (a−b,b)(a - b, b), determined by (a,b)(a, b). By induction, that parent pair is v(m)v(m) for exactly one mm, so ⌊n/2⌋=m\lfloor n/2 \rfloor = m, and the parity of nn is fixed, which determines nn. □\square

Theorem 2 (fusc). Let f(0)=0f(0) = 0, f(1)=1f(1) = 1, f(2m)=f(m)f(2m) = f(m), f(2m+1)=f(m)+f(m+1)f(2m + 1) = f(m) + f(m + 1). Then v(n)=(f(n),f(n+1))v(n) = (f(n), f(n + 1)) for all n≥1n \ge 1.

Proof. By induction on nn. v(1)=(1,1)=(f(1),f(2))v(1) = (1, 1) = (f(1), f(2)). If v(m)=(f(m),f(m+1))v(m) = (f(m), f(m + 1)), then v(2m)=(f(m),f(m)+f(m+1))=(f(2m),f(2m+1))v(2m) = (f(m), f(m) + f(m + 1)) = (f(2m), f(2m + 1)) and v(2m+1)=(f(m)+f(m+1),f(m+1))=(f(2m+1),f(2m+2))v(2m + 1) = (f(m) + f(m + 1), f(m + 1)) = (f(2m + 1), f(2m + 2)). □\square

Theorem 3 (CW-CHILDREN and CW-PARENT). Line L1 returns the children by definition. For a node (a,b)(a, b), CW-PARENT returns NIL exactly at the root and the parent otherwise.

Proof. By Lemma 1, a=ba = b only at the root. If a<ba < b, the node is a left child of (a,b−a)(a, b - a), and if a>ba > b, a right child of (a−b,b)(a - b, b), as in the uniqueness part of Theorem 1. □\square

Theorem 4 (CW-AT). For n≥1n \ge 1, CW-AT(n)(n) returns v(n)v(n).

Proof. Write n=1c1c2⋯cdn = 1 c_1 c_2 \cdots c_d in binary, and let nj=1c1⋯cjn_j = 1 c_1 \cdots c_j, so n0=1n_0 = 1, nd=nn_d = n, and nj=2nj−1+cjn_j = 2 n_{j-1} + c_j. Invariant: after processing c1,…,cjc_1, \ldots, c_j, (a,b)=v(nj)(a, b) = v(n_j). It holds for j=0j = 0 by line L5. If it holds for j−1j - 1, then njn_j is the left child of nj−1n_{j-1} when cj=0c_j = 0 and the right child when cj=1c_j = 1, and line L7 replaces (a,b)(a, b) by that child. After the last digit, (a,b)=v(n)(a, b) = v(n). □\square

Theorem 5 (CW-INDEX). For a,b≥1a, b \ge 1, CW-INDEX(a,b)(a, b) returns the nn with v(n)=(a,b)v(n) = (a, b) if gcd⁡(a,b)=1\gcd(a, b) = 1, and reports an error otherwise.

Proof. Let (a0,b0)(a_0, b_0) be the input and g=gcd⁡(a0,b0)g = \gcd(a_0, b_0). The loop keeps the invariant

(I) a,b≥1a, b \ge 1, gcd⁡(a,b)=g\gcd(a, b) = g, and if g=1g = 1 then (a,b)=v(m)(a, b) = v(m) for the mm such that v(N)=(a0,b0)v(N) = (a_0, b_0) has the binary form N=m⋅2len+nN = m \cdot 2^{len} + n with 0≤n<2len0 \le n < 2^{len}; that is, nn holds the last lenlen digits of the path and mm is the ancestor lenlen levels up.

At the start n=len=0n = len = 0, (a,b)=(a0,b0)(a, b) = (a_0, b_0), and (I) holds with m=Nm = N.

Consider one pass with a>ba > b (line L11). Let q=⌈a/b⌉−1q = \lceil a/b \rceil - 1, so q≥1q \ge 1 and 0<a−qb≤b0 < a - qb \le b. For 0≤j<q0 \le j < q, the pair (a−jb,b)(a - jb, b) has first entry a−jb>ba - jb > b, so it is a right child and its parent is (a−(j+1)b,b)(a - (j + 1)b, b). So qq parent steps from v(m)v(m) pass through right children only, and reach (a−qb,b)=v(⌊m/2q⌋)(a - qb, b) = v(\lfloor m / 2^q \rfloor), with m mod 2q=2q−1m \bmod 2^q = 2^q - 1, the qq digits 11. Line L11 adds these digits above the lenlen digits already found, and line L13 increases lenlen by qq, so (I) holds again. The gcd is kept since gcd⁡(a−qb,b)=gcd⁡(a,b)\gcd(a - qb, b) = \gcd(a, b). The case a<ba < b of line L12 is symmetric, with left children and digits 00.

Termination. Each pass lowers a+ba + b by at least min⁡(a,b)≥1\min(a, b) \ge 1, so the loop stops, with a=ba = b. Then gcd⁡(a,b)=a=g\gcd(a, b) = a = g. If g≠1g \ne 1, line L14 reports the error, which is right by Theorem 1 and Lemma 1, since non-reduced pairs are not nodes. If g=1g = 1, then (a,b)=(1,1)=v(1)(a, b) = (1, 1) = v(1), so m=1m = 1 and N=2len+nN = 2^{len} + n, which line L15 returns. □\square

Theorem 6 (CW-NEXT, Newman's formula). For n≥1n \ge 1 let xn=f(n)/f(n+1)x_n = f(n)/f(n + 1) be the value of node nn. Then xn+1=1/(2⌊xn⌋−xn+1)x_{n+1} = 1/(2\lfloor x_n \rfloor - x_n + 1), and line L17 returns v(n+1)v(n + 1).

Proof. Let N(x)=1/(2⌊x⌋−x+1)N(x) = 1/(2\lfloor x \rfloor - x + 1); the denominator is positive because x−⌊x⌋<1x - \lfloor x \rfloor < 1. We use induction on nn. For n=1n = 1: N(1)=1/2=x2N(1) = 1/2 = x_2.

Case n=2mn = 2m even. Node nn is the left child of node mm; write xm=u/wx_m = u/w. Then xn=u/(u+w)<1x_n = u/(u + w) < 1, ⌊xn⌋=0\lfloor x_n \rfloor = 0, and N(xn)=1/(1−u/(u+w))=(u+w)/wN(x_n) = 1/(1 - u/(u + w)) = (u + w)/w, which is the right child of node mm, node 2m+1=n+12m + 1 = n + 1.

Case n=2m+1n = 2m + 1 odd, m≥1m \ge 1. Node nn is the right child of node mm, so xn=xm+1x_n = x_m + 1 and ⌊xn⌋=⌊xm⌋+1\lfloor x_n \rfloor = \lfloor x_m \rfloor + 1. Node n+1=2(m+1)n + 1 = 2(m + 1) is the left child of node m+1m + 1, and a left child of a node with value yy has value y/(1+y)=1/(1+1/y)y/(1 + y) = 1/(1 + 1/y). By induction, xm+1=N(xm)x_{m+1} = N(x_m), so 1/xm+1=2⌊xm⌋−xm+11/x_{m+1} = 2\lfloor x_m \rfloor - x_m + 1 and

xn+1=11+2⌊xm⌋−xm+1=12(⌊xn⌋−1)−(xn−1)+2=12⌊xn⌋−xn+1=N(xn).x_{n+1} = \frac{1}{1 + 2\lfloor x_m \rfloor - x_m + 1} = \frac{1}{2(\lfloor x_n \rfloor - 1) - (x_n - 1) + 2} = \frac{1}{2\lfloor x_n \rfloor - x_n + 1} = N(x_n).

Integers. For xn=a/bx_n = a/b in lowest terms and q=⌊a/b⌋q = \lfloor a/b \rfloor, N(a/b)=b/((2q+1)b−a)N(a/b) = b/((2q + 1)b - a), and (2q+1)b−a>(2q+1)b−(q+1)b=qb≥0(2q + 1)b - a > (2q + 1)b - (q + 1)b = qb \ge 0 because a<(q+1)ba < (q + 1)b. And gcd⁡(b,(2q+1)b−a)=gcd⁡(b,a)=1\gcd(b, (2q + 1)b - a) = \gcd(b, a) = 1, so line L17 returns the reduced pair, which by Theorem 1 is v(n+1)v(n + 1). □\square

Theorem 7 (depth and cost). Let a/b=[a0;a1,…,ak]a/b = [a_0; a_1, \ldots, a_k] be the regular continued fraction, with ak≥2a_k \ge 2 unless a/b=1a/b = 1. The loop of CW-INDEX runs at most k+1k + 1 times, and the depth of a/ba/b is a0+⋯+ak−1a_0 + \cdots + a_k - 1. Each of CW-CHILDREN, CW-PARENT and CW-NEXT uses O(1)O(1) arithmetic operations, and CW-AT(n)(n) uses ⌊lg⁡n⌋\lfloor \lg n \rfloor additions.

Proof. The values of qq in the passes are the non-zero quotients of the Euclidean algorithm on (a,b)(a, b) (only a0a_0 can be zero), except that when b∣ab \mid a (or a∣ba \mid b) the quotient is reduced by 11 and the pass ends at (b,b)(b, b) instead of (b,0)(b, 0). So the passes correspond to the division steps, at most k+1k + 1, and the sum of the qq is a0+⋯+ak−1a_0 + \cdots + a_k - 1: all quotients, with the last one reduced by 11. By Theorem 5 this sum is lenlen, the depth. The other counts are read from the pseudocode: CW-AT does one addition for each of the ⌊lg⁡n⌋\lfloor \lg n \rfloor digits after the leading one, and each other operation is a fixed number of additions, subtractions, comparisons and at most one division. ■\blacksquare

Back to Calkin-Wilf tree