Correctness of cover tree insertion and nearest neighbor search full
This proof shows: correctness.
Let (M,δ) be a metric space and S the stored points, with the levels Ci as on the page. A tree is valid if it satisfies nesting, covering and separation for every i, and Ctop={root} with Ci={root} for i≥top.
Lemma 1 (descendants). In a valid tree, every descendant r of a node (q,j) satisfies δ(q,r)≤2j+1, and if r∈Cl with l<j then δ(q,r)≤2j+1−2l+1.
Proof. The path from (q,j) down to (r,l) passes through nodes (qm,m) for m=j,j−1,…,l, with qj=q, ql=r, and δ(qm,qm−1)≤2m by covering. By the triangle inequality δ(q,r)≤∑m=l+1j2m=2j+1−2l+1<2j+1. □
Lemma 2 (the sets Qj). In every call INSERT(p,Qj,j) made from the top call, Qj⊆Cj and Qj⊇{r∈Cj:δ(p,r)≤2j+1}. The same holds for the sets Qj of NEAREST with 2j+1 replaced by δ(p,Q)+2j+1, where Q is the set computed at the level above.
Proof. At the top, Qtop={root}=Ctop. Suppose the claim holds for Qi, and let r∈Ci−1 with δ(p,r)≤2i. Its parent s∈Ci satisfies δ(r,s)≤2i by covering, so δ(p,s)≤2i+1 and s∈Qi. Then r∈Children(Qi,i), and line L3 keeps it. Every element of Children(Qi,i) is in Ci−1 by nesting and the definition of children. □
Lemma 3 (termination). If δ(p,r)>0 for every r∈S, the recursion of INSERT has finite depth.
Proof. Let m=minr∈Sδ(p,r)>0. A call at level j with 2j<m finds δ(p,Q)≥m>2j at line L2 and returns without recursion. □
Lemma 4 (failure means far). If INSERT(p,Qj,j) returns "no parent found", then δ(p,r)>2l for every l≤j and every r∈Cl.
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 for every q∈Children(Qj,j), which by Lemma 2 contains every r∈Cj−1 with δ(p,r)≤2j. So δ(p,r)>2j for all r∈Cj−1, and since Cj⊆Cj−1 this covers l=j and l=j−1. For l<j−1 and r∈Cl, let a be the ancestor of r in Cj−1. By Lemma 1, δ(a,r)≤2j−2l+1, so δ(p,r)≥δ(p,a)−δ(a,r)>2j−(2j−2l+1)=2l+1>2l.
Return at line L5. The call at level j−1 returned "no parent found", so by induction the claim holds for all l≤j−1. Also δ(p,q)>2j for all q∈Qj, and by Lemma 2 every r∈Cj with δ(p,r)≤2j+1 is in Qj; so every r∈Cj has δ(p,r)>2j, which is the case l=j. □
Theorem 1 (insertion keeps the tree valid). Let the tree be valid, and let p be at positive distance from every point of S. After raising top until δ(p,root)≤2top, INSERT(p,{root},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) at distance at most 2top, so line L2 does not fail; if the recursive call fails, the root is within 2top at line L5, so the top call returns "parent found". Exactly one call executes line L6, at some level i, after the call at level i−1 failed. The new tree adds p to Cl for every l≤i−1.
Nesting holds since p is added to all levels below i−1 as its own child. Covering: at level i−1 the parent of p is q∈Qi⊆Ci with δ(p,q)≤2i; below it, p is its own parent. Other points keep their parents. Separation: for l≤i−1 and r∈Cl, Lemma 4 applied to the failed call at level i−1 gives δ(p,r)>2l. Pairs not involving p are unchanged. □
Theorem 2 (search). In a valid tree, NEAREST(p) returns a point r∈S with δ(p,r)=mins∈Sδ(p,s).
Proof. Let p∗ be a nearest point. For each level j from top down to bottom, let aj be the ancestor of p∗ in Cj (with aj=p∗ when p∗∈Cj). We show aj∈Qj by induction. atop is the root. Suppose ai∈Qi. Then ai−1 is a child of ai at level i−1, so ai−1∈Q=Children(Qi,i). By Lemma 1 applied to the node (ai−1,i−1), δ(ai−1,p∗)≤2i, so
δ(p,ai−1)≤δ(p,p∗)+2i≤δ(p,Q)+2i, since p∗ is nearest overall. Line L9 keeps ai−1.
At the end the loop has produced Qbottom. Every point appears in some explicit list at a level at least bottom, or is the root, so p∗∈Cbottom and abottom=p∗. Hence p∗∈Qbottom, and line L10 returns a point at distance at most δ(p,p∗), which is the minimum. ■
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−1 entries for n points, and the space is O(n) for every metric.