Proofs: Navigable small world graph
These are the human proofs of Navigable small world graph, at full length.
The NSW graph stays connected, its search returns the best points it evaluates, and a full beam is exact full
This proof shows: correctness.
We use the names of the pseudocode on the page. The distance is any function with ; only comparisons of its values are used. Ties between equal distances are broken by a fixed order of the points, so "nearest" and "largest distance" are well defined; the implementation compares pairs (distance, index).
Lemma 1 (the graph). After any sequence of insertions of , (I1) the graph is undirected, (I2) was linked at its insertion to distinct earlier points, and (I3) the graph is connected.
Proof. Induction on the number of insertions. Line L11 inserts with no links, and the three statements hold. Let and assume them before is inserted. Line L12 calls SEARCH on the graph of with and . By Theorem 1 below, it returns the nearest of the set of the points it evaluates, all distinct and all earlier than . The graph of the earlier points is connected by the induction hypothesis, so by Lemma 2 below , and the search returns points. Lemma 2 and Theorem 1 concern a single search on a fixed graph and do not use this lemma, so there is no circularity. Line L13 adds an undirected edge from to each of them, so (I1) and (I2) hold. Since , is adjacent to an earlier point, which is connected to all other earlier points by the induction hypothesis, so (I3) holds.
Lemma 2 (the search fills its beam). Let the graph be connected with points, and let SEARCH run with and at least one entry point. Then it evaluates at least points.
Proof. Suppose it stops having evaluated fewer than points. Since every evaluated point that is not inserted into fails the test of line L9, which passes whenever , and points leave only when , the size of is the number of evaluated points as long as that number is at most . So at the end , and line L6 never stopped the loop: the loop ended with . Every evaluated point was inserted into (in line L3, or in line L9 since at that time) and later removed by line L5 and expanded by line L7. So every neighbor of an evaluated point is evaluated. The evaluated set contains an entry point and is closed under taking neighbors, so by connectivity it is the whole graph, and it has points, a contradiction.
Lemma 3 (the beam holds the best seen). At every moment of SEARCH, is the set of the points nearest to in the set of the points evaluated so far.
Proof. Initially . Each evaluation, in line L2 or L8, adds one point to . Assume the claim before it. If , then , , and is inserted (the test of line L9 passes, and line L3 always inserts), so , as claimed. If , then holds the nearest points of . If is nearer than the farthest point of , line L9 inserts it and removes that farthest point, which leaves the nearest points of . Otherwise is not among the nearest of , and is unchanged, as needed. Line L2 of the implementation applies the same rule, insertion followed by removal of the farthest point when , which is the same update.
Theorem 1 (the answer). SEARCH returns the points nearest to among the set of the points it evaluates, in order of distance, and it terminates.
Proof. Each point is evaluated at most once, since it is marked when evaluated and line L7 skips marked points, and each point enters at most once, when it is evaluated. So the loop of line L4 runs at most times, and each iteration scans one adjacency set. At the end, Lemma 3 gives that is the set of the nearest points of . Since , the nearest points of are the nearest points of , and line L10 returns them in order.
Theorem 2 (a full beam is exact). If the graph is connected, and there is at least one entry point, then SEARCH evaluates all points and returns the nearest neighbors of .
Proof. By Lemma 2 with it evaluates at least points, so is the whole set, and Theorem 1 gives the claim. By Lemma 1, the graph built by INSERT is connected.