Algorithm Search⌘K

Proofs: Stern-Brocot tree

These are the human proofs of Stern-Brocot tree, at full length.

The Stern-Brocot tree holds each positive rational once, and its operations are correct full

This proof shows: correctness.

A node is a quadruple (a,b,c,d)(a, b, c, d) of nonnegative integers. The root is (0,1,1,0)(0, 1, 1, 0), the left child of (a,b,c,d)(a, b, c, d) is (a,b,a+c,b+d)(a, b, a + c, b + d) and the right child is (a+c,b+d,c,d)(a + c, b + d, c, d). The value of the node is (a+c)/(b+d)(a + c)/(b + d), and we read a/ba/b and c/dc/d as its left bound and right bound, with 1/0=+∞1/0 = +\infty. We write σ=a+b+c+d\sigma = a + b + c + d.

Lemma 1 (invariant). Every node satisfies bc−ad=1bc - ad = 1, b≥1b \ge 1, c≥1c \ge 1, and a/b<(a+c)/(b+d)<c/da/b < (a + c)/(b + d) < c/d; its value is in lowest terms and positive.

Proof. The root has 1⋅1−0⋅0=11 \cdot 1 - 0 \cdot 0 = 1. For the left child, b(a+c)−a(b+d)=bc−ad=1b(a + c) - a(b + d) = bc - ad = 1, and for the right child, (b+d)c−(a+c)d=bc−ad=1(b + d)c - (a + c)d = bc - ad = 1. The entries bb and cc never decrease and start at 1. Then (a+c)b−a(b+d)=bc−ad=1>0(a + c) b - a (b + d) = bc - ad = 1 > 0 gives a/b<(a+c)/(b+d)a/b < (a + c)/(b + d), and similarly c(b+d)−d(a+c)=1>0c(b + d) - d(a + c) = 1 > 0 gives the other inequality (for d=0d = 0 the right bound is +∞+\infty). The relation b⋅(a+c)−a⋅(b+d)=1b \cdot (a + c) - a \cdot (b + d) = 1 shows that any common divisor of a+ca + c and b+db + d divides 1. The value is positive because a+c≥c≥1a + c \ge c \ge 1. □\square

Lemma 2 (search tree). Every node in the subtree of a node x=(a,b,c,d)x = (a, b, c, d) has its value strictly between a/ba/b and c/dc/d; those in the left subtree are below the value of xx, and those in the right subtree are above it. In particular, all values are distinct.

Proof. By induction on the depth below xx, every node of the subtree has bounds in the closed interval [a/b,c/d][a/b, c/d], since a child replaces one bound by the value of its parent, which lies between the parent's bounds by Lemma 1. Its value lies strictly between its own bounds, hence strictly between a/ba/b and c/dc/d. The left child of xx has the right bound equal to the value of xx, so its whole subtree lies below it; symmetrically for the right child. Two distinct nodes either lie in different subtrees of their lowest common ancestor, or one is an ancestor of the other, and in both cases their values differ. □\square

Lemma 3 (completeness). Let p,q≥1p, q \ge 1 be coprime. The walk from the root that goes left when p/qp/q is smaller than the value and right when it is larger reaches the node with value p/qp/q after at most p+q−2p + q - 2 steps.

Proof. While the value differs from p/qp/q, Lemma 2 keeps a/b<p/q<c/da/b < p/q < c/d, so bp−aq≥1bp - aq \ge 1 and cq−dp≥1cq - dp \ge 1 as integers. Using bc−ad=1bc - ad = 1,

(c+d)(bp−aq)+(a+b)(cq−dp)=p(bc−ad)+q(bc−ad)=p+q,(c + d)(bp - aq) + (a + b)(cq - dp) = p(bc - ad) + q(bc - ad) = p + q,

so p+q≥a+b+c+d=σp + q \ge a + b + c + d = \sigma. Each step increases σ\sigma by a+ca + c or b+db + d, at least 1, and σ=2\sigma = 2 at the root, so after at most p+q−2p + q - 2 steps the walk is at a node whose value is p/qp/q. □\square

Lemmas 1 to 3 together say that the map from nodes to values is a bijection onto the positive rationals, with each value in lowest terms.

Lemma 4 (runs). From a node with bounds (a,b,c,d)(a, b, c, d), kk right moves lead to the bounds (a+kc,b+kd,c,d)(a + kc, b + kd, c, d), and kk left moves to (a,b,c+ka,d+kb)(a, b, c + ka, d + kb).

Proof. A right move keeps c,dc, d and replaces (a,b)(a, b) by (a+c,b+d)(a + c, b + d); after kk of them, (a,b)(a, b) has gained k(c,d)k(c, d). Left moves are symmetric. □\square

So DECODE (lines L8 to L11) returns the node at the end of the path.

Lemma 5 (ENCODE). For p,q≥1p, q \ge 1, ENCODE(p,q)(p, q) returns the path to the node with value p/qp/q.

Proof. Let p/q=[k0;k1,…,km]p/q = [k_0; k_1, \ldots, k_m] be the continued fraction produced by Euclid's algorithm, so k0≥0k_0 \ge 0, ki≥1k_i \ge 1 for 0<i<m0 < i < m, and km≥2k_m \ge 2 when m≥1m \ge 1 (the last quotient of Euclid is at least 2 because the last two remainders satisfy rm=kmrm+1r_{m} = k_m r_{m+1} and rm>rm+1r_m > r_{m+1}); if m=0m = 0, then p/q=k0≥1p/q = k_0 \ge 1. These quotients depend only on the value p/qp/q. Lines L2 to L6 compute exactly k0,…,kmk_0, \ldots, k_m, decrease kmk_m by 1, and emit (R,k0),(L,k1),(R,k2),…(R, k_0), (L, k_1), (R, k_2), \ldots, dropping runs of length 0.

It remains to show that the search walk for x=p/qx = p/q is Rk0Lk1Rk2⋯R^{k_0} L^{k_1} R^{k_2} \cdots with the last run of length km−1k_m - 1. At a node with bounds (a,b,c,d)(a, b, c, d), the map t↦(a+ct)/(b+dt)t \mapsto (a + ct)/(b + dt) is increasing on t≥0t \ge 0 (its derivative has the sign of bc−ad=1bc - ad = 1), runs from a/ba/b to c/dc/d, and takes the value of the node at t=1t = 1. So every xx with a/b<x<c/da/b < x < c/d is (a+ct)/(b+dt)(a + ct)/(b + dt) for exactly one real t>0t > 0, the coordinate of xx at that node, and the walk goes right if t>1t > 1, left if t<1t < 1, and stops if t=1t = 1. At the root, the coordinate of xx is xx itself.

  • After a right move, the bounds are (a+c,b+d,c,d)(a + c, b + d, c, d) and x=(a+c+ct′)/(b+d+dt′)x = (a + c + ct')/(b + d + dt') with t′=t−1t' = t - 1.
  • After a left move, the bounds are (a,b,a+c,b+d)(a, b, a + c, b + d) and x=(a+(a+c)t′)/(b+(b+d)t′)x = (a + (a + c)t')/(b + (b + d)t') with t=t′/(1+t′)t = t'/(1 + t'), that is, 1/t′=1/t−11/t' = 1/t - 1.

So, writing s=1/ts = 1/t while the walk goes left, the walk is: go right and subtract 1 from tt while t>1t > 1; go left and subtract 1 from ss while s>1s > 1; stop when the current coordinate is 1. We prove by induction on mm that, for t=[k0;k1,…,km]t = [k_0; k_1, \ldots, k_m] with km≥2k_m \ge 2 when m≥1m \ge 1 and t≥1t \ge 1 when m=0m = 0, this process makes k0k_0 moves in the first direction, then k1k_1 in the other, and so on, with km−1k_m - 1 in the last run. If m=0m = 0, then t=k0t = k_0 is an integer, and it makes k0−1k_0 - 1 moves before t=1t = 1. If m≥1m \ge 1, then k0<t<k0+1k_0 < t < k_0 + 1, so it makes exactly k0k_0 moves and reaches t=[0;k1,…,km]<1t = [0; k_1, \ldots, k_m] < 1, and from there it continues in the other direction with s=1/t=[k1;k2,…,km]>1s = 1/t = [k_1; k_2, \ldots, k_m] > 1, where the induction hypothesis applies. With t=x=p/qt = x = p/q at the root and the first direction RR, this is the output of ENCODE. Euclid's algorithm takes O(1+log⁡min⁡(p,q))O(1 + \log \min(p, q)) steps by Lamé's theorem, so ENCODE is fast even when the depth is large. □\square

Lemma 6 (PARENT). For a node x≠x \ne root with bounds (a,b,c,d)(a, b, c, d), line L13 returns the bounds of its parent.

Proof. If xx is the right child of y=(a′,b′,c′,d′)y = (a', b', c', d'), then (a,b)=(a′+c′,b′+d′)(a, b) = (a' + c', b' + d') and (c,d)=(c′,d′)(c, d) = (c', d'), so a+b=a′+b′+c′+d′>c′+d′=c+da + b = a' + b' + c' + d' > c' + d' = c + d because a′+b′≥b′≥1a' + b' \ge b' \ge 1, and y=(a−c,b−d,c,d)y = (a - c, b - d, c, d). If xx is the left child, then (c,d)=(a′+c′,b′+d′)(c, d) = (a' + c', b' + d') and (a,b)=(a′,b′)(a, b) = (a', b'), so c+d>a+bc + d > a + b because c′+d′≥c′≥1c' + d' \ge c' \ge 1, and y=(a,b,c−a,d−b)y = (a, b, c - a, d - b). The two cases are told apart by comparing a+ba + b with c+dc + d, which is what line L13 does. Only the root has bounds (0,1,1,0)(0, 1, 1, 0), since every other node has σ>2\sigma > 2; line L12 handles it. □\square

Lemma 7 (BEST-APPROXIMATION). For x=u/vx = u/v with u≥0u \ge 0, v≥1v \ge 1, and N≥1N \ge 1, BEST-APPROXIMATION returns a fraction p/qp/q in lowest terms with 1≤q≤N1 \le q \le N that minimizes ∣x−p/q∣|x - p/q|, and the smaller of two such fractions on a tie.

Proof. Invariant: the current bounds satisfy bc−ad=1bc - ad = 1, b≤Nb \le N, a/b≤x<c/da/b \le x < c/d with a/b<xa/b < x unless (a,b)=(0,1)(a, b) = (0, 1), and xx is not the value of any node visited so far. It holds at the start since 0≤x<1/00 \le x < 1/0.

In an iteration, the value m=(a+c)/(b+d)m = (a + c)/(b + d) has b+d≤Nb + d \le N. If m=xm = x, line L16 returns mm, which is in lowest terms by Lemma 1 and is at distance 0. If m<xm < x, every kk with (a+kc)/(b+kd)<x(a + kc)/(b + kd) < x and b+kd≤Nb + kd \le N is allowed, k=1k = 1 among them. The fraction (a+kc)/(b+kd)(a + kc)/(b + kd) is increasing in kk (because bc−ad=1>0bc - ad = 1 > 0), so these kk form an initial segment, and the condition (a+kc)v<u(b+kd)(a + kc) v < u (b + kd) is k(cv−ud)<ub−avk(cv - ud) < ub - av, where cv−ud>0cv - ud > 0 by x<c/dx < c/d; this gives the largest kk as ⌊(ub−av−1)/(cv−ud)⌋\lfloor (ub - av - 1)/(cv - ud) \rfloor, capped by ⌊(N−b)/d⌋\lfloor (N - b)/d \rfloor if d>0d > 0. By Lemma 4 the new bounds are those of a node of the tree, and the invariant holds, the new left bound being below xx. If m>xm > x, the argument is symmetric with (c+ka)/(d+kb)(c + ka)/(d + kb) decreasing in kk and the condition k(ub−av)<cv−udk(ub - av) < cv - ud, where ub−av≥0ub - av \ge 0, with equality only when (a,b)=(0,1)(a, b) = (0, 1) and x=0x = 0, in which case the condition holds for all kk. The new right bound stays above xx, and d+kb≤Nd + kb \le N.

The loop ends. A left move increases dd by kb≥1kb \ge 1, and a right move increases bb by kd≥1kd \ge 1 unless d=0d = 0. A right move with d=0d = 0 is not capped by NN, so its kk is the largest one with (a+kc)/(b+kd)<x(a + kc)/(b + kd) < x, and the next node is at least xx: the next iteration returns or moves left. So b+db + d grows at least every second iteration and eventually exceeds NN. When it ends without returning, b+d>Nb + d > N and a/b≤x<c/da/b \le x < c/d.

Now suppose p/qp/q is a fraction with a/b<p/q<c/da/b < p/q < c/d. Then bp−aq≥1bp - aq \ge 1 and cq−dp≥1cq - dp \ge 1, and as in Lemma 3, q=b(cq−dp)+d(bp−aq)q = b(cq - dp) + d(bp - aq)... precisely, q(bc−ad)=b(cq−dp)+d(bp−aq)≥b+d>Nq(bc - ad) = b(cq - dp) + d(bp - aq) \ge b + d > N. So no fraction with denominator at most NN lies strictly between a/ba/b and c/dc/d. Every candidate p/q≤a/bp/q \le a/b is at distance at least x−a/bx - a/b from xx, and every candidate p/q≥c/dp/q \ge c/d is at distance at least c/d−xc/d - x; both bounds have denominators at most NN when finite (b≤Nb \le N, and d≤Nd \le N when d≥1d \ge 1). So the nearest candidates are a/ba/b and c/dc/d, and line L19 picks the nearer, comparing (ub−av)d(ub - av) d with (cv−ud)b(cv - ud) b, which are bdvbdv times the two distances. If d=0d = 0, then c/d=+∞c/d = +\infty and a/ba/b is the answer. On a tie it returns a/ba/b, the smaller one, and a/ba/b is in lowest terms because bc−ad=1bc - ad = 1. ■\blacksquare

Back to Stern-Brocot tree