Algorithm Search⌘K

Proofs: Brute-force nearest neighbor search

These are the human proofs of Brute-force nearest neighbor search, at full length.

The brute-force scan returns the k nearest points full

This proof shows: correctness.

Let P[0…n−1]P[0 \ldots n-1] be the points, qq the query and k≥1k \ge 1. For j<nj < n let ej=(δ(q,P[j]),j)e_j = (\delta(q, P[j]), j), compared in lexicographic order. The indices are distinct, so the eje_j are distinct and totally ordered. For 0≤i≤n0 \le i \le n let BiB_i be the set of the min⁡(k,i)\min(k, i) smallest elements of {e0,…,ei−1}\{e_0, \ldots, e_{i-1}\}.

Invariant (H). At the start of the iteration of line L2 with index ii, and after the last iteration with i=ni = n, the heap HH holds exactly the elements of BiB_i.

Initialization. Before the first iteration, i=0i = 0, line L1 made HH empty, and B0=∅B_0 = \emptyset.

Maintenance. Assume (H) at the start of iteration i<ni < n. Line L3 computes eie_i.

  • If ∣H∣<k|H| < k, then ∣Bi∣=min⁡(k,i)<k|B_i| = \min(k, i) < k, so i<ki < k and Bi={e0,…,ei−1}B_i = \{e_0, \ldots, e_{i-1}\}. Then Bi+1B_{i+1} is all of {e0,…,ei}\{e_0, \ldots, e_i\}, since i+1≤ki + 1 \le k. Line L4 inserts eie_i, so H=Bi+1H = B_{i+1}.

  • Else ∣H∣=k|H| = k and i≥ki \ge k. Let tt be the largest element of BiB_i, which is TOP(H)(H) by the max-heap property. Each element of {e0,…,ei−1}∖Bi\{e_0, \ldots, e_{i-1}\} \setminus B_i is larger than tt.

    • If ei<te_i < t, then eie_i is among the kk smallest of {e0,…,ei}\{e_0, \ldots, e_i\}: at most k−1k - 1 elements of BiB_i are below it, and no other element is. The element tt now has kk smaller elements (Bi∖{t}B_i \setminus \{t\} and eie_i), so it leaves. So Bi+1=(Bi∖{t})∪{ei}B_{i+1} = (B_i \setminus \{t\}) \cup \{e_i\}, which is what line L5 makes.
    • If ei>te_i > t, then the kk elements of BiB_i are smaller than eie_i, so ei∉Bi+1e_i \notin B_{i+1} and Bi+1=BiB_{i+1} = B_i. Line L5 does nothing.

    The case ei=te_i = t cannot occur, because the elements are distinct.

In each case (H) holds at the start of iteration i+1i + 1.

Termination. The loop runs for i=0,1,…,n−1i = 0, 1, \ldots, n-1, so it stops after nn iterations, with H=BnH = B_n.

Theorem. Line L6 returns a list s1<s2<⋯<sms_1 < s_2 < \cdots < s_m of m=min⁡(k,n)m = \min(k, n) pairs, and the set SS of their points is a correct answer to the kk-nearest neighbor query: δ(q,s)≤δ(q,p)\delta(q, s) \le \delta(q, p) for each s∈Ss \in S and p∈P∖Sp \in P \setminus S.

Proof. By (H), line L6 sorts BnB_n, which has min⁡(k,n)\min(k, n) elements. Let s∈Ss \in S and p∈P∖Sp \in P \setminus S, with pairs ese_s and epe_p. Since ep∉Bne_p \notin B_n and BnB_n is the set of the smallest elements, es<epe_s < e_p. The first component of a pair is the distance, so lexicographic order gives δ(q,s)≤δ(q,p)\delta(q, s) \le \delta(q, p). ■\blacksquare

Ties. When several points have the distance of the mm-th result, BnB_n takes those with the smallest indices, because the second component decides between pairs of equal distance.

Back to Brute-force nearest neighbor search