---
title: 'HNSW: Efficient Graph-Based ANN Search'
url: https://www.emergentmind.com/topics/hnsw-algorithm
type: topic
---

# HNSW: Efficient Graph-Based ANN Search

The Hierarchical Navigable Small World (HNSW) algorithm is a graph-based approach for approximate nearest neighbor (ANN) search that exploits hierarchical proximity graphs to achieve efficient, scalable retrieval in general metric spaces. HNSW is characterized by a multi-layer structure in which each layer is an independently navigable small-world graph over a subset of the data points. Search complexity is near-logarithmic with high empirical recall, making HNSW the prevailing paradigm in dense vector and AI retrieval workloads.

## 1. Core Structure and Construction of HNSW

HNSW organizes data points into a hierarchy of proximity graphs—layers indexed by $\ell=0,1,\dots,L_{\max}$, with layer 0 covering all points and higher layers containing sparser samples. Each point $x$ is assigned a random maximum layer $\ell(x) \sim$ geometric distribution, typically $\Pr[\ell(x)\ge k] = e^{-k/m_L}$, where $m_L$ controls the decay [1603.09320].

Insertion proceeds as follows:

1. For each new point $q$ with assigned top layer $\ell(q)$:
   - Greedy descent from the current global entry point at the highest layer down to layer $\ell(q)+1$, moving at each step to the nearest neighbor in the present layer.
   - In each layer from $\ell(q)$ down to 0, perform a local search (beam width $efConstruction$) to identify candidate neighbors.
   - Out of these candidates, select up to $M$ neighbors using the "heuristic pruning" procedure (relative neighborhood criterion): retain candidates $c$ only if, for all chosen $s$, $d(q,c)<d(c,s)$. Edges are bidirectional.
2. Update the global entry point if the new point's level is highest.

The randomized level assignment induces a skip-list-like hierarchy. Upper layers facilitate global traversal via long-range links, while layer 0 is densely connected for local refinement. The average number of graph layers per data point is $O(\log N)$, and the total indexing cost is $O(N \log N)$ in practice [1603.09320], [2304.12139].

## 2. Search Procedure and Complexity

HNSW answers a $k$-NN query through two stages:

- **Hierarchical Greedy Descent:** Starting at the global entry point in the top layer, repeatedly move to the neighbor closest to the query $q$ until no closer neighbor is found; descend to the next layer and repeat.
- **Layer-0 Best-First Search:** At the base layer, maintain a candidate max-heap of size $efSearch$; iteratively expand the best candidate, scan its neighbors, and insert new candidates into the heap until convergence.

The search complexity is empirically $O(\log N)$ for random or embedding-based workloads, saturating to sublinear scaling even for millions of points. The memory footprint is $O(N M \log N)$, though most edges reside in the base layer [1603.09320], [2304.12139].

## 3. Neighbor Selection, Connectivity, and Pruning

Neighbor selection critically uses the diverse-neighbors rule (relative neighborhood heuristic)—no two chosen neighbors "block" each other, ensuring the graph remains well-connected and avoids local cluster traps [1603.09320]. For construction, increasing $M$ or $efConstruction$ yields denser graphs and better recall but raises memory and index time linearly; the heuristic is essential for good performance on highly clustered and high-dimensional data [1603.09320], [2405.17813].

The standard HNSW (pruning parameter $\alpha=1$ in RobustPrune) lacks theoretical shortcut guarantees: pathological clustering or adversarial insertion order can trap greedy search in suboptimal regions, causing empirical worst-case query time $\Omega(N)$ on low-dimensional hard instances [2310.19126]. Some extensions (DiskANN) employ $\alpha>1$ pruning to establish theoretical shortcut properties and guarantee polylogarithmic search [2310.19126].

## 4. Hierarchical Structure, the Hub-Highway Hypothesis, and Alternatives

The effectiveness of HNSW's hierarchy in modern high-dimensional ANN workloads is debated. Recent studies show that for large-scale, high-dimensional data ($d\geq96$), a flat navigable small world graph (FlatNav) matches HNSW's latency and recall while reducing memory usage by 38% [2412.01940]. The underlying mechanism is the "Hub Highway Hypothesis": high-dimensional embedding spaces naturally yield a set of "hub" nodes that are k-nearest-neighbors for many points and form a densely interconnected "highway." Greedy search rapidly enters this subgraph, obviating the need for explicit hierarchy—multi-layer skipping does not improve traversal.

Empirical measurements demonstrate:
- No latency difference ($\Delta T_{\rm query} \approx 0$) between HNSW and FlatNav on all major benchmarks [2412.01940].
- The majority of beam-search visits occur on "hub highway" nodes (50–70% in early expansion).

This suggests that explicit hierarchy is dispensable for high-$d$ ANN—explicitly constructing or favoring hubs could provide equivalent performance at lower cost [2412.01940].

## 5. Parameter Sensitivity and Impact of Data Characteristics

HNSW performance depends on:
- **Intrinsic dimensionality:** Data with high intrinsic or local intrinsic dimensionality (LID) requires larger $M$; recall degrades linearly as LID increases [2405.17813]. LID is computed as
  $$
  \mathrm{LID}(x) = \Bigl(\frac{1}{k-1}\sum_{i=1}^{k-1} \log \frac{d_k}{d_i}\Bigr)^{-1}
  $$
  [2501.13992].
- **Insertion order:** Inserting high-LID or category-diverse points early ("annealing") can boost recall by up to 12 percentage points. Descending-LID order provides +2.6pp (HNSWLib) and +6.2pp (FAISS) average recall@10 over random, with swings up to 7.7pp in real-world image retrieval [2405.17813].
- **Default parameters:** $M=16$, $efConstruction=128$, $efSearch=40$ are typical in deployed systems [2405.17813]. Larger values improve recall but incur trade-offs in speed and memory.

## 6. Recent Variants and Practical Extensions

### Dual-Branch HNSW++ and LID-Driven Optimization

HNSW++ partitions the dataset into two branches, each with its own hierarchy, merging at layer 0. Layer assignments preferentially select high-LID points for upper layers. The bridge-building technique allows direct traversal from high layers to layer 0 ("skip bridges") when searches reach sparse regions [2501.13992]. Key findings:
- Recall enhancement: +18% (NLP), +30% (CV datasets).
- Construction time reduction: 16–20% vs vanilla HNSW.
- Dual-branch and LID insertion mitigate cluster disconnections and local minima.

### Disaggregated Memory and d-HNSW

d-HNSW adapts HNSW for RDMA disaggregated architectures. Optimal performance is achieved via meta-HNSW representative caching, RDMA-friendly contiguous data layout, and batch/deduplicated loading [2505.11783]. Benchmarks on SIFT1M at recall 0.87 demonstrate up to 117× latency reduction over naïve memory layouts.

### Predicate-Agnostic DBMS Filtering

NaviX extends HNSW in graph DBMSs to robustly support filtered kNN queries. It implements adaptive, locally selective candidate expansion to maintain search efficiency and recall under varying filter selectivities and correlation structures [2506.23397].

### Real-Time Updates, Deletions, and Unreachable Points

MN-RU (Mutual-Neighbor Replaced_Update) improves HNSW update efficiency and suppresses the growth of unreachable points during dynamic deletions/insertions [2407.07871]. Key mechanisms include restricted repair to mutual neighbors and backup dual-index management. MN-RU delivers 2–4× faster updates and stable recall under high-frequency modification workloads.

## 7. Applications and Practical Optimization

HNSW is the default ANN engine in Lucene (via Anserini), Weaviate, Milvus, FAISS, and other vector DBs [2304.12139], [2405.17813]. In Lucene, careful tuning of $M=16$, $efConstruction=100$, $efSearch=1000$ strikes a trade-off between recall (MRR@10 ~ 0.38) and QPS (30+). Multi-threaded indexing and segment merging ("optimize") are essential for low-latency deployments. Sparse vector support and GPU variants (SONG) further accelerate HNSW in large-scale EBR (embedding-based retrieval) [2306.07607].

**Memory-access optimization:** Post-construction graph reordering (Gorder, RCM) reorganizes memory layout, reducing cache misses and query time by up to 40% at high recall [2104.03221]. Reordering is highly recommended for large, static indices.

## References

- [1603.09320]: Malkov & Yashunin, Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs
- [2310.19126]: Worst-case Performance of Popular Approximate Nearest Neighbor Search Implementations: Guarantees and Limitations
- [2412.01940]: Down with the Hierarchy: The 'H' in HNSW Stands for "Hubs"
- [2501.13992]: Dual-Branch HNSW Approach with Skip Bridges and LID-Driven Optimization
- [2505.11783]: Efficient Vector Search on Disaggregated Memory with d-HNSW
- [2506.23397]: NaviX: A Native Vector Index Design for Graph DBMSs With Robust Predicate-Agnostic Search Performance
- [2407.07871]: Enhancing HNSW Index for Real-Time Updates: Addressing Unreachable Points and Performance Degradation
- [2304.12139]: Anserini Gets Dense Retrieval: Integration of Lucene's HNSW Indexes
- [2306.07607]: Practice with Graph-based ANN Algorithms on Sparse Data: Chi-square Two-tower model, HNSW, Sign Cauchy Projections
- [2104.03221]: Graph Reordering for Cache-Efficient Near Neighbor Search
- [2405.17813]: The Impacts of Data, Ordering, and Intrinsic Dimensionality on Recall in Hierarchical Navigable Small Worlds

Source: https://www.emergentmind.com/topics/hnsw-algorithm