Small Group Tutorials

Here to help students catch up, keep up, and move ahead. Book a consultation here.

How to Learn Nearest-Neighbour Search Algorithms: k-d Trees, LSH, HNSW and Recall–Latency Trade-Offs

Wait, What?

The nearest point can become harder to find as your computer learns more dimensions.

Nearest-neighbour search sounds simple: given a query, return the most similar stored items. In one dimension, this can feel almost trivial. In high-dimensional spaces, however, distance calculations become expensive, geometric pruning weakens, and the fastest practical systems often choose an answer that is very probably near the true answer rather than proving exactness every time.

Quick Answer

Learn nearest-neighbour search through the route distance metric → brute-force baseline → exact spatial indexing → k-d trees → curse of dimensionality → approximate search → locality-sensitive hashing → proximity graphs → HNSW → recall/latency/memory trade-offs → build/update behaviour → production evaluation. The professional skill is not memorising one index. It is choosing the right search guarantee for the geometry, scale and service requirement.

1. Begin With the Search Question, Not the Data Structure

A nearest-neighbour problem has four parts: a set of stored objects, a representation for each object, a distance or similarity measure, and a query asking for one or more close objects. If any of these are vague, the algorithmic problem is still undefined.

Before learning an index, practise stating: “I have n points in d dimensions, I measure closeness with this metric, and I need the nearest k results.” That single sentence prevents many later mistakes.

2. The Distance Function Is Part of the Algorithm

Euclidean distance, Manhattan distance, cosine similarity and domain-specific metrics do not create the same neighbourhoods. A system can be computationally fast and conceptually wrong if its metric does not match the meaning of similarity in the task.

At beginner level, draw a few points and calculate distances manually. At professional level, ask whether the metric obeys properties that the chosen data structure relies on and whether normalisation changes the ranking.

3. Brute Force Is the Baseline You Should Keep

The simplest exact algorithm computes the distance from the query to every stored point, keeps the best k candidates and returns them. This costs O(nd) distance work for a d-dimensional representation, plus whatever selection cost is used for the top k.

Do not dismiss this baseline. It is easy to verify, provides ground truth for recall tests, and can be competitive for small datasets, low query rates or heavily vectorised hardware. A professional optimisation should beat a measured baseline, not an imagined one.

4. k-d Trees Partition Space Recursively

A k-d tree recursively splits points by coordinate dimensions. A query descends toward a likely region, keeps the best candidate found so far, then decides whether another branch could contain a closer point. If the geometry proves that a region cannot beat the current best distance, that region can be pruned.

Jon Bentley introduced the multidimensional binary search tree in 1975. The foundational paper is Multidimensional Binary Search Trees Used for Associative Searching.

5. Learn k-d Search by Drawing the Bounding Regions

Do not start by copying recursive code. Draw the partition lines. Mark the query. Track the current best radius. Then ask whether that radius crosses a splitting boundary. If it does, the opposite branch may still contain a better answer and must be explored.

The important invariant is geometric: a branch is skipped only when its entire feasible region is farther away than the best candidate already known.

6. Exact Spatial Search Weakens in High Dimensions

As dimensionality grows, many spatial partitions stop excluding much of the search space. Distances can also become less discriminating, depending on the data distribution and metric. A tree that prunes aggressively in two dimensions may visit a large fraction of nodes in hundreds of dimensions.

This is one face of the curse of dimensionality. The lesson is not that k-d trees are “bad”. It is that their usefulness depends on geometry.

7. Approximate Nearest Neighbour Changes the Contract

Approximate nearest-neighbour search accepts that the returned neighbour may not always be the mathematically exact nearest point. In exchange, query time can fall dramatically on large, high-dimensional datasets.

The crucial professional move is to make approximation measurable. “Usually close enough” is not an engineering requirement. Recall at k, latency, memory and build cost are measurable requirements.

8. Locality-Sensitive Hashing Makes Nearby Points Collide More Often

Ordinary hash tables try to spread keys uniformly. Locality-sensitive hashing does something almost opposite: it chooses hash families so nearby points have a higher probability of landing in the same bucket than far-away points.

The query probes candidate buckets rather than scanning the whole dataset. Multiple hash tables or projections can raise the chance that true neighbours become candidates. A classic Euclidean-space construction uses p-stable distributions; see Datar, Immorlica, Indyk and Mirrokni, Locality-sensitive hashing scheme based on p-stable distributions.

9. LSH Teaches a Powerful Probability Trade-Off

With LSH, more tables or probes can increase recall but also consume more memory and work. Fewer tables reduce cost but may miss good neighbours. This makes LSH a useful learning model for probabilistic indexing: the data structure is not merely faster or slower; it moves along a controllable quality-cost curve.

10. Graph-Based Search Uses Neighbours to Find Neighbours

Another family builds a proximity graph in which points connect to selected nearby points. At query time, the algorithm starts from one or more entry points and repeatedly moves toward candidates that look closer to the query.

This converts geometric search into guided graph traversal. The graph must be connected and navigable enough that local moves can reach strong candidates without examining everything.

11. HNSW Adds Layers of Navigation

Hierarchical Navigable Small World graphs organise proximity links into layers. Upper layers contain fewer points and support coarse movement across the dataset; lower layers refine the search locally. The original HNSW work by Malkov and Yashunin describes the layered graph and its practical search behaviour: Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs.

The learning idea is similar to finding a city by first choosing the country, then the region, then the street—except the “map” is a graph constructed from data similarity rather than geography.

12. HNSW Has Build Parameters and Query Parameters

Professional learners should distinguish parameters that shape the index from parameters that shape each search. More graph connections and more careful construction can improve search quality but increase build time and memory. Exploring more candidates during a query can improve recall but raise latency.

This separation is important because a production service may build an index once but serve millions of queries. Spending extra work at construction can be sensible when it reduces repeated query work.

13. Recall@k Is the First Quality Metric to Learn

Suppose brute force says the true top 10 neighbours are known. If an approximate search returns eight of those ten, recall@10 is 0.8. This lets you compare algorithms and parameter settings against exact ground truth.

Recall must be paired with latency. A setting that raises recall from 0.95 to 0.999 may be worthwhile—or may be an expensive gain that the application does not need.

14. Measure the Tail, Not Only the Average

Interactive systems care about slow queries as well as average queries. A professional benchmark should record median latency and tail behaviour such as p95 or p99, together with throughput, recall, memory footprint, build time and update cost.

One average number can hide a system that occasionally becomes painfully slow.

15. Dynamic Data Changes the Choice

Some indexes are easiest to build for static data. Others support insertions naturally but may require care with deletions, graph maintenance or rebalancing. If the dataset changes every minute, an index with excellent static-query benchmarks may be the wrong production choice.

Always ask: How often do points arrive? How often are they removed? Must updates be visible immediately? Can the index be rebuilt offline?

16. High-Dimensional Search Is Still an Active Research Area

HNSW is widely used, but its design continues to be studied. Recent work has examined whether the hierarchy itself is always responsible for performance and how graph structure creates fast navigation paths. This is a useful professional lesson: a successful practical algorithm can remain theoretically interesting long after adoption.

For one recent example, see Munyampirwa, Lakshman and Coleman, Down with the Hierarchy: The ‘H’ in HNSW Stands for “Hubs”.

17. Connect This Topic to Earlier Algorithm Foundations

The existing Searching Algorithms article owns the basic search progression. The Hash Tables article owns ordinary hashing. The Computational Geometry article owns broad spatial reasoning. Nearest-neighbour search combines those foundations around a different job: retrieving similar items under explicit geometric and probabilistic guarantees.

18. Common Learning Failure States

  • Using a distance metric without checking what it means.
  • Learning HNSW code before understanding the brute-force ground truth.
  • Assuming a k-d tree remains efficient as dimension rises.
  • Treating approximate search as “wrong” instead of measuring recall.
  • Reporting recall without latency, or latency without recall.
  • Ignoring build time, memory and update behaviour.
  • Tuning against one dataset and assuming the same settings generalise.
  • Comparing indexes without using the same hardware and query workload.

19. A Beginner-to-Professional Learning Ladder

  • Level 1: compute one-dimensional and two-dimensional nearest neighbours by hand.
  • Level 2: implement brute-force k-nearest neighbours.
  • Level 3: draw a k-d tree and trace pruning.
  • Level 4: measure how k-d search changes as dimension increases.
  • Level 5: implement or use an LSH scheme and observe collision probabilities.
  • Level 6: trace greedy movement on a proximity graph.
  • Level 7: build and query an HNSW index.
  • Level 8: plot recall against latency as query effort changes.
  • Level 9: benchmark memory, build time, updates and p99 latency.
  • Level 10: choose an index for a real workload and defend the choice from measured evidence.

20. Teach the Search by Predicting Before Running

A strong learning sequence is: predict which branch or graph neighbour will be explored, run the algorithm, inspect what happened, modify one condition, then explain the change. This aligns well with PRIMM—Predict, Run, Investigate, Modify, Make—an evidence-informed structure for programming education. See Sentance, Waite and Kallia, Teachers’ Experiences of using PRIMM to Teach Programming in School.

For novices, pair this with worked examples and gradually remove support. A 2023 programming study found benefits from faded worked examples combined with metacognitive scaffolding: Shin et al. (2023).

21. Immediate, Delayed and Transfer Checks

  • Immediate: compute exact neighbours for a tiny point set.
  • Tree trace: identify which k-d branches can be pruned and why.
  • Approximation: explain what recall@k measures.
  • Trade-off: predict what happens when more LSH tables or more HNSW search effort are used.
  • Delayed: reconstruct the difference between k-d tree, LSH and HNSW without notes.
  • Transfer: choose a method for geospatial points, image embeddings, a small classroom dataset and a rapidly changing catalogue.

22. AI Assistance Boundary

AI can generate test points, plotting code, benchmark harnesses and alternative explanations. The learner should still be able to define the metric, produce exact ground truth, trace pruning or graph movement, calculate recall, interpret latency distributions and defend a search-quality trade-off independently.

Professional Direction

Advanced study includes product quantisation, inverted-file indexes, multi-probe LSH, navigable proximity graphs, vector compression, filtering with metadata, disk-based ANN structures, distributed indexing, deletion/rebuild strategies, distance kernels, SIMD/GPU acceleration, filtered nearest-neighbour search and workload-aware benchmarking.

Algorithm-learning rule: nearest-neighbour search is not one algorithm. First define what “near” means, then decide how much exactness the application can afford, and finally measure the quality-cost frontier on the real workload.