Algorithm Search⌘K

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 δ\delta be a distance on a set of elements, with δ(x,x)=0\delta(x, x) = 0. 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(q,E,ef,l)(q, E, ef, l) ends, and it returns the min⁡(ef,∣O∣)\min(ef, |O|) elements nearest to qq among the set OO of elements ever added to WW, where E⊆O⊆VE \subseteq O \subseteq V.

Proof. An element enters VV at most once, at line L1 or line L5, and enters CC only when it enters VV. So CC receives at most as many elements as there are elements on layer ll, and the loop of line L2, which removes one element of CC per pass, ends. WW is a max-heap with at most efef elements. An element is removed from WW only when ∣W∣>ef|W| > ef, and the removed one is the farthest. So after each offer WW holds the min⁡(ef,∣O∣)\min(ef, |O|) nearest elements of OO, by induction on the offers, exactly as for a bounded heap in top-k selection. □\square

Lemma 2 (levels). If UU is uniform in (0,1](0, 1] and ℓ=⌊−ln⁡U/ln⁡M⌋\ell = \lfloor -\ln U / \ln M \rfloor with M≥2M \ge 2, then Pr⁡[ℓ≥j]=M−j\Pr[\ell \ge j] = M^{-j} for every integer j≥0j \ge 0, and E[ℓ]=1/(M−1)\mathbb{E}[\ell] = 1/(M - 1).

Proof. ℓ≥j\ell \ge j if and only if −ln⁡U/ln⁡M≥j-\ln U / \ln M \ge j, that is, U≤M−jU \le M^{-j}, which has probability M−jM^{-j}. Then E[ℓ]=∑j≥1Pr⁡[ℓ≥j]=∑j≥1M−j=1/(M−1)\mathbb{E}[\ell] = \sum_{j \ge 1} \Pr[\ell \ge j] = \sum_{j \ge 1} M^{-j} = 1/(M - 1). □\square

So an element lies on 1+1/(M−1)1 + 1/(M-1) layers in expectation, and the expected number of elements on layer jj is nM−jn M^{-j}.

Lemma 3 (reach). If ef≥nef \ge n, where nn is the number of elements on layer ll, then SEARCH-LAYER(q,E,ef,l)(q, E, ef, l) adds to VV every element reachable from EE along the directed links NlN_l.

Proof. Since ∣W∣≤n≤ef|W| \le n \le ef, line L1 cuts nothing, and the removal of line L6 never happens: WW would need more than ef≥nef \ge n elements. So every element of VV is in WW. The test of line L6 holds for every new ee, since ∣W∣<ef|W| < ef unless ∣W∣=n|W| = n, in which case all elements are in VV and there is no new ee. So every element of VV enters CC. An element cc taken from CC is in WW, so δ(q,c)≤δ(q,farthest of W)\delta(q, c) \le \delta(q, \text{farthest of } W), and line L4 never breaks. So every element of VV is expanded at line L5. Then VV is closed under the links: if c∈Vc \in V and e∈Nl(c)e \in N_l(c), then e∈Ve \in V. A set that contains EE and is closed under the links contains everything reachable from EE. □\square

Lemma 4 (connectivity). Suppose no list N0N_0 is ever cut at line L15 (for example M0≥n−1M_0 \ge n - 1) and SELECT returns a nonempty set whenever WW 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 xx is inserted, layer 0 is among its layers, and the search of line L14 on layer 0 starts from a nonempty set, so WW is not empty and SELECT links xx to at least one earlier element. The simple rule keeps the nearest element of WW; the heuristic keeps it too, since the set of kept elements is empty when it is scanned. So the graph stays connected. □\square

Theorem. Under the conditions of Lemma 4, K-NN-SEARCH(q,k,ef)(q, k, ef) with ef≥nef \ge n returns kk elements whose distances to qq are the kk 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 ef≥nef \ge n every element is added to VV and, by the proof of Lemma 3, to WW. So OO is the set of all elements, and by Lemma 1 the returned WW holds the min⁡(ef,n)=n\min(ef, n) = n elements in order of distance. Line L10 returns the first kk. ■\blacksquare

Back to Hierarchical navigable small world