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 points, , and the dissimilarity . For a point and write , and order these pairs lexicographically. This is a strict total order on the points ; let be the position of in it. Write for the largest with .
Lemma 1 (valid lists). At all times each holds distinct points other than , each stored with , and never increases.
Proof. Line L1 picks distinct points and stores . The lists change only in UPDATE with and (line L8). It inserts only if , so the points stay distinct and different from , it replaces one entry, so the size stays , and it stores the correct distance. It inserts only if , and it removes the entry of that maximum. The other entries are below the old maximum, and so is the new one, so the new maximum is smaller than the old one.
Lemma 2 (termination). Let . Each update lowers by at least 1, and starts at most above its least possible value. So there are at most updates. With there are at most rounds, and with at most .
Proof. An update of replaces the entry of rank of the maximum by a point of smaller rank, so drops by at least 1. For one list, the sum of distinct ranks in lies between and , which differ by ; summing over the lists bounds the total decrease, and so the number of updates. Every round except the last one has updates by line L11. With that means at least one update, which gives the first bound on rounds. With , if there are rounds, then , so .
For local optimality, assume no sampling ( every list size, so lines L4 and L7 take everything) and . Call an entry an occurrence of a point in a list , 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 is settled when or . After line L10 compares and , both and are settled, and they stay settled until the end.
Proof. Take ; the other pair is symmetric, since line L10 calls UPDATE in both directions. Right after UPDATE, either (it was there or it was inserted), or the insertion was refused with and ; the maximum is for some , so the inequality is strict. Now consider a later change of . If , the maximum does not increase by Lemma 1, so the inequality stays. If and it is not the removed entry, stays. If is removed, it was the maximum, the other entries were below , and the new one is below it too, so afterwards .
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 . Round is not the last round, since the last round has and so no update. So round 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 ran before the entry existed.
Theorem (local optimality). With no sampling and , at the end, for every and all distinct , the pair is settled.
Proof. Membership is witnessed by an entry : the entry of in , or the entry of in . Both entries are present at the end. By Lemma 4, was new at the start of some round and has existed since; let be that round. Rename so that . Consider round . At its start, is new, so in line L4 (if is in ) or line L5 then L7 (if is the entry of in ), joins . Also exists at the start of round . If , it is new and joins the same way. If , line L4 of round made it old and it has stayed old, so line L3 or lines L5 and L7 put in . Either way and with , so line L8 compares them in round , and by Lemma 3 the pair is settled from then to the end.
Corollary. With no sampling and , at the end, if , , and for some , then : no neighbor of a neighbor of , in the graph of all lists, could enter .
Proof. Apply the theorem with center , and . Since , the pair is settled by the second clause.