Algorithm Search⌘K

Proofs: BK-tree

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

The BK-tree keeps its invariant and answers range and nearest queries exactly full

This proof shows: correctness.

Let (M,δ)(M, \delta) be a metric space with δ(x,y)∈{0,1,2,… }\delta(x, y) \in \{0, 1, 2, \dots\}. So δ(x,y)=0\delta(x, y) = 0 if and only if x=yx = y, δ\delta is symmetric, and δ(x,z)≤δ(x,y)+δ(y,z)\delta(x, z) \le \delta(x, y) + \delta(y, z).

Invariant (B). For each node tt and each key ii of t.childt.child, every item xx in the subtree of t.child[i]t.child[i] has δ(x,t.item)=i\delta(x, t.item) = i.

Lemma 1. INSERT keeps (B) and stores no item twice.

Proof. Let t0,t1,…,tmt_0, t_1, \dots, t_m be the nodes visited, with t0t_0 the root. For j<mj < m, INSERT moves from tjt_j to tj+1=tj.child[kj]t_{j+1} = t_j.child[k_j] with kj=δ(x,tj.item)k_j = \delta(x, t_j.item), and at tmt_m it either returns at line L4 or adds xx as tm.child[km]t_m.child[k_m]. In the second case, xx lies in the subtree of key kjk_j of each tjt_j and has distance kjk_j to tj.itemt_j.item, so (B) holds for xx at every ancestor. No other node gains or loses an item. If an equal item is stored, it lies on the path of xx: by induction, if y=xy = x is in the subtree of tjt_j, then δ(y,tj.item)=δ(x,tj.item)=kj\delta(y, t_j.item) = \delta(x, t_j.item) = k_j, so yy is tj.itemt_j.item or lies in the subtree of tj.child[kj]=tj+1t_j.child[k_j] = t_{j+1}. So INSERT reaches the node of yy and returns at line L4. □\square

Lemma 2. If xx is in the subtree of t.child[i]t.child[i], then δ(q,x)≥∣δ(q,t.item)−i∣\delta(q, x) \ge |\delta(q, t.item) - i| for every qq.

Proof. By (B), δ(x,t.item)=i\delta(x, t.item) = i. The triangle inequality gives δ(q,t.item)≤δ(q,x)+i\delta(q, t.item) \le \delta(q, x) + i and i=δ(x,t.item)≤δ(x,q)+δ(q,t.item)i = \delta(x, t.item) \le \delta(x, q) + \delta(q, t.item). With symmetry, these two inequalities say δ(q,x)≥∣δ(q,t.item)−i∣\delta(q, x) \ge |\delta(q, t.item) - i|. □\square

Theorem 1. RANGE(t,q,r)(t, q, r) reports exactly the items xx in the subtree of tt with δ(q,x)≤r\delta(q, x) \le r, each once.

Proof. By induction on the height of tt. For $t = $ nil there is nothing to report. Otherwise, line L7 reports t.itemt.item if and only if k=δ(q,t.item)≤rk = \delta(q, t.item) \le r. Every other item xx of the subtree lies in exactly one child subtree, say of key ii. If δ(q,x)≤r\delta(q, x) \le r, Lemma 2 gives ∣k−i∣≤r|k - i| \le r, so line L8 calls RANGE on that child, and by induction the call reports xx once. If the child is not called, no item in it is within rr. Items of called children with δ(q,x)>r\delta(q, x) > r are not reported, by induction. □\square

Theorem 2. NEAREST(T,q)(T, q) returns τ=min⁡x∈Sδ(q,x)\tau = \min_{x \in S} \delta(q, x) and an item bestbest with δ(q,best)=τ\delta(q, best) = \tau, if SS is not empty.

Proof. We show that the loop keeps the following invariant (N): for each stack entry (b,u)(b, u), every item of the subtree of uu is at distance at least bb from qq; and every item of SS is in the subtree of a stack entry, or has been evaluated at line L11, or is at distance at least τ\tau from qq.

At the start, the stack holds (0,root)(0, root), and (N) holds. Pop (b,t)(b, t). If b≥τb \ge \tau, every item of the subtree of tt is at distance at least b≥τb \ge \tau, so dropping the entry keeps (N). Otherwise line L11 evaluates t.itemt.item, and lowers τ\tau only to the distance of an evaluated item; items earlier covered by "at least τ\tau" remain at least the new, smaller τ\tau. Each child of key ii is pushed with bound ∣i−k∣|i - k|, which is a lower bound for its subtree by Lemma 2. A child with ∣i−k∣≥τ|i - k| \ge \tau is not pushed, and all of its items are at distance at least τ\tau by Lemma 2. So (N) holds after the step.

Each node is pushed at most once, since it is pushed only by its parent when the parent is evaluated, so the loop ends. Then the stack is empty, and by (N) every item is evaluated or at distance at least τ\tau. Since τ\tau is the least distance among evaluated items and bestbest is an item with that distance, τ\tau is the minimum over SS. ■\blacksquare

Back to BK-tree