Show HN: HNSW index for vector embeddings in approx 500 LOC
Published on: 2025-05-08 20:43:14
HNSW - Hierarchical Navigable Small Worlds
Nearest neighbor search for vector embeddings
Single header, modern C++.
Approximately 500 lines of code.
Fast (uses Eigen for SIMD acceleration of distance calculations).
What is HNSW?
// Level 2 * * * * // Level 1 * * * * * * ** * * // Level 0 * ** * * * * * ** * * * * ** ** ** ** * * * * * * *
HNSW is a graph structure that consists of levels that are more sparsely populated at the top and more densely populated at the bottom. Nodes within a layer have connections to other nodes that are near them on the same level. When a node is inserted a random level is picked and the node is inserted there. It is also inserted into all levels beneath that level down to 0.
When searches arrive they start at the top and search that level (following connections) until they find the closest node in the top level. The search then descends and keeps searching nearby nodes. As the search progresses the code keeps track of the K nearest nodes it has se
... Read full article.