Hierarchical Navigable Small World (HNSW) is a graph-based Approximate Nearest Neighbor (ANN) algorithm that constructs a multi-layered structure of proximity graphs, where each layer represents the same dataset at a different density. The top layers contain only long-range edges connecting distant regions of the vector space, enabling the search to make large jumps and skip irrelevant areas. The bottom layer contains all data points with short-range edges, ensuring high-precision refinement of the final candidate set.
Glossary
Hierarchical Navigable Small World (HNSW)

What is Hierarchical Navigable Small World (HNSW)?
A multi-layered proximity graph structure enabling logarithmic scaling for vector similarity search by traversing long-range edges in sparse upper layers and refining in dense lower layers.
During search, the algorithm begins at a fixed entry point in the topmost sparse layer and performs a greedy traversal, moving to the nearest neighbor until a local minimum is reached. It then descends to the next denser layer and repeats the process, using the result from the previous layer as the new entry point. This hierarchical strategy reduces search complexity to logarithmic scale, making HNSW one of the fastest and highest-recall algorithms for billion-scale vector search in production systems like FAISS and hnswlib.
Key Features of HNSW
Hierarchical Navigable Small World (HNSW) is a graph-based ANN algorithm that builds a multi-layered proximity graph. It achieves logarithmic scaling search complexity by traversing long-range edges in sparse upper layers and refining in dense lower layers.
Multi-Layer Navigable Structure
HNSW constructs a hierarchy of proximity graphs where each layer represents a different density of connections. The top layers are sparse with long-range edges, enabling rapid coarse navigation across the vector space. Bottom layers are dense with short-range edges, ensuring high-precision final refinement. An exponentially decaying probability function assigns each inserted element to a maximum layer, creating a natural hierarchy where higher layers contain fewer nodes.
Greedy Search with Beam Width
Search begins at the entry point in the topmost layer and performs a greedy best-first traversal. At each step, the algorithm evaluates neighbors and moves to the closest unvisited node, maintaining a dynamic candidate list of size efSearch (beam width). This parameter directly controls the recall-speed tradeoff: higher values explore more paths for better accuracy at the cost of increased distance computations. The search descends layer by layer until reaching the ground layer for final results.
Heuristic Neighbor Selection
During index construction, HNSW uses a pruning heuristic to select a node's neighbors rather than simply connecting to the nearest candidates. The algorithm iterates through candidates by distance and discards any that are closer to an already-selected neighbor than to the current node. This ensures diverse edge directions, prevents redundant connections, and maintains the small-world navigability property that enables logarithmic scaling. The parameter M controls the maximum number of outgoing edges per node.
Incremental Insertion Without Rebuilding
HNSW supports fully dynamic indexing—new vectors can be inserted at any time without requiring a full index rebuild. Each insertion performs a search to find the nearest neighbors at the appropriate layer, then establishes bidirectional connections using the pruning heuristic. This makes HNSW ideal for streaming data and applications where the vector collection grows continuously. Deletions are supported by marking nodes as removed, though periodic compaction may be needed for optimal performance.
Logarithmic Complexity Scaling
The hierarchical structure enables O(log N) search complexity in practice, where N is the dataset size. The top layers act as a skip list over the vector space, allowing the algorithm to jump across large distances quickly. Each descent to a lower layer reduces the search radius while increasing graph density. This scaling behavior makes HNSW one of the fastest ANN algorithms for high-recall scenarios, consistently outperforming tree-based and hashing methods on benchmark datasets.
Key Hyperparameters
- M: Maximum outgoing edges per node (typically 16-64). Higher values improve recall but increase memory usage and build time.
- efConstruction: Beam width during index building (typically 100-500). Larger values produce higher-quality graphs at the cost of slower construction.
- efSearch: Beam width during query time (typically 16-512). The primary knob for the recall-latency tradeoff—higher values explore more paths.
- mL: Normalization factor for the level generation probability, controlling the hierarchy height (typically 1/ln(M)).
HNSW vs. Other ANN Algorithms
A feature-level comparison of Hierarchical Navigable Small World against other major approximate nearest neighbor search algorithms.
| Feature | HNSW | IVFPQ | DiskANN | ScaNN |
|---|---|---|---|---|
Index Structure | Multi-layer proximity graph | Clustering + compressed residuals | Single-layer graph on SSD | Anisotropic vector quantization |
Search Complexity | O(log N) | O(√N) with probes | O(log N) with SSD I/O | O(√N) with quantization |
Incremental Insertion | ||||
Deletion Support | ||||
Memory Footprint | High (raw vectors + graph edges) | Low (compressed codes) | Very low (SSD-resident) | Low (quantized codes) |
Recall@10 (Typical) | 95-99% | 90-95% | 95-99% | 95-98% |
Build Time | Moderate | Fast | Slow (SSD writes) | Fast |
GPU Optimization |
Frequently Asked Questions
Addressing the most common technical questions about the Hierarchical Navigable Small World algorithm, from its multi-layer graph structure to practical tuning parameters for production vector search systems.
Hierarchical Navigable Small World (HNSW) is a graph-based ANN algorithm that constructs a multi-layered proximity graph to achieve logarithmic search complexity. It works by building a hierarchy of NSW graphs, where the bottom layer contains all data points and each higher layer contains a sparser subset. Search begins at the topmost layer, performing a greedy traversal along long-range edges to quickly approach the query's neighborhood. Once a local minimum is found, the search descends to the next layer and repeats, using the best candidate from the upper layer as the entry point. This skip-list-inspired architecture allows the algorithm to make large jumps in sparse upper layers and refine the search in dense lower layers, dramatically reducing the number of distance computations compared to a flat graph traversal. The graph is built incrementally by inserting vectors one by one, with each new node's maximum layer assigned probabilistically using an exponentially decaying distribution controlled by the m_L parameter.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Understanding HNSW requires familiarity with the broader landscape of vector search algorithms, performance metrics, and indexing strategies that define the tradeoffs between speed, accuracy, and memory.
Recall@K
The standard evaluation metric for ANN algorithms, measuring the fraction of true nearest neighbors found within the top K retrieved results. A Recall@10 of 0.95 means 95% of the exact 10 nearest neighbors were included in the algorithm's returned set.
- Tradeoff Axis: Recall is the primary currency exchanged for speed in approximate search
- Ground Truth: Requires computing exact distances against all vectors, feasible only for benchmarking
- Typical Targets: Production systems often target Recall@10 between 0.95 and 0.99
- HNSW Performance: Routinely achieves 0.99+ recall with only 0.1-1% of vectors visited, dramatically outperforming flat search
Graph Degree
A critical hyperparameter in HNSW controlling the number of outgoing edges per node at each layer. Higher degree increases graph connectivity and recall but consumes more memory and slows traversal.
- Layer 0 (Bottom): Typically uses a higher degree (e.g., M=16-64) for dense local connectivity
- Upper Layers: Inherit a reduced degree (e.g., M/2) to maintain sparse, long-range shortcut structure
- Memory Impact: Each edge stores a vector ID and distance, scaling linearly with degree × dataset size
- Tuning Guidance: Increase M for higher recall targets; decrease for memory-constrained edge deployments
Brute-Force Search
The exact nearest neighbor retrieval method that computes the distance between a query vector and every database vector. Guarantees perfect recall at the cost of O(N·D) time complexity, where N is dataset size and D is dimensionality.
- Flat Index: The simplest vector index with no preprocessing; vectors stored sequentially
- When Viable: Acceptable for small datasets (<100K vectors) or when perfect accuracy is non-negotiable
- HNSW Comparison: HNSW achieves sub-linear O(log N) complexity by traversing only a fraction of the graph
- Benchmark Baseline: Brute-force serves as the ground truth for computing Recall@K in ANN evaluations
Re-ranking
A two-stage retrieval pipeline where a fast ANN index like HNSW retrieves a candidate set, and a more precise computation re-scores these candidates using full-precision vectors or a cross-encoder model.
- Candidate Generation: HNSW retrieves top-K candidates (e.g., K=100) with high recall
- Refinement Stage: Candidates are re-scored using exact distance computation or a more expensive model
- Benefit: Decouples recall from precision, allowing the ANN index to optimize for speed while the re-ranker ensures final accuracy
- Common Pattern: HNSW efSearch set lower for speed, compensated by larger K and precise re-ranking

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us