Algorithm Search⌘K

Proofs: k-nearest neighbor graph

These are the human proofs of k-nearest neighbor graph, at full length.

Correctness of the k-nearest neighbor graph build and of its beam search full

This proof shows: correctness.

Let P={p0,…,pn−1}P = \{p_0, \dots, p_{n-1}\} with a distance δ\delta. Order pairs (d,j)(d, j) lexicographically, so that ties in distance go to the smaller index.

Lemma 1 (build). After line L4, N(i)N(i) is the sequence of the min⁡(k,n−1)\min(k, n-1) least pairs (δ(pi,pj),j)(\delta(p_i, p_j), j) over j≠ij \ne i, in increasing order.

Proof. We show by induction on the offers at line L3 that HiH_i holds the min⁡(k,t)\min(k, t) least pairs among the first tt pairs offered. For t=0t = 0 it is empty. Suppose it holds after tt offers and pair aa is offered next. If ∣Hi∣<k|H_i| < k, aa is added, and HiH_i holds all t+1t + 1 pairs. Otherwise HiH_i holds the kk least of the first tt, and its root mm is the largest of them. If a<ma < m, the kk least of the first t+1t + 1 pairs are Hi∖{m}∪{a}H_i \setminus \{m\} \cup \{a\}, which is what the replacement gives. If a>ma > m, the kk least are unchanged and aa is not added. All pairs are distinct since the indices are, so there is no third case. After all n−1n - 1 offers, HiH_i holds the min⁡(k,n−1)\min(k, n-1) least pairs, and line L4 lists them in increasing order. □\square

For the search, write f(x)=(δ(q,px),x)f(x) = (\delta(q, p_x), x), and let "nearer" compare ff. The heaps CC and WW order by ff.

Lemma 2 (termination and the set WW). SEARCH ends after at most nn passes of line L7. At every moment, WW holds the min⁡(ef,∣V∣)\min(ef, |V|) nearest points of VV, and every point of WW has entered CC.

Proof. A point enters VV once, at line L5 or line L8, and enters CC only then, so CC receives at most nn points, and each pass of line L7 removes one. At line L5, WW is the min⁡(ef,∣S∣)\min(ef, |S|) nearest points of V=SV = S, and all of SS is in CC. At line L8, a new point ee joins VV. If ∣W∣<ef|W| < ef then ∣V∣≤ef|V| \le ef before the step, W=VW = V, and ee is added to WW and to CC. Otherwise WW holds the efef nearest points of the old VV; if ee is nearer than the farthest of WW, it replaces that point and enters CC, and if not, WW is still the efef nearest of the new VV. So the invariant holds after each step. □\square

Lemma 3 (local minimum). Let ww be the nearest point of WW when SEARCH stops. Then ww was extracted at line L7 and expanded at line L8. Hence every out-neighbor ee of ww is in VV and satisfies f(e)>f(w)f(e) > f(w); in particular δ(q,pe)≥δ(q,pw)\delta(q, p_e) \ge \delta(q, p_w).

Proof. By Lemma 2, ww is the nearest point of VV, and ww entered CC. Suppose ww was never extracted. Then ww is still in CC when the loop stops. The loop cannot stop because CC is empty, so it stops at line L7 with an extracted cc such that δ(q,pc)>δ(q,farthest of W)≥δ(q,pw)\delta(q, p_c) > \delta(q, \text{farthest of } W) \ge \delta(q, p_w). But CC is a min-heap that contains ww, so the extracted cc satisfies f(c)≤f(w)f(c) \le f(w), a contradiction. So ww was extracted, and since ww is the nearest point of VV and W≠∅W \ne \emptyset, the test of line L7 did not stop the loop at ww: δ(q,pw)≤δ(q,farthest of W)\delta(q, p_w) \le \delta(q, \text{farthest of } W) at that time, as WW then held points of VV and ww was already among them or nearer. So ww was expanded, all its out-neighbors entered VV, and since ww is the nearest point of VV, each of them is farther in the order ff. □\square

Lemma 4 (reach). If ef≥nef \ge n, then when SEARCH stops, VV is the set RR of points reachable from SS along directed edges.

Proof. V⊆RV \subseteq R, since points enter VV only from SS or along an edge out of a point of VV. With ef≥n≥∣V∣ef \ge n \ge |V|, the removal at line L8 never happens, the test ∣W∣<ef|W| < ef holds for every new point unless ∣W∣=n|W| = n, in which case all points are already in VV; so every point of VV is in WW and in CC. Every point cc extracted from CC is in WW, so δ(q,pc)≤δ(q,farthest of W)\delta(q, p_c) \le \delta(q, \text{farthest of } W), and line L7 never stops the loop. So the loop runs until CC is empty, and every point of VV is expanded. Then VV contains SS and is closed under out-edges, so R⊆VR \subseteq V. □\square

Theorem. If ef≥nef \ge n, SEARCH(q,k′,ef,S)(q, k', ef, S) returns the min⁡(k′,∣R∣)\min(k', |R|) nearest points to qq in RR, in the order ff.

Proof. By Lemma 4, V=RV = R at the end. By Lemma 2, WW holds the min⁡(ef,∣R∣)=∣R∣\min(ef, |R|) = |R| nearest points of RR, that is, all of RR. Line L9 returns the first k′k' of them in the order ff. ■\blacksquare

Back to k-nearest neighbor graph