Algorithm Search⌘K

Proofs: Ball tree

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

The ball tree is balanced and its searches are exact full

This proof shows: correctness.

Let (M,δ)(M, \delta) be a metric space, S⊆MS \subseteq M with ∣S∣=n≥1|S| = n \ge 1, and b≥1b \ge 1 the leaf size.

Lemma 1 (covering). Every node tt built by BUILD satisfies (R): δ(t.c,x)≤t.ρ\delta(t.c, x) \le t.\rho for every point xx of its subtree.

Proof. Line L2 sets t.ρ=max⁡x∈Sδ(t.c,x)t.\rho = \max_{x \in S} \delta(t.c, x), where SS is the set of points of the subtree of tt. □\square

Lemma 2 (height). A node at depth jj (the root has depth 0) holds at most ⌈n/2j⌉\lceil n / 2^j \rceil points, and the tree has at most ⌈lg⁡(n/b)⌉+1\lceil \lg(n/b) \rceil + 1 levels when n>bn > b.

Proof. Line L4 gives the children ⌊s/2⌋\lfloor s/2 \rfloor and ⌈s/2⌉\lceil s/2 \rceil points, and ⌈⌈n/2j⌉/2⌉=⌈n/2j+1⌉\lceil \lceil n/2^j \rceil / 2 \rceil = \lceil n / 2^{j+1} \rceil. A node with at most bb points is a leaf. At depth j=⌈lg⁡(n/b)⌉j = \lceil \lg(n/b) \rceil, n/2j≤bn/2^j \le b, and bb is an integer, so ⌈n/2j⌉≤b\lceil n/2^j \rceil \le b and every node at that depth is a leaf. □\square

Lemma 3 (lower bound). For every q∈Mq \in M and every point xx in the subtree of tt, δ(q,x)≥δ(q,t.c)−t.ρ\delta(q, x) \ge \delta(q, t.c) - t.\rho.

Proof. By the triangle inequality, δ(q,t.c)≤δ(q,x)+δ(x,t.c)\delta(q, t.c) \le \delta(q, x) + \delta(x, t.c), and δ(x,t.c)≤t.ρ\delta(x, t.c) \le t.\rho by Lemma 1. □\square

Theorem 1. KNN(root,q,k)(root, q, k) returns min⁡(k,n)\min(k, n) pairs whose distances are the min⁡(k,n)\min(k, n) smallest values of δ(q,x)\delta(q, x) over x∈Sx \in S, each with a point at that distance.

Proof. Let τ\tau be the kk-th distance in HH, or ∞\infty when HH has fewer than kk pairs; τ\tau never increases. Every pair in HH is a stored point with its true distance, since only line L7 offers pairs. Call a point skipped if it lies in a subtree pruned at line L6. At the moment a node tt is pruned, Lemma 3 gives δ(q,x)≥δ(q,t.c)−t.ρ≥τ\delta(q, x) \ge \delta(q, t.c) - t.\rho \ge \tau for each point xx of tt, so HH already holds kk pairs of distance at most δ(q,x)\delta(q, x). Since HH keeps the kk least offered pairs, it holds kk pairs of distance at most δ(q,x)\delta(q, x) at the end as well.

Let D1≤⋯≤DkD_1 \le \dots \le D_k be the final distances in HH and δ1≤⋯≤δn\delta_1 \le \dots \le \delta_n the true sorted distances. Each DiD_i is the distance of a distinct point, so Di≥δiD_i \ge \delta_i. Suppose Di>δiD_i > \delta_i for some ii, and take the ii points x1,…,xix_1, \dots, x_i with distances δ1,…,δi\delta_1, \dots, \delta_i. At most i−1i - 1 of them are in HH with a distance below DiD_i, so some xjx_j with δ(q,xj)≤δi<Di\delta(q, x_j) \le \delta_i < D_i is not in HH. If xjx_j was offered and later dropped or never kept, then at that moment HH held kk pairs of distance at most δ(q,xj)\delta(q, x_j), and the final pairs are no larger, so Dk≤δ(q,xj)<Di≤DkD_k \le \delta(q, x_j) < D_i \le D_k, which is impossible. If xjx_j was skipped, the paragraph above gives Dk≤δ(q,xj)<Di≤DkD_k \le \delta(q, x_j) < D_i \le D_k, again impossible. So Di=δiD_i = \delta_i for all ii. If n<kn < k, no node is pruned since τ=∞\tau = \infty, and every point is offered. □\square

Theorem 2. RANGE(root,q,r)(root, q, r) reports exactly the points x∈Sx \in S with δ(q,x)≤r\delta(q, x) \le r, each once.

Proof. Line L10 reports only points that pass the test. A point xx is not offered only if an ancestor node tt was pruned at line L9, and then Lemma 3 gives δ(q,x)≥δ(q,t.c)−t.ρ>r\delta(q, x) \ge \delta(q, t.c) - t.\rho > r. Each point is in exactly one leaf, which is visited at most once. ■\blacksquare

Back to Ball tree