HNSW finds nearest vectors fast by walking a layered graph of links, trading a little recall for large speed gains.
Many AI tools turn text or images into long lists of numbers called vectors. Similar items get vectors that are close together. To search, you turn the question into a vector and find the nearest stored vectors. Comparing with every stored vector is exact but slow for millions of them. This paper finds the nearest vectors fast, with a small chance of missing one.
Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs. Yu. A. Malkov and D. A. Yashunin. First posted on arXiv in March 2016, last revised August 2018. Link: arxiv.org/abs/1603.09320.
Link each point to a few nearby points, stack sparser copies of that network on top, and search by walking from the top layer down, always stepping to whichever neighbour is closer to the query.
The top layers have few points, so you cross the space in big steps. The bottom layer has every point, so you finish with small steps.
First, some terms.
Give every point links to a few points near it. To search, start at any node, look at its neighbours, and move to the one closest to the query. Repeat. Stop when no neighbour is closer than where you stand. This is greedy search.
A graph with a mix of short and long links, where greedy search reaches any target in few steps, is called a navigable small-world graph. The earlier NSW method used one flat graph like this. HNSW adds layers.
Each point gets a top layer number, picked at random. Most points get 0, so they appear only in the bottom layer. A few get 1 or more, so they appear in that layer and in every layer below it. Each layer is a graph over the points that reached it. The paper compares this to a skip list, a sorted list with "express lanes".
The paper picks the layer as floor(-ln(u) * mL), where u is a random number between 0 and 1 and mL is a constant. A good choice is mL = 1/ln(M). Then each point goes up one more layer with probability 1/M.
Take M = 16 and 1,000,000 points. A point reaches layer 1 or higher with probability 1/16, layer 2 or higher with probability 1/256, and so on.
| Layer | Expected points |
|---|---|
| 0 (all points) | 1,000,000 |
| 1 or higher | 62,500 |
| 2 or higher | about 3,900 |
| 3 or higher | about 244 |
| 4 or higher | about 15 |
So a million points need only about 5 layers. These numbers come from the formula, not from measurements.
To find the nearest neighbours of a query:
ef best nodes found so far and keep exploring from the closest unexplored one. Stop when the closest unexplored node is farther than the worst node in the list. Return the best K.Eight points on a number line, where distance is the difference. Layer 0 holds 2, 9, 15, 21, 30, 38, 47, 55, and each links to its two nearest points. Layer 1 holds only 15 and 47, linked to each other. The query is 40.
Start at the entry point 15 in layer 1. Its distance to 40 is 25
Greedy search can get stuck at a point that is closer than its neighbours but is not the true nearest. That is why layer 0 keeps a list of ef candidates instead of one.
A new point uses the same walk to find its place.
If a neighbour then has more than its maximum number of links, it drops some. Which ones it drops matters.
The paper's neighbour-selection heuristic looks at candidates from nearest to farthest. It keeps a candidate only if it is closer to the new point than to every neighbour already kept. This spreads the links in different directions. Plain "keep the M nearest" can put all links inside one cluster, so a search that starts elsewhere cannot get in.
The new point is at 0. The candidates are at -1, 2 and 3, and we want M = 2.
The result is -1 and 2, one on each side, instead of the two nearest on one side.
| Setting | What it controls | Larger value |
|---|---|---|
| M | How many links each new point makes | Better recall, more memory |
| efConstruction | How many candidates are examined while inserting | Better graph, slower build; search speed is not affected directly |
| ef | How many candidates are kept while searching | Better recall, slower search |
M is fixed when you build. ef can change on every query. The paper says good values of M are usually between 6 and 48, and that layer 0 gets up to twice as many links as the others (Mmax0 = 2*M).
The paper argues that search cost grows like log(N), where N is the number of points. Cost scales that way for low-dimensional data in its tests. On 128-dimensional SIFT data the cost deviated from a pure logarithm, possibly because of the higher dimension. Building the whole index costs about O(N log N).
Memory is mainly links: about 60 to 450 bytes per point on top of the data, for M between 6 and 48.
The authors compare HNSW with other open-source methods from the same benchmark library (nmslib) and with the Faiss library on 200 million SIFT vectors. Their Table 3 for the Faiss comparison:
| Method | Build time | Peak memory | Settings |
|---|---|---|---|
| HNSW (1) | 5.6 hours | 64 GB | M=16, efConstruction=500 |
| HNSW (2) | 42 minutes | 64 GB | M=16, efConstruction=40 |
| Faiss (1) | 12 hours | 30 GB | OPQ64, IMI2x14, PQ64 |
| Faiss (2) | 11 hours | 23.5 GB | OPQ32, IMI2x14, PQ32 |
The authors report that HNSW reached much higher accuracy and searched much faster than Faiss here. On 10 million SIFT vectors, efConstruction=100 gave a reasonable index in about 3 minutes on a 40-core server. HNSW also beat the original NSW on every tested set, by almost 3 orders of magnitude on one low-dimensional set.
What this shows: for the tested data, HNSW gives a better speed and recall trade-off than the compared methods, at the cost of more memory. What it does not show: that HNSW is best for your data, or on hardware and software versions newer than the paper's.