# Nearest neighbors

> Find the nearest vectors by squared Euclidean distance. Two IVF-Flat layouts isolate the cost of gathering coordinates by record position versus scanning a cluster contiguously. Residual IVF-PQ searches the same clusters using compressed vector codes. Disk-backed IVF-PQ can search a supplied set of eligible IDs. HNSW searches a hierarchy of in-memory proximity graphs. DiskANN traverses a disk-backed Vamana graph, scoring its frontier with PQ. An exhaustive scan supplies the exact answer.

Find the nearest vectors by squared Euclidean distance. Two IVF-Flat layouts isolate the cost of gathering coordinates by record position versus scanning a cluster contiguously. Residual IVF-PQ searches the same clusters using compressed vector codes. Disk-backed IVF-PQ can search a supplied set of eligible IDs. HNSW searches a hierarchy of in-memory proximity graphs. DiskANN traverses a disk-backed Vamana graph, scoring its frontier with PQ. An exhaustive scan supplies the exact answer.

## Result

> On SIFT10K, HNSW at width 32 reaches 99.4% held-out recall\@10 at 0.21 ms p95, against 1.5 ms for exhaustive search

## Modules

* [`knotkit/inverted-index`](/modules/inverted-index/): Indexes records by exact keys and scans sorted record IDs for one key or the union of several, using packed posting lists
* [`knotkit/heap`](/modules/heap/): Provides binary and k-ary min/max heaps plus indexed priority queues, ordered by caller-supplied comparators
* [`knotkit/bitset`](/modules/bitset/): Provides exact membership, cardinality, set algebra, and order statistics over unsigned 32-bit ids, in one flat bitmap or 65,536-id chunks
* [`knotkit/cache`](/modules/cache/): Bounds an in-memory key-value store by entry count or total weight, with least-recently-used, SIEVE, S3-FIFO, or caller-supplied eviction and optional expiry
* [`knotkit/distance`](/modules/distance/): Compares vectors, probability distributions, and sets and exposes each rule’s mathematical guarantees
* [`knotkit/prng`](/modules/prng/): Generates seeded pseudorandom numbers and unbiased integers for reproducible simulations and tests
* [`knotkit/sampling`](/modules/sampling/): Draws unbiased subsets, pairs, shuffles, and weighted samples over injected randomness

## Figures

* HNSW finds 99.4% of the true top 10 with 7.1× lower p95 latency.
* Measured on 50 held-out SIFT10K queries.
* On the 2,048-vector synthetic fixture, HNSW reaches 100% recall\@10 scoring 179 vectors per query; clustered IVF-Flat needs 1,065.
* In a separate tiny replay, IVF-Flat scores 2 candidates and exhaustive search scores 4.

Hover or focus a chart for exact values. Drawn by [`demo.ts`](https://github.com/jkomyno/knotkit/blob/main/problems/nearest-neighbors/demo.ts) at build time, from the fixtures the benchmark uses; it reproduces the README figure.

## Reproduce

```bash
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:nearest-neighbors
```

[`problems/nearest-neighbors/README.md`](https://github.com/jkomyno/knotkit/blob/main/problems/nearest-neighbors/README.md) states the fixture, the seed, and the result table.
