Exact nearest neighbor search scans every vector in the database - O(n) per query, completely infeasible at billion-vector scale. Approximate Nearest Neighbor algorithms trade a small recall penalty for orders-of-magnitude speedup. Flat (brute force) is the baseline with 100% recall. IVF (Inverted File Index) partitions vectors into k-means clusters and searches only the nearest nprobe clusters. HNSW (Hierarchical Navigable Small World) builds a multi-layer graph with logarithmic search time and the best recall-speed tradeoff in practice. LSH (Locality Sensitive Hashing) uses random projections to hash similar vectors into the same bucket - fastest build time, lowest recall. FAISS (Facebook AI Similarity Search) implements all of these and is the de-facto production library.
HNSW: hierarchical graph, O(log n) query, best recall/QPS tradeoff, high memory (graph edges overhead)
LSH: random projections to hash buckets, O(1) amortized query, lowest recall, simplest implementation
Flat: exact brute-force, 100% recall, O(n) per query - only viable for <1M vectors
Recall@10: HNSW achieves 99%+ recall at 2ms query time; IVF achieves 90%+ recall at similar speed with less memory
ann-benchmarks.com: standardized recall vs QPS comparisons across all major algorithms and libraries
Part of the EngineersOfAI Interactive 3D - free interactive visualizations covering every major concept in machine learning and AI engineering. Hover any element for a plain-English explanation. No code required.