Hierarchical Navigable Small World graphs for Approximate Nearest Neighbor search
New to HNSW? Read the article first: HNSW explained: how vector search finds nearest neighbors fast
HNSW builds a multi-layer graph. Think of it like a city's transport network: highways (top layers) let you travel far quickly, then local roads (bottom layer) take you exactly where you need to go.
Result: roughly logarithmic search cost even for millions of points, with high recall.
Changing Layers, M, efConstruction or the distance metric rebuilds the index from the same points, so you can compare settings on identical data.
Exact nearest neighbor search in high-dimensional spaces (like embeddings from LLMs) is extremely slow β you'd have to compare every point. HNSW trades a tiny bit of accuracy for massive speed gains: