Back

HNSW Explorer

Watch a search travel through the layered graph — from the sparse top layer (highways) down to the dense bottom layer (local roads). Drag the query point on Layer 0 and tune M / ef to see how the graph changes.

M4
Max neighbors per node
ef8
Layer-0 candidate pool
Speed450ms
Per step
Current Step
14 / 14
Search complete. Top-3: [3, 10, 4].

Search Stats

Visited
17 / 30
Layers
4
Entry
node 0
Top-3
[3, 10, 4]

Layer Distribution

L3
2
L2
2
L1
7
L0
30

How HNSW works

  1. Layered graph: each node is randomly assigned a max layer using an exponential distribution. Most nodes only live at Layer 0; very few reach the top.
  2. Within each layer: every node connects to its M nearest neighbors. Higher layers are sparse (few nodes, long edges = "highways"). Layer 0 is dense (all nodes, short edges = "local roads").
  3. Search starts at the top: from a fixed entry point, greedily walk to the neighbor closest to the query, repeat until no improvement.
  4. Drop down a layer and repeat: the position from the previous layer becomes the start of the next layer's walk. Each drop refines the result with finer-grained edges.
  5. Layer 0 with ef: at the bottom layer, instead of stopping at the first local minimum, HNSW maintains a pool of ef best candidates and expands their neighborhoods. Larger ef → more accurate but slower.

Why it's fast: the top layers act like an index over the index — you cover huge distances in just a few hops, then zoom in. For N points, search visits roughly O(log N) nodes instead of N.