# HNSW Visualised

**AI Engineering Visualised** · Retrieval · Approximate search · Interactive explainer · Updated 2026-09-09

HNSW, hierarchical navigable small world, is the graph index most vector databases use for approximate nearest neighbour search. A query enters at a sparse top layer and descends, hopping between neighbours, to find close vectors in a fraction of the comparisons exact search needs.

> See a search move down a 3D HNSW index and find nearby vectors. Change the query or search breadth, then compare the result with exact search.

HNSW stands for Hierarchical Navigable Small World. It searches a stack of proximity graphs: sparse upper layers guide the route, then the base layer searches among all indexed vectors. Choose a query and press Play to follow each expansion and descent.

Search breadth (efSearch) controls how many promising candidates the base-layer search retains. It is not a cap on distance calculations. A wider search can find better neighbours at the cost of more work, but cannot guarantee perfect recall. At the end, compare the three returned neighbours with exact search and see how many unique vectors were scored. The separate exact comparison is excluded from that work count.

This example uses 36 fixed four-dimensional vectors, a sparse index with M = 2 and entry point N01. Distances use all four dimensions. The plot uses two coordinates plus organisational height for layers, so screen proximity can be misleading. Repeated nodes across layers represent the same vector. Fixed levels and simple neighbour selection make this a teaching model, not a production benchmark.

## The maths

### How many true neighbours did we find?

```latex
\operatorname{recall@}3=\frac{|A_3\cap E_3|}{3}
```

A is the returned set and E is the exact nearest-neighbour set. Finding two of the exact top three gives about 67% recall for this query. The final comparison shows both sets; exact search scores all 36 vectors separately.

## Sources

- [Malkov and Yashunin: the original HNSW paper](https://arxiv.org/abs/1603.09320)
- [hnswlib: search and construction parameters](https://github.com/nmslib/hnswlib/blob/master/ALGO_PARAMS.md)

## Related terms

- [Vector database](https://understandingdata.com/context-engineering-dictionary/vector-database/)
- [Embeddings](https://understandingdata.com/context-engineering-dictionary/embeddings/)

## More visualisations

- [Embedding space](https://understandingdata.com/ai-engineering-visualised/embedding-space/)
- [Hybrid Retrieval Visualised](https://understandingdata.com/ai-engineering-visualised/hybrid-retrieval/)
- [GraphRAG Visualised](https://understandingdata.com/ai-engineering-visualised/graphrag/)

---

Source: https://understandingdata.com/ai-engineering-visualised/vector-search/
From AI Engineering Visualised by James Phoenix, Understanding Data. Published 2026-09-09.
