---
title: "Reverse Nearest Neighbor Queries with a Dynamic HNSW Index"
description: "A dynamic HNSW index stores a reverse-neighbor connection for every neighbor connection to make deletion cheap. The same bookkeeping turns reverse-nearest-neighbor retrieval — which elements reference X as a nearest neighbor — into a single key lookup, an approximate reverse-kNN over the graph's edges."
draft: false
author: "Marc Erdmann"
publishedAt: "2024-05-15"
updatedAt: "2026-06-16"
---

A dynamic HNSW index stores, for every neighbor connection, a corresponding reverse-neighbor connection, so that a deletion can find the elements that point at a node without scanning the dataset. That same bookkeeping quietly answers a different question — *which elements reference $X$ as one of their nearest neighbors?* — the reverse-nearest-neighbor (reverse-kNN) query. This article looks at reverse-kNN as a query capability that the stored connections naturally unlock.
<!--more-->

> **Editor's note.** This article is based on the author's 2020 bachelor's thesis and should be read against the state of the art at that time.

The thesis stores reverse-neighbor connections to make deletion tractable; the design is described in a [companion article on extending HNSW for dynamically changing graphs](/blog/extending-the-hnsw-algorithm-for-dynamically-changing-graphs). What follows is a design observation rather than a measured result: the index does not implement, expose, or benchmark a reverse-kNN query API. The point is that the data needed to answer such a query is already present, and retrieving it is cheap. I am careful below about what exactly is retrievable, because it is not the exact reverse-kNN of the dataset.

## Why reverse-kNN is a distinct query

The ordinary $k$-nearest-neighbor search (kNN) takes a query point $q$ and returns the $k$ points most similar to it under a distance function. The reverse query inverts the direction of the relation: a reverse-kNN query $R_k(q)$ returns the set of elements that have $q$ among *their* $k$ nearest neighbors.

It is tempting to assume the two coincide — that if $q$'s nearest neighbors are $\{a, b, c\}$, then $a$, $b$, and $c$ are precisely the elements that consider $q$ their nearest neighbor. They do not coincide, because the nearest-neighbor relation is not symmetric. If $b$ is the nearest neighbor of $a$, it does not follow that $a$ is the nearest neighbor of $b$: $b$ may sit in a denser region and have several other points closer to it than $a$. The thesis illustrates this directly — the nearest neighbor of a left node is a middle node, whereas the nearest neighbor of that middle node is a third node off to the right, not the left node it was named by.

This asymmetry is exactly why the $k$-nearest-neighbor graph is *directed*: an edge $e(p, q)$ records that $q$ is one of $p$'s $k$ nearest neighbors, and the reverse edge need not exist. Forward neighbors and reverse neighbors are therefore genuinely different sets, and a reverse-kNN query is a genuinely different query from a kNN query — not a re-reading of the same result.

## What the index already stores

In the dynamic index, every directed neighbor connection added from an element $e$ to a candidate $c$ is mirrored by a directed reverse-neighbor connection from $c$ back to $e$. The two connection types are maintained in lockstep: an admission adds both the forward and the reverse edge, an eviction removes both. For every neighbor connection there is therefore always a matching reverse-neighbor entry. This is the bookkeeping the deletion procedure relies on — it needs the set of elements pointing at a node in order to detach their incoming edges — and it is described in full in the companion article.

The persistence layer stores these connections in a way that makes the reverse set directly addressable. The index is built on a key/value store, where a value can be retrieved efficiently only by its key or by a key prefix. Reverse-neighbor entries are given a key composed of a type identifier `r`, the collection and field identifiers, a single-byte layer identifier, and the hashed document identifier. Crucially, *all* reverse nearest neighbors of a given document in a given layer are stored together under this one key, as a single entry — not one key per reverse neighbor. The thesis notes this consolidation explicitly: storing all reverse neighbors of a document under one key minimizes lookups, the same optimization applied to the forward-neighbor entries.

The consequence for retrieval is the part that matters here. Asking "which elements reference document $X$ as a nearest neighbor, in layer $\ell$?" is answered by reading the reverse-neighbor entry whose key carries $X$'s identifier and the layer identifier $\ell$ — a single key lookup. Asking for all reverse neighbors of $X$ across the structure is a prefix scan over its per-layer entries. Either way, no scan of the dataset is required. Because the layer identifier is part of the key, the answer is naturally organized per layer.

## What this is, and what it is not

The retrievable set is the reverse of the *graph's* neighbor connections, and that distinction must be stated plainly. It is not the exact reverse-kNN of the dataset, for two reasons that compound.

First, the neighbor connections are produced by an *approximate* search. HNSW is a $k$-ANNS structure: it trades exactness for speed, so the forward neighbors stored for an element are its approximate nearest neighbors, not necessarily its true ones. The reverse of an approximate relation is itself approximate.

Second, the number of connections an element may hold per layer is capped at $M_{max}$. An element keeps only its $M_{max}$ closest neighbors in a layer; when a closer candidate arrives, the furthest current neighbor is evicted and its reverse-neighbor entry removed. So even where the forward edges are accurate, they are a bounded selection. An element that would be the $(M_{max}+1)$-th nearest neighbor of $X$ is simply not recorded as referencing $X$.

Taken together, what a single key or prefix lookup returns is an **approximate reverse-kNN over the proximity graph's stored edges, per layer** — not the exact reverse-kNN over the full dataset. This is the honest characterization, and it is the one to use. The efficiency claim is correspondingly narrow: retrieval is a single key or prefix lookup rather than a dataset scan. I make no claim about latency, and there are no measurements of a reverse-kNN query here, because the thesis neither implements nor benchmarks one.

## What reverse-kNN is good for

The interest in reverse-kNN is older than this index. Korn and Muthukrishnan introduced reverse-nearest-neighbor queries as a way to compute *influence sets*: the set of points for which a given point is the nearest neighbor is, in a precise sense, the set it influences. The size and membership of $R_k(q)$ estimate how much impact $q$ has on the rest of the data, which is a different and often more useful notion than which points $q$ is close to.

Several applications follow from that framing. In a recommendation setting, the elements that hold a product or a user among their nearest neighbors are the ones whose recommendations that product or user shapes — its influence set, rather than merely its look-alikes. For estimating impact, the cardinality of a reverse-neighbor set is a direct proxy for how central or referenced an element is. And for change management, a reverse-kNN query answers "what would be affected if this element were updated or removed?" — which is, not coincidentally, the very question the deletion procedure has to answer internally before it detaches a node. Seen this way, exposing reverse-kNN as a query is less an addition to the index than a surfacing of work it already does.

These uses tolerate approximation well, in the same way kNN applications do: an approximate influence set computed from the graph's edges is, for ranking and impact-estimation purposes, often as serviceable as an exact one would be. The caveat from the previous section still stands — the result reflects the graph's edges, not the full dataset — and any application built on it should be designed with that in mind.

## Conclusion

Once a dynamic HNSW index records a reverse-neighbor connection for every neighbor connection — which it does to make deletion possible without scanning the dataset — it holds, at each node and in each layer, the set of elements that reference that node as a nearest neighbor. Because those entries are keyed by document and layer and consolidated under a single key, that set is recoverable with one key or prefix lookup. This turns reverse-nearest-neighbor retrieval, the basis of influence sets, into a cheap operation that the design already supports in substance.

Two qualifications keep the claim honest. The retrievable set is an approximate reverse-kNN over the graph's stored, $M_{max}$-capped edges, per layer — not the exact reverse-kNN of the dataset. And it is a design observation: the reference implementation stores these connections for deletion and does not implement, expose, or benchmark a reverse-kNN query API. Surfacing one is a natural extension, not a measured feature.

## References

Malkov, Yu A., and D. A. Yashunin. "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs." IEEE Transactions on Pattern Analysis and Machine Intelligence (2018).

Korn, F., and S. Muthukrishnan. "Influence sets based on reverse nearest neighbor queries." ACM SIGMOD Record (2000).
