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 with a distance . Order pairs lexicographically, so that ties in distance go to the smaller index.
Lemma 1 (build). After line L4, is the sequence of the least pairs over , in increasing order.
Proof. We show by induction on the offers at line L3 that holds the least pairs among the first pairs offered. For it is empty. Suppose it holds after offers and pair is offered next. If , is added, and holds all pairs. Otherwise holds the least of the first , and its root is the largest of them. If , the least of the first pairs are , which is what the replacement gives. If , the least are unchanged and is not added. All pairs are distinct since the indices are, so there is no third case. After all offers, holds the least pairs, and line L4 lists them in increasing order.
For the search, write , and let "nearer" compare . The heaps and order by .
Lemma 2 (termination and the set ). SEARCH ends after at most passes of line L7. At every moment, holds the nearest points of , and every point of has entered .
Proof. A point enters once, at line L5 or line L8, and enters only then, so receives at most points, and each pass of line L7 removes one. At line L5, is the nearest points of , and all of is in . At line L8, a new point joins . If then before the step, , and is added to and to . Otherwise holds the nearest points of the old ; if is nearer than the farthest of , it replaces that point and enters , and if not, is still the nearest of the new . So the invariant holds after each step.
Lemma 3 (local minimum). Let be the nearest point of when SEARCH stops. Then was extracted at line L7 and expanded at line L8. Hence every out-neighbor of is in and satisfies ; in particular .
Proof. By Lemma 2, is the nearest point of , and entered . Suppose was never extracted. Then is still in when the loop stops. The loop cannot stop because is empty, so it stops at line L7 with an extracted such that . But is a min-heap that contains , so the extracted satisfies , a contradiction. So was extracted, and since is the nearest point of and , the test of line L7 did not stop the loop at : at that time, as then held points of and was already among them or nearer. So was expanded, all its out-neighbors entered , and since is the nearest point of , each of them is farther in the order .
Lemma 4 (reach). If , then when SEARCH stops, is the set of points reachable from along directed edges.
Proof. , since points enter only from or along an edge out of a point of . With , the removal at line L8 never happens, the test holds for every new point unless , in which case all points are already in ; so every point of is in and in . Every point extracted from is in , so , and line L7 never stops the loop. So the loop runs until is empty, and every point of is expanded. Then contains and is closed under out-edges, so .
Theorem. If , SEARCH returns the nearest points to in , in the order .
Proof. By Lemma 4, at the end. By Lemma 2, holds the nearest points of , that is, all of . Line L9 returns the first of them in the order .