Hierarchical Navigable Small World graphs for Approximate Nearest Neighbor search
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: logarithmic search time even for millions of points, with high recall.
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: