Algorithm Search⌘K

Proofs: Cover tree

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

Correctness of cover tree insertion and nearest neighbor search full

This proof shows: correctness.

Let (M,δ)(M, \delta) be a metric space and SS the stored points, with the levels CiC_i as on the page. A tree is valid if it satisfies nesting, covering and separation for every ii, and Ctop={root}C_{\text{top}} = \{\text{root}\} with Ci={root}C_i = \{\text{root}\} for i≥topi \ge \text{top}.

Lemma 1 (descendants). In a valid tree, every descendant rr of a node (q,j)(q, j) satisfies δ(q,r)≤2j+1\delta(q, r) \le 2^{j+1}, and if r∈Clr \in C_l with l<jl < j then δ(q,r)≤2j+1−2l+1\delta(q, r) \le 2^{j+1} - 2^{l+1}.

Proof. The path from (q,j)(q, j) down to (r,l)(r, l) passes through nodes (qm,m)(q_m, m) for m=j,j−1,…,lm = j, j-1, \dots, l, with qj=qq_j = q, ql=rq_l = r, and δ(qm,qm−1)≤2m\delta(q_{m}, q_{m-1}) \le 2^m by covering. By the triangle inequality δ(q,r)≤∑m=l+1j2m=2j+1−2l+1<2j+1\delta(q, r) \le \sum_{m = l+1}^{j} 2^m = 2^{j+1} - 2^{l+1} < 2^{j+1}. □\square

Lemma 2 (the sets QjQ_j). In every call INSERT(p,Qj,j)(p, Q_j, j) made from the top call, Qj⊆CjQ_j \subseteq C_j and Qj⊇{r∈Cj:δ(p,r)≤2j+1}Q_j \supseteq \{r \in C_j : \delta(p, r) \le 2^{j+1}\}. The same holds for the sets QjQ_j of NEAREST with 2j+12^{j+1} replaced by δ(p,Q)+2j+1\delta(p, Q) + 2^{j+1}, where QQ is the set computed at the level above.

Proof. At the top, Qtop={root}=CtopQ_{\text{top}} = \{\text{root}\} = C_{\text{top}}. Suppose the claim holds for QiQ_i, and let r∈Ci−1r \in C_{i-1} with δ(p,r)≤2i\delta(p, r) \le 2^i. Its parent s∈Cis \in C_i satisfies δ(r,s)≤2i\delta(r, s) \le 2^i by covering, so δ(p,s)≤2i+1\delta(p, s) \le 2^{i+1} and s∈Qis \in Q_i. Then r∈Children(Qi,i)r \in \text{Children}(Q_i, i), and line L3 keeps it. Every element of Children(Qi,i)(Q_i, i) is in Ci−1C_{i-1} by nesting and the definition of children. □\square

Lemma 3 (termination). If δ(p,r)>0\delta(p, r) > 0 for every r∈Sr \in S, the recursion of INSERT has finite depth.

Proof. Let m=min⁡r∈Sδ(p,r)>0m = \min_{r \in S} \delta(p, r) > 0. A call at level jj with 2j<m2^j < m finds δ(p,Q)≥m>2j\delta(p, Q) \ge m > 2^j at line L2 and returns without recursion. □\square

Lemma 4 (failure means far). If INSERT(p,Qj,j)(p, Q_j, j) returns "no parent found", then δ(p,r)>2l\delta(p, r) > 2^l for every l≤jl \le j and every r∈Clr \in C_l.

Proof. By induction on the recursion, from the deepest call up, which is possible by Lemma 3.

Return at line L2. Then δ(p,q)>2j\delta(p, q) > 2^j for every q∈Children(Qj,j)q \in \text{Children}(Q_j, j), which by Lemma 2 contains every r∈Cj−1r \in C_{j-1} with δ(p,r)≤2j\delta(p, r) \le 2^j. So δ(p,r)>2j\delta(p, r) > 2^j for all r∈Cj−1r \in C_{j-1}, and since Cj⊆Cj−1C_j \subseteq C_{j-1} this covers l=jl = j and l=j−1l = j - 1. For l<j−1l < j - 1 and r∈Clr \in C_l, let aa be the ancestor of rr in Cj−1C_{j-1}. By Lemma 1, δ(a,r)≤2j−2l+1\delta(a, r) \le 2^j - 2^{l+1}, so δ(p,r)≥δ(p,a)−δ(a,r)>2j−(2j−2l+1)=2l+1>2l\delta(p, r) \ge \delta(p, a) - \delta(a, r) > 2^j - (2^j - 2^{l+1}) = 2^{l+1} > 2^l.

Return at line L5. The call at level j−1j - 1 returned "no parent found", so by induction the claim holds for all l≤j−1l \le j - 1. Also δ(p,q)>2j\delta(p, q) > 2^j for all q∈Qjq \in Q_j, and by Lemma 2 every r∈Cjr \in C_j with δ(p,r)≤2j+1\delta(p, r) \le 2^{j+1} is in QjQ_j; so every r∈Cjr \in C_j has δ(p,r)>2j\delta(p, r) > 2^j, which is the case l=jl = j. □\square

Theorem 1 (insertion keeps the tree valid). Let the tree be valid, and let pp be at positive distance from every point of SS. After raising top until δ(p,root)≤2top\delta(p, \text{root}) \le 2^{\text{top}}, INSERT(p,{root},top)(p, \{\text{root}\}, \text{top}) returns "parent found", and the new tree is valid.

Proof. Raising top adds levels that hold only the root; they are valid. In the top call, the root is in Children({root},top)(\{\text{root}\}, \text{top}) at distance at most 2top2^{\text{top}}, so line L2 does not fail; if the recursive call fails, the root is within 2top2^{\text{top}} at line L5, so the top call returns "parent found". Exactly one call executes line L6, at some level ii, after the call at level i−1i - 1 failed. The new tree adds pp to ClC_l for every l≤i−1l \le i - 1.

Nesting holds since pp is added to all levels below i−1i - 1 as its own child. Covering: at level i−1i - 1 the parent of pp is q∈Qi⊆Ciq \in Q_i \subseteq C_i with δ(p,q)≤2i\delta(p, q) \le 2^i; below it, pp is its own parent. Other points keep their parents. Separation: for l≤i−1l \le i - 1 and r∈Clr \in C_l, Lemma 4 applied to the failed call at level i−1i - 1 gives δ(p,r)>2l\delta(p, r) > 2^l. Pairs not involving pp are unchanged. □\square

Theorem 2 (search). In a valid tree, NEAREST(p)(p) returns a point r∈Sr \in S with δ(p,r)=min⁡s∈Sδ(p,s)\delta(p, r) = \min_{s \in S} \delta(p, s).

Proof. Let p∗p^* be a nearest point. For each level jj from top down to bottom, let aja_j be the ancestor of p∗p^* in CjC_j (with aj=p∗a_j = p^* when p∗∈Cjp^* \in C_j). We show aj∈Qja_j \in Q_j by induction. atopa_{\text{top}} is the root. Suppose ai∈Qia_i \in Q_i. Then ai−1a_{i-1} is a child of aia_i at level i−1i - 1, so ai−1∈Q=Children(Qi,i)a_{i-1} \in Q = \text{Children}(Q_i, i). By Lemma 1 applied to the node (ai−1,i−1)(a_{i-1}, i - 1), δ(ai−1,p∗)≤2i\delta(a_{i-1}, p^*) \le 2^i, so

δ(p,ai−1)≤δ(p,p∗)+2i≤δ(p,Q)+2i,\delta(p, a_{i-1}) \le \delta(p, p^*) + 2^i \le \delta(p, Q) + 2^i,

since p∗p^* is nearest overall. Line L9 keeps ai−1a_{i-1}.

At the end the loop has produced QbottomQ_{\text{bottom}}. Every point appears in some explicit list at a level at least bottom, or is the root, so p∗∈Cbottomp^* \in C_{\text{bottom}} and abottom=p∗a_{\text{bottom}} = p^*. Hence p∗∈Qbottomp^* \in Q_{\text{bottom}}, and line L10 returns a point at distance at most δ(p,p∗)\delta(p, p^*), which is the minimum. ■\blacksquare

Space. Each insertion executes line L6 once and adds one entry to one child list; a duplicate adds one entry to a duplicate list. So the explicit tree has exactly n−1n - 1 entries for nn points, and the space is O(n)O(n) for every metric.

Back to Cover tree