How does ANN HNSW algorithm work?

LORY
6 min readJul 23, 2023

Explained in layman’s terms, no DS knowledge is required.

Beginning

If we ever working on any data science project or want to add smarter search (like vector search) into your system. you may have already been using this algorithm — HNSW.

This post is not to go through the papers (I am not able to do that) of details, just to simply extract the core idea behind this algorithm to share and discuss.

As usual, let’s start with a simple analogy.

NSW (Navigatable small world)

So today let’s say you want to catch up with your friend on the weekend, you stay in city1, and your friend in the city2, city1, and city2 together become a “Nevigatible small world”, in this world, 2 rules:

  • you can only use public transport
  • There are only buses

To achieve the shortest traveling time, the bus system is built in such a way:

  • every time add a new station (randomly pick) to map
  • When adding a new station, connect to the nearest station on the map (greedy)

Let’s build it.

So here as you can see: there are 2 cities. now if you are the builder to link these 7 bus stops together to…

--

--

LORY

A channel which focusing on developer growth and self improvement