---
title: Hierarchical Navigable Small World (HNSW)
url: https://www.emergentmind.com/topics/hierarchical-navigable-small-world-hnsw
type: topic
---

# Hierarchical Navigable Small World (HNSW)

Hierarchical Navigable Small World (HNSW) is a graph-based indexing structure designed for efficient, scalable approximate nearest neighbor search (ANNS), widely adopted in both research and industrial retrieval systems. HNSW leverages a hierarchy of sparse small-world graphs and employs greedy, layered search algorithms to achieve sub-linear query complexity and high recall, particularly in high-dimensional vector spaces. Its architecture and algorithms have inspired a substantial lineage of extensions, optimizations, and practical deployments across billions of data points.

## 1. Layered Small-World Graph Structure and Construction

HNSW maintains a multi-layer hierarchy $G_0, G_1, ..., G_{L_{max}}$ where $G_\ell$ is a proximity graph over a geometrically sampled subset of the data. For each point $p$, the maximum layer $\ell(p)$ is drawn from a geometric distribution:
\[
\Pr[\ell(p) = l] = p \cdot e^{-\zeta l}
\]
where $p \approx 1/e^\zeta$ in practice [2407.07871]. Layer 0 contains all $n$ data points, with higher layers containing progressively sparser subsets, forming a skip-list-like hierarchy.

Index construction proceeds as follows:
- A new point $p$ is assigned a random $\ell(p)$.
- Starting from the global entry point at $L_{max}$, greedy routing is performed down to $\ell(p)+1$ to locate appropriate insertion points.
- At each layer $\ell$ from $\ell(p)$ down to 0, a beam search (best-first search with candidate list size $ef_{construction}$, typically 100–200) collects $W$ candidate neighbors. The selection heuristic (approximate relative-neighborhood graph) prunes these to at most $M$ bidirectional connections, maintaining connectivity and diversity [1603.09320].

The established neighbor-selection rule is critical for robust performance in clustered or low-dimensional data [1603.09320, 2405.17813].

## 2. Hierarchical Search Algorithm and Computational Properties

A $k$-NN query $q$ initiates a greedy descent from the entry point in the highest layer, locally moving to the closest neighbor at each level:
- For $\ell$ descending from $L_{max}$ to 1, $ep \leftarrow \operatorname{argmin}_{x\in ep-\text{neighbors}}\; d(q, x)$
- At layer 0, a best-first (beam) search is executed with candidate pool size $ef$; the closest $k$ neighbors are returned.

Typical metric is Euclidean, $d(q,x) = \|q - x\|_2$ [2407.07871]. The candidate pool size $ef$ (exploration factor) directly governs recall and query cost; larger $ef$ increases recall, but incurs higher latency and memory usage.

Empirically and theoretically, HNSW search runtime is sub-linear:
\[
\text{Average-case query cost:}\quad O(\log n)
\]
and often observed as $O(n^{1/2})$ in complex or high-dimensional settings [2407.07871, 2405.17813]. Insertion cost is
\[
O(\ell(p) \cdot (ef_{construction} \log ef_{construction} + M \log M))
\]
with space complexity $O(n\cdot M)$ edges [2407.07871].

## 3. Impact of Data Properties, Insertion Order, and Intrinsic Dimensionality

Search quality and recall in HNSW are sensitive to data intrinsic dimensionality and insertion sequence. The pointwise Local Intrinsic Dimensionality (LID):
\[
\mathrm{LID}(x) = \left( \frac{1}{k-1} \sum_{i=1}^{k-1} \ln\frac{d_k(x)}{d_i(x)} \right)^{-1}
\]
quantifies the local growth rate of the neighborhood volume. High-LID points are harder to connect and traverse, affecting final recall [2405.17813].

Ordered insertion based on descending LID improves recall systematically (up to 12 percentage points in benchmarks) compared to ascending or chronological order. Category-aware or buffer-interleaved insertion can further mitigate recall degradation in real-world batch updates. High global intrinsic dimensionality leads to recall collapses—this must be offset by increasing $M$, $ef_{construction}$, and $ef_{search}$ as needed [2405.17813].

## 4. Update Operations and Unreachable Points Phenomenon

Frequent updates—deletions, insertions, overwrites—trigger the unreachable points phenomenon: points become isolated in all layers ($\forall\;\ell:\;degree_{in,G_\ell}(v) = 0$), rendering them inaccessible to search.

Standard "replaced_update" operation involves:
- Marking a node as deleted.
- Collecting one-hop and two-hop neighbor candidates.
- Pruning each candidate's neighbor list.
- Writing new data over the deleted node.

This process may inadvertently disconnect nodes if their only incoming edge is lost, resulting in persistent unreachable subgraphs. On SIFT ($n=1$M), iteratively killing & reinserting 5% for 3,000 rounds grows the unreachable fraction to 3–4%, with recall@1 dropping ≈3% [2407.07871]. Increasing $ef$ during search does not recover lost accuracy.

The MN-RU algorithm mitigates the phenomenon by restricting reconnections to only one-hop neighbors that lose links, using an $\alpha$-RNG pruning with complexity $O(M^2)$, markedly faster than previous $O(M^3)$ two-hop repairs [2407.07871].
- Update efficiency is improved by $2$–$4\times$ over HNSW-RU.
- Unreachable-point growth is suppressed ($<0.2\%$ after 200 updates).
- Recall is kept within $0.5$–$1\%$ of a freshly built index even after hundreds of updates.

A backup index on unreachable points ("dualSearch") can restore overall recall with minimal overhead.

## 5. Algorithmic Extensions and Optimizations

Recent extensions have focused on mitigating local optima, cluster disconnections, and costly construction in high-dimensional or clustered data:

- Dual-Branch HNSW ("HNSW++"): splits the index into two parallel branches, each with its own entry point, converging at layer 0. Nodes are assigned to branches and layers using normalized LID, with high-LID nodes prioritized for upper layers—this improves cluster connectivity and recall, reducing cluster disconnections [2501.13992]. Search is performed in both branches, merging the candidate sets for final results.

- Skip-Bridges: allow the search to bypass intermediate sparse layers when a node's LID and proximity to the query exceed given thresholds. This further reduces effective search depth and accelerates recall attainment.

- LID-based Layer Assignment: stratifies insertion order and connectivity by local dimensionality, yielding recall improvements of +18% (NLP) and +30% (CV) over baseline HNSW in datasets [2501.13992].

Construction time is reduced by up to 20% over standard HNSW; empirical ablation studies demonstrate the centrality of LID-based assignment to these gains.

## 6. Distributed, Merging, and Disaggregated Memory Architectures

Scalable deployments of HNSW across memory boundaries (e.g., disaggregated memory in data centers) require maintaining the global graph structure:

- SHINE stores the entire HNSW index across memory nodes (MNs), preserving all edges via remote pointers, accessed by compute nodes (CNs) over RDMA. No graph partitioning occurs, so recall matches single-machine HNSW [2507.17647].

- Efficient caching mechanisms are required—only vectors (not neighbor lists) are cached per CN. Logical index partitioning via $k$-means clusters ensures that cache-segmentation penalties are minimized, and adaptive query routing (via an oracle and load balancing) allows up to $2$–$3\times$ higher query throughput than with naive cache-only or no-cache strategies.

- Distributed HNSW merging involves efficiently combining separately built HNSW graphs. Algorithms such as Naive Graph Merge (NGM), Intra Graph Traversal Merge (IGTM), and Cross Graph Traversal Merge (CGTM) operate via iterative vertex selection, candidate neighbor collection, neighborhood construction (via RNG/k-NN filtering), and information propagation. IGTM achieves $\sim70\%$ fewer distance computations than naive methods at the same recall, supporting incremental, compaction, and distributed indexing scenarios [2505.16064].

## 7. Practical Tuning, Adaptive Exploration, and Benchmarking

Operational best practices emphasize parameter selection:
- $M=16$ is widely used; increasing $M$ (e.g., to $32$) marginally increases recall (<1%) but slows construction and increases space.
- $ef_{construction}=100$–$200$ balances build time and recall.
- $ef_{search}$ should be tuned for latency/recall trade-off; $200$ suffices for ≈90% recall, $1000$ for >99% [2409.06464].

Adaptive-ef (Ada-ef) introduces statistically principled data- and query-driven determination of $ef$: fitting the Full Distance List (FDL) between the query and database to a normal distribution allows theoretical estimation of required $ef$ to meet desired recall $R$ [2512.06636]. The algorithm dynamically scores queries and assigns minimal $ef$ for each, producing up to $4\times$ lower latency and robust recall guarantees, even under distribution shift, with drastically reduced offline computation and memory compared to learning-based adaptive strategies.

For collections under $100$K, brute-force (flat) search may be preferable for rapid prototyping; above $1$M, HNSW becomes the dominant paradigm, yielding $10$–$100\times$ query speedup over flat methods with negligible nDCG loss. Quantization (int8) is low risk for speed increase; the nDCG@10 loss is $<0.02$ [2409.06464].

## References

- [2407.07871] Enhancing HNSW Index for Real-Time Updates: Addressing Unreachable Points and Performance Degradation
- [2501.13992] Dual-Branch HNSW Approach with Skip Bridges and LID-Driven Optimization
- [2405.17813] The Impacts of Data, Ordering, and Intrinsic Dimensionality on Recall in Hierarchical Navigable Small Worlds
- [2412.01940] Down with the Hierarchy: The 'H' in HNSW Stands for "Hubs"
- [2505.16064] Three Algorithms for Merging Hierarchical Navigable Small World Graphs
- [2507.17647] SHINE: A Scalable HNSW Index in Disaggregated Memory
- [2409.06464] Operational Advice for Dense and Sparse Retrievers: HNSW, Flat, or Inverted Indexes?
- [1603.09320] Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs
- [2512.06636] Distribution-Aware Exploration for Adaptive HNSW Search

Source: https://www.emergentmind.com/topics/hierarchical-navigable-small-world-hnsw