Multi-layer proximity graph index for fast, high-recall approximate nearest-neighbor (ANN) search over high-dimensional vector embeddings (D = 64 to 4096).
Compact metadata for implementation and discovery.
Category
Searching
Difficulty
Advanced
Family
Approximate Nearest Neighbor (ANN)
Tier
Foundation
Status
Verified
Review
Source Checked
Sources
1
Formulas
2
Relationships
2
Code example
Ready
Visualization
Ready
Aliases
Hierarchical Navigable Small World, HNSW Graph ANN
Overview
Hierarchical Navigable Small World (HNSW) builds a hierarchy of navigable small-world proximity graphs where upper layers provide long-range skip links and lower layers refine local cosine or Euclidean neighborhoods. Unlike K-D Trees and axis-aligned spatial trees—which suffer from the curse of dimensionality above ~20 dimensions and degrade to O(N) brute-force scan—HNSW achieves logarithmic empirical query time and >95% recall on million-scale 768D and 1536D embedding vectors.
Core Concepts
navigable small world graphhierarchical skip layersbeam search (efSearch)cosine distance
Quick facts
Complexity
Runtime and memory behavior at a glance.
Best
O(log n)
Average
O(log n)
Worst
O(n)
Space
O(n * M)
Interactive Runtime & Memory Budget Calculator
Estimate wall-clock latency, operation count, and working memory for HNSW (Hierarchical Navigable Small World) across input scales.
Input Scale N:
JIT-compiled JS/TS (~350M primitive ops/sec)
Est. Average TimeO(log n)
95 ns
Sub-microsecond~33
Est. Worst-Case TimeO(n)
428.6 µs
60 FPS Frame-Safe (<16ms)~150.0k ops
Est. Working MemoryO(n * M)
1.53 MB
Standard Heap FitN=100,000
When to Use
Hierarchical Navigable Small World multi-layer proximity graph for fast approximate nearest neighbor (ANN) vector search in high-dimensional embedding spaces (e.g., 512D, 768D, 1536D cosine or Euclidean embeddings).
Use HNSW when searching high-dimensional embeddings (D > 20, such as 64D, 768D, or 1536D vectors).
Use it when sub-millisecond query latency and >95% Recall@10 are required on large vector collections.
When Not to Use
Avoid HNSW for low-dimensional 2D or 3D geometry where an exact K-D Tree or R-Tree uses far less RAM.
Avoid uncompressed HNSW when memory is strictly bounded and graph links (M * 4-8 bytes/vector) exceed RAM.
What It Does
Assigns each inserted vector a maximum layer drawn from an exponential decay distribution
Greedily traverses upper layers (ef = 1) to locate the closest entry point for the base layer
Performs bounded beam search (efConstruction / efSearch) on lower layers and links diverse neighbors up to degree M
Use Cases
Semantic vector search over 768D or 1536D LLM and multimodal embeddings
Retrieval-Augmented Generation (RAG) and recommendation candidate generation
High-dimensional k-nearest-neighbor (k-NN) search where exact K-D Trees fail
Inputs
Collection of D-dimensional float vectors, query vector q, neighbor count k, beam width efSearch
Outputs
Top-k approximate nearest neighbor IDs and cosine/L2 distances