Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 24

Exact vs. Approximate k-NN: Speed-Accuracy Tradeoff

Recommended resources

Hello! Welcome back.

In our last lesson, you implemented a bounded max-heap to efficiently find the top k candidates in your exact k-NN search. This was a critical optimization, moving the selection complexity from a costly O(N log N) sort to a much more efficient O(N log k). However, the core of the algorithm remains a brute-force, sequential scan of every single vector in the dataset, which has an O(N) complexity. For truly massive datasets—common in modern AI applications—even this highly optimized exact search becomes a performance bottleneck.

This lesson addresses that fundamental limitation. Our goal is to contrast exact k-NN search with Approximate Nearest Neighbor (ANN) search, explaining the tradeoff between speed and accuracy. We will explore why and when we might be willing to sacrifice the guarantee of finding the perfect answer in exchange for potentially orders-of-magnitude improvements in speed and scalability. This is a core concept in the design of any high-performance vector search system.

The Wall of Exactness

Your current implementation performs an exact nearest neighbor search. This means for any query, you guarantee finding the mathematically true k nearest neighbors. As you've seen, this involves:

  1. Iterating through all N vectors in the database.
  2. Calculating the distance from the query vector to each of the N vectors.
  3. Using an efficient selection algorithm (your bounded max-heap) to keep track of the k best candidates seen so far.

The complexity is dominated by the linear scan, making it O(N). While your SIMD optimizations and cache-aligned data layout make each step of that scan very fast, the total time still grows linearly with the number of vectors. If N is 1 million, you do 1 million comparisons. If N is 1 billion, you do 1 billion comparisons. At this scale, no amount of low-level optimization can escape the tyranny of linear growth.

This video from Redis provides a great summary of this exact search method, often called a "flat" index or "brute-force linear scan."

Exact vs Approximate (HNSW) Nearest Neighbors in Vector Databases

This video succinctly explains the concept of exact k-NN and its limitations.

Please watch the segment from "Exact and nearest neighbor search" up to the introduction of ANN. It clearly defines the brute-force approach and why its linear scaling becomes a bottleneck for large datasets.

The problem is even more pronounced in high-dimensional space, a phenomenon known as the "curse of dimensionality." Many traditional spatial indexing structures (like k-d trees) that work well for 2D or 3D data become less effective than a simple linear scan when the number of dimensions grows large (e.g., > 10-20). Since modern vector embeddings often have hundreds or thousands of dimensions, these classic data structures don't solve our problem.

We need a different approach.


The ANN Compromise: Trading Perfection for Speed

If we cannot reduce the cost of an exact search, perhaps we can change the problem. This is the core idea of Approximate Nearest Neighbor (ANN) search.

Instead of demanding the absolute, mathematically-guaranteed k nearest neighbors, ANN algorithms aim to find k neighbors that are very close to the true answer, and to do so dramatically faster. The fundamental compromise is this: we give up a small, often tunable, amount of accuracy in exchange for a massive gain in performance.

The following video introduces this idea perfectly. It frames ANN not as a magic bullet for the exact problem, but as a change in the goal itself.

Approximate Nearest Neighbors : Data Science Concepts

This video from the ritvikmath channel gives a clear, intuitive explanation of the motivation behind ANN.

Watch the section from "the best you can do". The key phrase to focus on is: "What if we got the almost closest neighbors... but in a fraction of the time?"

This trade-off is formalized through a metric called recall. In the context of k-NN, recall@k measures the fraction of the true k nearest neighbors that are successfully found by the approximate algorithm.

Recall@k = (Number of true k-NN found) / k

A recall of 1.0 means you found all the true neighbors (i.e., you performed an exact search). A recall of 0.95 means you found 95% of them. In many applications—like recommendation engines or semantic search—finding 95% of the most similar items at 100x the speed is a far better outcome than finding 100% of them at 1x speed.

A high-level comparison between the guarantees of K-Nearest Neighbors (KNN) and the practical advantages of Approximate Nearest Neighbors (ANN).

The Dimensions of the Trade-off

The choice between exact and approximate search isn't a simple binary one. It's a multi-dimensional optimization problem. When evaluating an ANN algorithm, system designers consider several competing factors.

Choosing ANN Algorithms: Performance Trade-offs

The article from apxml.com provides an excellent breakdown of the factors involved in choosing an ANN algorithm.

Please read the section titled "Core Trade-off Dimensions". This list—Search Speed, Recall, Memory Usage, Index Build Time, and Updateability—is the standard framework for evaluating these systems.

Let's focus on the four most critical dimensions for our project:

  1. Search Speed (Latency / QPS): How fast can we get an answer? This is often the primary motivator for using ANN. Instead of O(N), many ANN algorithms offer logarithmic (O(log N)) or polylogarithmic complexity.
  2. Recall (Accuracy): How correct is the answer? This is what we sacrifice. The key is that this trade-off is often tunable. We can choose parameters to get higher recall at the cost of lower speed, or vice-versa.
  3. Memory Usage (Index Size): ANN algorithms work by building an intelligent data structure (an "index") in memory before any queries are run. This index allows for faster searching but requires memory to store. Some indices are very memory-hungry.
  4. Index Build Time: The pre-processing step of building the index can take a significant amount of time. For static datasets, this is a one-time cost. For dynamic systems where data is constantly added, a long build time is a major drawback.

A Concrete Example: HNSW vs. IVFPQ

To make these trade-offs tangible, let's examine a real-world benchmark. The following article from AWS compares two popular ANN methods on a dataset of 1 billion vectors.

  • HNSW (Hierarchical Navigable Small Worlds): A graph-based algorithm. We will be building this later in the course. It builds a multi-layered graph where nodes are vectors and edges connect similar vectors. Searching involves navigating this graph.
  • IVFPQ (Inverted File with Product Quantization): A clustering and compression-based method. It groups vectors into buckets (IVF) and then compresses them (PQ) to save memory. Searching involves picking a few buckets and scanning them.

Choose the k-NN algorithm for your billion-scale use case ... - AWS

This AWS Big Data Blog post provides a superb, data-driven comparison of ANN algorithms at massive scale.

Focus on the Experiments section. First, quickly read the parameter descriptions to understand what's being tuned (m, ef_search for HNSW; nprobes, m for IVFPQ). Then, spend your time carefully analyzing the two Results tables. Compare the p50 Query latency, Recall@10, and Native memory consumption for different configurations.

Let's dissect those results, as they perfectly illustrate the engineering trade-offs:

Test ID Method Latency (p50) Recall@10 Memory (GB) Cost/hr
hnsw1 HNSW 9.1 ms 0.84 1182 ~$75
hnsw3 HNSW 23.1 ms 0.99 1306 ~$75
hnsw6 HNSW 103.7 ms 0.99 2055 ~$75
ivfpq1 IVFPQ 74.9 ms 0.17 68 ~$11
ivfpq4 IVFPQ 117.2 ms 0.61 114 ~$11

Analysis:

  1. HNSW Performance: Look at hnsw1 vs. hnsw3. By slightly increasing latency (9ms to 23ms), the recall jumps from a decent 84% to a near-perfect 99%. This is the classic speed/accuracy trade-off in action. The memory cost is enormous but provides top-tier performance.

  2. IVFPQ Performance: The most striking feature is the memory usage. It uses ~20x less memory than HNSW! This comes at a steep price. Even at its best (ivfpq4 and ivfpq6), the recall barely passes 60%, and the latency is much higher than the fastest HNSW configurations.

  3. The Engineering Decision: Imagine you are a systems architect.

    • If your application absolutely requires the lowest possible latency and >98% recall (e.g., a critical production search system), and you have the budget, HNSW is the clear winner. You pay for the performance with a large memory footprint and higher server costs.
    • If your application is memory-constrained or you can tolerate lower recall (e.g., an internal analytics tool), IVFPQ is an extremely cost-effective choice. You accept a "good enough" answer to save dramatically on hardware costs.

There is no single "best" algorithm. The optimal choice is entirely dependent on the constraints of the application.


Conclusion

In this lesson, we established the fundamental difference between exact k-NN and approximate (ANN) search. You now understand the "why" behind sacrificing perfect accuracy and the multi-dimensional nature of the trade-offs involved.

Key Takeaways:

  • Exact k-NN guarantees 100% recall but scales linearly with the dataset size (O(N)), making it impractical for very large collections.
  • ANN trades a small, controllable amount of accuracy (recall) for massive improvements in search speed, often achieving sub-linear complexity (e.g., O(log N)).
  • The choice of an ANN algorithm is a multi-dimensional problem, balancing Search Speed, Recall, Memory Usage, and Index Build Time.
  • Different algorithms like HNSW and IVFPQ occupy different points in this trade-off space, making them suitable for different application requirements and resource constraints.

We have now laid the theoretical groundwork. You know why we are going to build an ANN index. In the next module, we will begin to explore how.

Our next lesson will kick off the "ANN with Navigable Small World (NSW) Graphs" module. We'll start by examining the data structure of an NSW graph, the simpler precursor to HNSW, and design the C structs necessary to represent it in memory. This will be our first step towards building a search algorithm that can break the linear-time barrier.