Similarity Search, Part 4: Hierarchical Navigable Small World (HNSW) | by Vyacheslav Efimov | Jun, 2023


Uncover learn how to assemble environment friendly multi-layered graphs to spice up search velocity in large volumes of knowledge

Similarity search is an issue the place given a question the aim is to seek out essentially the most comparable paperwork to it amongst all of the database paperwork.

In information science, similarity search usually seems within the NLP area, search engines like google and yahoo or recommender methods the place essentially the most related paperwork or objects must be retrieved for a question. There exists a big number of other ways to enhance search efficiency in large volumes of knowledge.

Hierarchical Navigable Small World (HNSW) is a state-of-the-art algorithm used for an approximate search of nearest neighbours. Underneath the hood, HNSW constructs optimized graph constructions making it very totally different from different approaches that had been mentioned in earlier elements of this text collection.

The principle thought of HNSW is to assemble such a graph the place a path between any pair of vertices may very well be traversed in a small variety of steps.

A well known analogy on the well-known six handshakes rule is said to this technique:

All individuals are six or fewer social connections away from one another.

Earlier than continuing to inside workings of HNSW allow us to first focus on skip lists and navigable small phrases — essential information constructions used contained in the HNSW implementation.

Skip list is a probabilistic information construction that enables inserting and looking out parts inside a sorted checklist for O(logn) on common. A skip checklist is constructed by a number of layers of linked lists. The bottom layer has the unique linked checklist with all the weather in it. When shifting to greater ranges, the variety of skipped parts will increase, thus lowering the variety of connections.

Discovering factor 20 in skip checklist

The search process for a sure worth begins from the best degree and compares its subsequent factor with the worth. If the worth is much less or equal to the factor, then the algorithm proceeds to its subsequent factor. In any other case, the search process descends to the decrease layer with extra connections and repeats the identical course of. On the finish, the algorithm descends to the bottom layer and finds the specified node.

Based mostly on the data from Wikipedia, a skip checklist has the primary parameter p which defines the chance of a component showing in a number of lists. If a component seems in layer i, then the chance that it’ll seem in layer i + 1 is the same as p (p is often set to 0.5 or 0.25). On common, every factor is introduced in 1 / (1 — p) lists.

As we will see, this course of is way sooner than the conventional linear search within the linked checklist. The truth is, HNSW inherits the identical thought however as a substitute of linked lists, it makes use of graphs.

Navigable small world is a graph with polylogarithmic T = O(logᵏn) search complexity which makes use of grasping routing. Routing refers back to the means of beginning the search course of from low-degree vertices and ending with high-degree vertices. Since low-degree vertices have only a few connections, the algorithm can quickly transfer between them to effectively navigate to the area the place the closest neighbour is more likely to be situated. Then the algorithm regularly zooms in and switches to high-degree vertices to seek out the closest neighbour among the many vertices in that area.

Vertex is typically additionally known as a node.


Within the first place, search is proceeded by selecting an entry level. To find out the following vertex (or vertices) to which the algorithm makes a transfer, it calculates the distances from the question vector to the present vertex’s neighbours and strikes to the closest one. Sooner or later, the algorithm terminates the search process when it can’t discover a neighbour node that’s nearer to the question than the present node itself. This node is returned because the response to the question.

Grasping search course of in a navigable small world. Node A is used as an entry level. It has two neighbours B and D. Node D is nearer to the question than B. Because of this, we transfer to D. Node D has three neighbours C, E and F. E is the closest neighbour to the question, so we transfer to E. Lastly, the search course of will result in node L. Since all neighbours of L are situated farther from the question than L itself, we cease the algorithm and return L as the reply to the question.

This grasping technique doesn’t assure that it’ll discover the precise nearest neighbour as the tactic makes use of solely native data on the present step to take choices. Early stopping is likely one of the issues of the algorithm. It happens particularly at first of the search process when there are not any higher neighbour nodes than the present one. For essentially the most half, this may occur when the beginning area has too many low-degree vertices.

Early stopping. Each neighbours of the present node are additional away from the question. Thus, the algorithm returns the present node because the response, although there exist a lot nearer nodes to the question.

The search accuracy could be improved through the use of a number of entry factors.


The NSW graph is constructed by shuffling dataset factors and inserting them one after the other within the present graph. When a brand new node is inserted, it’s then linked by edges to the M nearest vertices to it.

Sequential insertion of nodes (from left to proper) with M = 2. At every iteration, a brand new vertex is added to the graph and linked to its M = 2 nearest neighbours. Blue traces signify the related edges to a newly inserted node.

In most eventualities, long-range edges will seemingly be created at first section of the graph development. They play an essential position in graph navigation.

Hyperlinks to the closest neighbors of the weather inserted at first of the development later change into bridges between the community hubs that maintain the general graph connectivity and permit the logarithmic scaling of the variety of hops throughout grasping routing. — Yu. A. Malkov, D. A. Yashunin

From the instance within the determine above, we will see the significance of the long-range edge AB that was added at first. Think about a question requiring the traverse of a path from the comparatively far-located nodes A and I. Having the sting AB permits doing it quickly by instantly navigating from one aspect of the graph to the other one.

Because the variety of vertices within the graph will increase, it will increase the chance that the lengths of newly related edges to a brand new node might be smaller.

HNSW is predicated on the identical rules as skip checklist and navigable small world. Its construction represents a multi-layered graph with fewer connections on the highest layers and extra dense areas on the underside layers.

The search begins from the best layer and proceeds to 1 degree under each time the native nearest neighbour is greedily discovered among the many layer nodes. In the end, the discovered nearest neighbour on the bottom layer is the reply to the question.

Search in HNSW

Equally to NSW, the search high quality of HNSW could be improved through the use of a number of entry factors. As a substitute of discovering just one nearest neighbour on every layer, the efSearch (a hyperparameter) closest nearest neighbours to the question vector are discovered and every of those neighbours is used because the entry level on the following layer.


The authors of the original paper declare that the variety of operations required to seek out the closest neighbour on any layer is bounded by a relentless. Taking into account that the variety of all layers in a graph is logarithmic, we get the full search complexity which is O(logn).

Selecting the utmost layer

Nodes in HNSW are inserted sequentially one after the other. Each node is randomly assigned an integer l indicating the utmost layer at which this node can current within the graph. For instance, if l = 1, then the node can solely be discovered on layers 0 and 1. The authors choose l randomly for every node with an exponentially decaying chance distribution normalized by the non-zero multiplier mL (mL = 0 leads to a single layer in HNSW and non-optimized search complexity). Usually, nearly all of l values must be equal to 0, so many of the nodes are current solely on the bottom degree. The bigger values of mL enhance the chance of a node showing on greater layers.

The variety of layers l for each node is chosen randomly with exponentially decaying chance distribution.
Distribution of the variety of layers primarily based on normalization issue mL. The horizontal axis represents values of the uniform(0, 1) distribution.

To realize the optimum efficiency benefit of the controllable hierarchy, the overlap between neighbors on totally different layers (i.e. p.c of factor neighbors which might be additionally belong to different layers) must be small. — Yu. A. Malkov, D. A. Yashunin.

One of many methods to lower the overlap is to lower mL. However it is very important take into account that lowering mL additionally leads on common to extra traversals throughout a grasping search on every layer. That’s the reason it’s important to decide on such a worth of mL that can stability each the overlap and the variety of traversals.

The authors of the paper suggest selecting the optimum worth of mL which is the same as 1 / ln(M). This worth corresponds to the parameter p = 1 / M of the skip checklist being a mean single factor overlap between the layers.


After a node is assigned the worth l, there are two phases of its insertion:

  1. The algorithm begins from the higher layer and greedily finds the closest node. The discovered node is then used as an entry level to the following layer and the search course of continues. As soon as the layer l is reached, the insertion proceeds to the second step.
  2. Ranging from layer l the algorithm inserts the brand new node on the present layer. Then it acts the identical as earlier than at step 1 however as a substitute of discovering just one nearest neighbour, it greedily searches for efConstruction (hyperparameter) nearest neighbours. Then M out of efConstruction neighbours are chosen and edges from the inserted node to them are constructed. After that, the algorithm descends to the following layer and every of discovered efConstruction nodes acts as an entry level. The algorithm terminates after the brand new node and its edges are inserted on the bottom layer 0.
Insertion of a node (in blue) in HNSW. The utmost layer for a brand new node was randomly chosen as l = 2. Due to this fact, the node might be inserted on layers 2, 1 and 0. On every of those layers, the node might be related to its M = 2 nearest neighbours.

Selecting values for development parameters

The unique paper gives a number of helpful insights on how to decide on hyperparameters:

  • In response to simulations, good values for M lie between 5 and 48. Smaller values of M are typically higher for decrease recollects or low-dimensional information whereas greater values of M are suited higher for top recollects or high-dimensional information.
  • Increased values of efConstruction suggest a extra profound search as extra candidates are explored. Nonetheless, it requires extra computations. Authors advocate selecting such an efConstruction worth that outcomes at recall being near 0.95–1 throughout coaching.
  • Moreover, there may be one other essential parameter Mₘₐₓ — the utmost variety of edges a vertex can have. Other than it, there exists the identical parameter Mₘₐₓ₀ however individually for the bottom layer. It is strongly recommended to decide on a worth for Mₘₐₓ near 2 * M. Values higher than 2 * M can result in efficiency degradation and extreme reminiscence utilization. On the identical time, Mₘₐₓ = M leads to poor efficiency at excessive recall.

Candidate choice heuristic

It was famous above that in node insertion, M out of efConstruction candidates are chosen to construct edges to them. Allow us to focus on doable methods of selecting these M nodes.

The naïve method takes M closest candidates. However, it isn’t at all times the optimum selection. Beneath is an instance demonstrating it.

Think about a graph with the construction within the determine under. As you’ll be able to see, there are three areas with two of them not being related to one another (on the left and on the highest). Because of this, getting, for instance, from level A to B requires an extended path by one other area. It will be logical to in some way join these two areas for higher navigation.

Node X is inserted into the graph. The target is to optimally join it to different M = 2 factors.

Then a node X is inserted into the graph and must be linked to M = 2 different vertices.

On this case, the naïve method instantly takes the M = 2 nearest neighbours (B and C) and connects X to them. Although X is related to its actual nearest neighbours, it doesn’t clear up the issue. Allow us to take a look at the heuristical method invented by the authors.

The heuristic considers not solely the closest distances between nodes but additionally the connectivity of various areas on the graph.

The heuristic chooses the primary nearest neighbour (B in our case) and connects the inserted node (X) to it. Then the algorithm sequentially takes one other most closest nearest neighbour within the sorted order (C) and builds an edge to it provided that its distance to the brand new node (X) is bigger than any distance from the neighbour to all already related vertices (B) to the brand new node (X). After that, the algorithm proceeds to the following closest neighbour till M edges are constructed.

Getting again to the instance, the heuristical process is illustrated within the determine under. The heuristic chooses B because the closest nearest neighbour for X and builds the sting BX. Then the algorithm chooses C as the following closest nearest neighbour. Nonetheless, this time BC < CX. This means that including the sting CX to the graph shouldn’t be optimum as a result of there already exists the sting BX and the nodes B and C are very shut to one another. The identical analogy proceeds with the nodes D and E. After that, the algorithm examines the node A. This time, it satisfies the situation since AC > AX. Because of this, the brand new edge AX and each preliminary areas change into related to one another.

The instance on the left makes use of the naïve method. The instance on the best makes use of the choice heuristic which leads to two preliminary disjoint areas being related to one another.


The insertion course of works very equally, in comparison with the search process, with none vital variations which might require a non-constant variety of operations. Thus, the insertion of a single vertex imposes O(logn) of time. To estimate the full complexity, the variety of all inserted nodes n in a given dataset must be thought of. In the end, HNSW development requires O(n * logn) time.

HNSW can be utilized along with different similarity search strategies to supply higher efficiency. One of the in style methods to do it’s to mix it with an inverted file index and product quantization (IndexIVFPQ) which had been described in different elements of this text collection.

Inside this paradigm, HNSW performs the position of a coarse quantizer for IndexIVFPQ that means that it is going to be answerable for discovering the closest Voronoi partition, so the search scope could be lowered. To do it, an HNSW index must be constructed on all Voronoi centroids. When given a question, HNSW is used to seek out the closest Voronoi centroid (as a substitute of brute-force search because it was beforehand by evaluating distances to each centroid). After that, the question vector is quantized inside a respective Voronoi partition and distances are calculated through the use of PQ codes.

Selecting the closest Voronoi centroid by discovering the closest neighbour in HNSW constructed on high of Voronoi centroids.

When utilizing solely an inverted file index, it’s higher to set the variety of Voronoi partitions not too massive (256 or 1024, as an example) as a result of brute-force search is carried out to seek out the closest centroids. By selecting a small variety of Voronoi partitions, the variety of candidates inside every partition turns into comparatively massive. Due to this fact, the algorithm quickly identifies the closest centroid for a question and most of its runtime is focused on discovering the closest neighbour inside a Voronoi partition.

Nonetheless, introducing HNSW into the workflow requires an adjustment. Take into account operating HNSW solely on a small variety of centroids (256 or 1024): HNSW wouldn’t carry any vital advantages as a result of, with a small variety of vectors, HNSW performs comparatively the identical by way of execution time as naïve brute-force search. Furthermore, HNSW would require extra reminiscence to retailer the graph construction.

That’s the reason when merging HNSW and inverted file index, it is suggested to set the variety of Voronoi centroids a lot greater than standard. By doing so, the variety of candidates inside every Voronoi partition turns into a lot smaller.

This shift in paradigm leads to the next settings:

  • HNSW quickly identifies the closest Voronoi centroids in logarithmic time.
  • After that, an exhaustive search inside respective Voronoi partitions is carried out. It shouldn’t be a hassle as a result of the variety of potential candidates is small.

Faiss (Fb AI Search Similarity) is a Python library written in C++ used for optimised similarity search. This library presents several types of indexes that are information constructions used to effectively retailer the information and carry out queries.

Based mostly on the data from the Faiss documentation, we are going to see how HNSW could be utilized and merged along with inverted file index and product quantization.


FAISS has a category IndexHNSWFlat implementing the HNSW construction. As standard, the suffix “Flat” signifies that dataset vectors are totally saved in index. The constructor accepts 2 parameters:

  • d: information dimensionality.
  • M: the variety of edges that must be added to each new node throughout insertion.

Moreover, through thr hnsw area, IndexHNSWFlat gives a number of helpful attributes (which could be modified) and strategies:

  • hnsw.efConstruction: variety of nearest neighbours to discover throughout development.
  • hnsw.efSearch: variety of nearest neighbours to discover throughout search.
  • hnsw.max_level: returns the utmost layer.
  • hnsw.entry_point: returns the entry level.
  • faiss.vector_to_array(index.hnsw.ranges): returns an inventory of most layers for every vector
  • hnsw.set_default_probas(M: int, level_mult: float): permits setting M and mL values respectively. By default, level_mult is ready to 1 / ln(M).
Faiss implementation of IndexHNSWFlat

IndexHNSWFlat units values for Mₘₐₓ = M and Mₘₐₓ₀ = 2 * M.

IndexHNSWFlat + IndexIVFPQ

IndexHNSWFlat could be mixed with different indexes as effectively. One of many examples is IndexIVFPQ described within the earlier half. Creation of this composite index proceeds in two steps:

  1. IndexHNSWFlat is initialized as a rough quantizer.
  2. The quantizer is handed as a parameter to the constructor of IndexIVFPQ.

Coaching and including could be completed through the use of totally different or the identical information.

FAISS implementation of IndexHNSWFlat + IndexIVFPQ

On this article, now we have studied a sturdy algorithm which works particularly effectively for big dataset vectors. Through the use of multi-layer graph representations and the candidate choice heuristic its search velocity scales effectively whereas sustaining an honest prediction accuracy. It is usually price noting that HNSW can be utilized together with different similarity search algorithms making it very versatile.

All photos except in any other case famous are by the creator.

Source link


Please enter your comment!
Please enter your name here