There is a chronological evolution in academics that lays the foundations of vector database.
- Product Quantization for Nearest Neighbor Search
- The Graph Standard: HNSW: Multi-layer skip-list graph topologies, logarithmic search
complexity in high dimensions, beam search heuristic, and trade-offs between connectivity degree and exploration
depth
efSearch. - Billion-scale similarity search with GPUs
- DiskANN: Fast Accurate Billion-point Nearest Neighbor Search on a Single Node

The Graph Standard: HNSW
Paper link
The constantly growing amount of the available information resources has led to high demand in scalable and efficient similarity search data structures. One of the generally used approaches for information search is the -Nearest Neighbor Search (-NNS). Unfortunately, the complexity of the naïve approach scales linearly with the number of stored elements making it infeasible for large-scale datasets.
To overcome this problem a concept of Approximate Nearest Neighbors Search (-ANNS) was proposed.
Approximate Nearest Neighbor Search (-ANNS) Algorithm
-Nearest Neighbor Search (-NNS) is the formal problem of retrieving the points in a dataset closest to a query point under a specified metric space , whereas -Approximate Nearest Neighbor Search (-ANNS) relaxes this guarantee by trading mathematical exactness for sub-linear query time in high dimensions.
In exact -NNS, given:
- A finite dataset of size , where
- A distance metric (e.g., Euclidean distance , Cosine distance, or Manhattan distance )
- A query vector and integer
Exact -NNS finds a subset of cardinality such that:
For exact search, traditional space-partitioning structures such as trees or -trees require traversing nearly all bounding boxes once the intrinsic dimensionality . As grows large, the relative difference between the distance to the nearest neighbor and the furthest neighbor approaches zero () and partitioning methods degrade to an exhaustive linear scan with computational complexity , both result in the Curse of Dimensionality.
To bypass such linear scaling, -ANNS permits an approximation error . In the standard -ANN definition, the algorithm returns a point such that:
where is the true nearest neighbor. In practical retrieval, this accuracy is measured empirically via Recall@:
where is the set of points returned by the index, and is the ground-truth exact set computed via brute-force search.
How -ANNS Works Under the Hood
All -ANNS algorithms avoid scanning every vector by pruning the search space ahead of time using one of 4 foundational paradigms:

-
Tree-Based Space Partitioning (KD-Trees/Ball Trees/RP-Trees)
Recursively bisects the vector space into hierarchical bounding volumes. trees split space using axis-aligned orthogonal hyperplanes, Ball trees partition space using nested hyper-spheres, and Random Projection (RP) trees (like Spotify’s Annoy) split points using hyperplanes oriented along random directions.

Traversal descends the tree from the root to the leaf cell containing query in time, then backtracks across parent branches using priority queues to evaluate intersecting bounding regions.
Backtracking stops after visiting an inspection limit of leaf nodes (search_k). While exact branch-and-bound works well in low dimensions (), high-dimensional queries intersect almost every bounding boundary, forcing tree search to approximate aggressively or degrade toward exhaustive scan.
-
Locality-Sensitive Hashing (LSH)
Uses hash functions designed so that collision probability is directly proportional to vector similarity:
During search, query point is hashed into multiple hash buckets; only items sharing buckets with are evaluated.
-
Inverted File and Quantization (IVF-PQ)
The space is partitioned into Voronoi cells using -means centroids. Each vector is assigned to its nearest centroid (an Inverted File list, or IVF).
At query time, is compared only to the centroids. The algorithm selects the closest centroids and inspects only the vectors residing in those specific lists. Vectors within lists are compressed via Product Quantization (PQ), splitting vectors into sub-vectors and estimating distances through lookup tables (Asymmetric Distance Computation).
-
Points are vertices in a directed graph; edges connect spatial neighbors.

Search operates as a greedy best-first traversal. Starting at an entry node, the algorithm evaluates distances to all outgoing neighbors of the current node, hopping to whichever neighbor is closest to . Traversal halts when local greedy steps no longer reduce the distance to within a candidate priority queue (beam search).
TIPGraph-based methods achieve higher recall at lower latency than inverted files, but require significant index build time ( distance computations) and hold substantial memory overhead for adjacency lists.
The power-law scaling of the proximity graph routing in NSW, however, causes extreme performance degradation in case of low dimensional or clustered data, which is why the Hierarchical Navigable Small World (Hierarchical NSW, HNSW) comes into play.
Hierarchical NSW
The paper proposed the Hierarchical Navigable Small World (Hierarchical NSW, HNSW), a new fully graph based incremental K-ANNS structure, which can offer a much better logarithmic complexity scaling.
The idea of Hierarchical NSW algorithm is to separate the links according to their length scale into different layers and then search in a multilayer graph.
In such structure the search starts from the upper layer which has only the longest links (the “zoom-in” phase). The algorithm greedily traverses through the elements from the upper layer until a local minimum is reached. After that, the search switches to the lower layer (which has shorter links), restarts from the element which was the local minimum in the previous layer and the process repeats.

Illustration of the Hierarchical NSW idea. The search starts from an element from the top layer (shown red). Red arrows show direction of the greedy algorithm from the entry point to the query (shown green).
Constructing the Hierarchical Layers
When a vector is inserted, the algorithm assigns it a ceiling layer index . For example, if an element receives level , it exists in Layer 0, Layer 1, and Layer 2. Every single vector exists at Layer 0 (the ground floor, which contains 100% of the dataset). Higher layers contain progressively smaller subsets of vectors.

Within any given layer, points are connected only to their spatial neighbors relative to the points present in that layer. To ensure higher layers become exponentially sparser, the level is drawn from an exponential distribution controlled by a scale parameter :
where , and is the target number of outgoing links per node. The probability behavior under this setup is such that the probability that a point reaches layer is . If , for example, roughly in every vectors reaches Layer 1, in every reaches Layer 2, and so on. The result is that the total number of layers scales as , keeping the index compact and preventing top layers from becoming cluttered.
The search procedure is an iterative greedy search starting from the top layer and finishing at the zero layer.


Step-by-Step Anatomy of an NSW Search Run
In a Navigable Small World graph, search operates as a greedy best-first graph traversal (Algorithm 1 / Algorithm 2 in Malkov’s original NSW papers):

What is a queryA query, or query vector , is a dense, high-dimensional array of numbers (floats) that represents the user’s input, such as a piece of text, an image, or an audio snippet, projected into the exact same mathematical embedding space as the indexed data.
-
Initialization
- The query is introduced into the metric space.
- The traversal initializes at a predefined or randomly selected Entry Point ().
- A fixed-capacity priority queue keeps track of the closest nodes discovered so far, sorted by distance .
-
Long-Range Phase (Zooming In)
Early in the run, the algorithm traverses long-range “highway” edges, links created during the early stages of graph construction that bridge distant regions of the vector space.
At each hop, the algorithm computes distances from to every neighbor of the current node and greedily advances to whichever neighbor minimizes .
-
Short-Range Phase (Clustering & Local Exploration)
As the search approaches the spatial neighborhood of , the traversal shifts from long-range jumps to dense, short-range cluster connections.
Instead of hopping along a single line, the search fans out across adjacent vertices to explore the local Voronoi neighborhood around .
-
Termination & -NN Extraction
The search evaluates candidates in its priority queue. Traversal halts when the closest unvisited neighbor of any explored node is further away than the furthest candidate currently residing in the best- set.
The top points in the priority queue are returned as the final approximate nearest neighbors.
The Fundamental Vulnerability IllustratedBecause NSW maintains all edges on a single flat plane,
If the traversal reaches a node whose direct neighbors all happen to be slightly further from than the node itself (even though a closer cluster exists just beyond an unlinked void), pure greedy search stops prematurely. This is called the local minima trap, illustrated below:
In dense local clusters, evaluating every short-range neighbor before reaching the target neighborhood inflates distance computations to or worse.
NSW can struggle at scale
Yahoo Vespa
Long before “vector database” became industry parlance, large-scale production search engines had already implemented aspects of vector database. Yahoo’s Vespa engine, which originated inside Norwegian enterprise search firm FAST Search & Transfer before being acquired and eventually open-sourced in 2017, had integrated tensor evaluation and vector search alongside traditional inverted indexes.
