Proofs: Hierarchical navigable small world
These are the human proofs of Hierarchical navigable small world, at full length.
Properties of HNSW search, the level law, and exactness without pruning full
This proof shows: correctness.
Let be a distance on a set of elements, with . HNSW is approximate, so we prove three properties: what SEARCH-LAYER returns, the law of the levels, and that the search is exact in a limiting setting.
Lemma 1 (SEARCH-LAYER). SEARCH-LAYER ends, and it returns the elements nearest to among the set of elements ever added to , where .
Proof. An element enters at most once, at line L1 or line L5, and enters only when it enters . So receives at most as many elements as there are elements on layer , and the loop of line L2, which removes one element of per pass, ends. is a max-heap with at most elements. An element is removed from only when , and the removed one is the farthest. So after each offer holds the nearest elements of , by induction on the offers, exactly as for a bounded heap in top-k selection.
Lemma 2 (levels). If is uniform in and with , then for every integer , and .
Proof. if and only if , that is, , which has probability . Then .
So an element lies on layers in expectation, and the expected number of elements on layer is .
Lemma 3 (reach). If , where is the number of elements on layer , then SEARCH-LAYER adds to every element reachable from along the directed links .
Proof. Since , line L1 cuts nothing, and the removal of line L6 never happens: would need more than elements. So every element of is in . The test of line L6 holds for every new , since unless , in which case all elements are in and there is no new . So every element of enters . An element taken from is in , so , and line L4 never breaks. So every element of is expanded at line L5. Then is closed under the links: if and , then . A set that contains and is closed under the links contains everything reachable from .
Lemma 4 (connectivity). Suppose no list is ever cut at line L15 (for example ) and SELECT returns a nonempty set whenever is not empty. Then after any number of insertions, every element is reachable from every other along layer-0 links.
Proof. Without cuts, line L14 adds each link in both directions and no link is removed, so the layer-0 links form an undirected graph. The first element alone is connected. When a later element is inserted, layer 0 is among its layers, and the search of line L14 on layer 0 starts from a nonempty set, so is not empty and SELECT links to at least one earlier element. The simple rule keeps the nearest element of ; the heuristic keeps it too, since the set of kept elements is empty when it is scanned. So the graph stays connected.
Theorem. Under the conditions of Lemma 4, K-NN-SEARCH with returns elements whose distances to are the smallest.
Proof. Line L8 gives a nonempty entry set on layer 0. By Lemma 4 every element is reachable from it, and by Lemma 3 with every element is added to and, by the proof of Lemma 3, to . So is the set of all elements, and by Lemma 1 the returned holds the elements in order of distance. Line L10 returns the first .