Algorithm Search⌘K

Proofs: NN-Descent

These are the human proofs of NN-Descent, at full length.

NN-Descent keeps valid lists, terminates, and stops at a locally optimal graph full

This proof shows: correctness.

We use the names of the pseudocode on the page. Fix n≥2n \ge 2 points, 1≤k<n1 \le k < n, and the dissimilarity σ\sigma. For a point vv and u≠vu \ne v write πv(u)=(σ(v,u),u)\pi_v(u) = (\sigma(v, u), u), and order these pairs lexicographically. This is a strict total order on the n−1n - 1 points u≠vu \ne v; let rankv(u)∈{1,…,n−1}\mathrm{rank}_v(u) \in \{1, \ldots, n - 1\} be the position of uu in it. Write max⁡B[v]\max B[v] for the largest πv(u)\pi_v(u) with u∈B[v]u \in B[v].

Lemma 1 (valid lists). At all times each B[v]B[v] holds kk distinct points other than vv, each stored with σ(v,u)\sigma(v, u), and max⁡B[v]\max B[v] never increases.

Proof. Line L1 picks kk distinct points u≠vu \ne v and stores σ(v,u)\sigma(v, u). The lists change only in UPDATE(B[u1],u2,l)(B[u_1], u_2, l) with l=σ(u1,u2)l = \sigma(u_1, u_2) and u2≠u1u_2 \ne u_1 (line L8). It inserts only if u2∉B[u1]u_2 \notin B[u_1], so the points stay distinct and different from u1u_1, it replaces one entry, so the size stays kk, and it stores the correct distance. It inserts only if πu1(u2)<max⁡B[u1]\pi_{u_1}(u_2) < \max B[u_1], and it removes the entry of that maximum. The other k−1k - 1 entries are below the old maximum, and so is the new one, so the new maximum is smaller than the old one. □\square

Lemma 2 (termination). Let Φ=∑v∑u∈B[v]rankv(u)\Phi = \sum_v \sum_{u \in B[v]} \mathrm{rank}_v(u). Each update lowers Φ\Phi by at least 1, and Φ\Phi starts at most nk(n−1−k)n k (n - 1 - k) above its least possible value. So there are at most nk(n−1−k)n k (n - 1 - k) updates. With δ=0\delta = 0 there are at most nk(n−1−k)+1n k (n - 1 - k) + 1 rounds, and with δ>0\delta > 0 at most (n−1−k)/δ+1(n - 1 - k)/\delta + 1.

Proof. An update of B[u1]B[u_1] replaces the entry of rank ranku1\mathrm{rank}_{u_1} of the maximum by a point of smaller rank, so Φ\Phi drops by at least 1. For one list, the sum of kk distinct ranks in {1,…,n−1}\{1, \ldots, n - 1\} lies between 1+⋯+k1 + \cdots + k and (n−k)+⋯+(n−1)(n - k) + \cdots + (n - 1), which differ by k(n−1−k)k(n - 1 - k); summing over the nn lists bounds the total decrease, and so the number of updates. Every round except the last one has c>δnkc > \delta n k updates by line L11. With δ=0\delta = 0 that means at least one update, which gives the first bound on rounds. With δ>0\delta > 0, if there are TT rounds, then (T−1)δnk<nk(n−1−k)(T - 1)\delta n k < n k (n - 1 - k), so T<(n−1−k)/δ+1T < (n - 1 - k)/\delta + 1. □\square

For local optimality, assume no sampling (s≥s \ge every list size, so lines L4 and L7 take everything) and δ=0\delta = 0. Call an entry an occurrence of a point uu in a list B[w]B[w], from the moment UPDATE or line L1 puts it there until it is removed. An entry is flagged new when it is created, and only line L4 changes its flag, to old.

Lemma 3 (a compared pair stays settled). Say that (u1,u2)(u_1, u_2) is settled when u2∈B[u1]u_2 \in B[u_1] or πu1(u2)>max⁡B[u1]\pi_{u_1}(u_2) > \max B[u_1]. After line L10 compares u1u_1 and u2u_2, both (u1,u2)(u_1, u_2) and (u2,u1)(u_2, u_1) are settled, and they stay settled until the end.

Proof. Take (u1,u2)(u_1, u_2); the other pair is symmetric, since line L10 calls UPDATE in both directions. Right after UPDATE(B[u1],u2,l)(B[u_1], u_2, l), either u2∈B[u1]u_2 \in B[u_1] (it was there or it was inserted), or the insertion was refused with u2∉B[u1]u_2 \notin B[u_1] and πu1(u2)≥max⁡B[u1]\pi_{u_1}(u_2) \ge \max B[u_1]; the maximum is πu1(w)\pi_{u_1}(w) for some w≠u2w \ne u_2, so the inequality is strict. Now consider a later change of B[u1]B[u_1]. If u2∉B[u1]u_2 \notin B[u_1], the maximum does not increase by Lemma 1, so the inequality stays. If u2∈B[u1]u_2 \in B[u_1] and it is not the removed entry, u2u_2 stays. If u2u_2 is removed, it was the maximum, the other k−1k - 1 entries were below πu1(u2)\pi_{u_1}(u_2), and the new one is below it too, so afterwards πu1(u2)>max⁡B[u1]\pi_{u_1}(u_2) > \max B[u_1]. □\square

Lemma 4 (every entry is joined once). With no sampling, every entry that exists at the start of some round and is new then is flagged old in line L4 of that round; and every entry present at the end was new at the start of some round and has existed since then.

Proof. The first claim is line L4 with no sampling. For the second, take an entry present at the end. If line L1 made it, it was new at the start of round 1. Otherwise UPDATE made it during some round rr. Round rr is not the last round, since the last round has c=0c = 0 and so no update. So round r+1r + 1 exists, and at its start the entry exists (it is present at the end, and an entry once removed is gone) and is still new, since only line L4 changes flags and line L4 of round rr ran before the entry existed. □\square

Theorem (local optimality). With no sampling and δ=0\delta = 0, at the end, for every vv and all distinct u1,u2∈Nˉ[v]=B[v]∪R[v]u_1, u_2 \in \bar N[v] = B[v] \cup R[v], the pair (u1,u2)(u_1, u_2) is settled.

Proof. Membership ui∈Nˉ[v]u_i \in \bar N[v] is witnessed by an entry eie_i: the entry of uiu_i in B[v]B[v], or the entry of vv in B[ui]B[u_i]. Both entries are present at the end. By Lemma 4, eie_i was new at the start of some round tit_i and has existed since; let tit_i be that round. Rename so that t1≤t2t_1 \le t_2. Consider round t2t_2. At its start, e2e_2 is new, so in line L4 (if e2e_2 is in B[v]B[v]) or line L5 then L7 (if e2e_2 is the entry of vv in B[u2]B[u_2]), u2u_2 joins new[v]new[v]. Also e1e_1 exists at the start of round t2t_2. If t1=t2t_1 = t_2, it is new and u1u_1 joins new[v]new[v] the same way. If t1<t2t_1 < t_2, line L4 of round t1t_1 made it old and it has stayed old, so line L3 or lines L5 and L7 put u1u_1 in old[v]old[v]. Either way u2∈new[v]u_2 \in new[v] and u1∈new[v]∪old[v]u_1 \in new[v] \cup old[v] with u1≠u2u_1 \ne u_2, so line L8 compares them in round t2t_2, and by Lemma 3 the pair (u1,u2)(u_1, u_2) is settled from then to the end. □\square

Corollary. With no sampling and δ=0\delta = 0, at the end, if v≠wv \ne w, w∉B[v]w \notin B[v], and v,w∈Nˉ[u]v, w \in \bar N[u] for some uu, then πv(w)>max⁡B[v]\pi_v(w) > \max B[v]: no neighbor of a neighbor of vv, in the graph of all lists, could enter B[v]B[v].

Proof. Apply the theorem with center uu, u1=vu_1 = v and u2=wu_2 = w. Since w∉B[v]w \notin B[v], the pair is settled by the second clause. ■\blacksquare

Back to NN-Descent