Discovering Data Structures: Nearest Neighbor Search and Beyond

Omar Salemohamed, Laurent Charlin, Shivam Garg, Vatsal Sharan, Gregory Valiant

In the world of computer science, data structures like binary search trees and hash tables are the bedrock of efficient computing. Traditionally, these are hand-crafted by humans to be "worst-case optimal." But what if a neural network could look at a specific dataset and "hallucinate" a brand-new, more efficient way to organize it?

A recent paper from researchers at Mila, Microsoft Research, and Stanford, titled "Discovering Data Structures: Nearest Neighbor Search and Beyond," explores exactly this. They demonstrate that neural networks can discover classical algorithms and data structures from scratch, often adapting them to specific data distributions in ways human designers might overlook.

TL;DR

Researchers have developed a framework that allows neural networks to learn both a data structure and its corresponding query algorithm end-to-end. Without any prior templates, the model "re-invents" classical solutions like binary search, k-d trees, and locality-sensitive hashing, while also discovering distribution-dependent optimizations that outperform traditional hand-designed structures.

What Problem Does This Paper Address?

Traditional data structures are usually distribution-agnostic—they are designed to work well regardless of whether your data is uniform, skewed, or clustered. While "learning-augmented" data structures (using ML to improve existing structures) have gained traction, they still rely on a human-designed "skeleton."

The authors ask a more fundamental question: Can a neural network discover the entire data structure and query logic from scratch? This is a massive combinatorial search problem. If successful, it could lead to "AI-designed" data structures perfectly tuned for specific real-world datasets (like IP traffic or genomic sequences) where human intuition might reach its limits.

Methods

The authors propose a two-part neural architecture to tackle this combinatorial challenge:

The two-part neural architecture for data structure discovery.
  1. The Data-Processing Network: A transformer-based model that takes a raw dataset and transforms it into a structured "database." For nearest-neighbor tasks, it learns to output a permutation—essentially deciding the optimal order in which to store the data points.
  2. The Query Execution Network: A sequence of MLP-based "query models." Given a query $q$, the first model picks a position in the database to "look up." The second model takes the result of that lookup, the original query, and the history of previous lookups to decide where to look next.

The "Secret Sauce": To make this work with gradient descent (which struggles with discrete "lookups"), the researchers used a differentiable sorting function and Gumbel-Softmax noise. This forced the model to eventually learn discrete, sparse access patterns, mimicking how a real computer accesses memory.

Datasets

The framework was rigorously tested across a variety of synthetic and real-world scenarios:

  • Synthetic 1D & 2D: Uniform, Zipfian (skewed), and "Hard" distributions (designed to have no exploitable patterns).
  • High-Dimensional: A 30D hypersphere and 100D projections of SIFT (a standard computer vision benchmark).
  • Image-based: FashionMNIST and a custom 3-digit MNIST dataset where the model had to learn the numerical value of images to sort and search them.
  • Streaming Data: The CAIDA IP traffic dataset, used to test frequency estimation in data streams.

Key Insights and Ideas

  • Emergent Classicality: When trained on "hard" distributions, the model independently discovered binary search (in 1D) and structures resembling k-d trees (in 2D). It wasn't explicitly programmed to do this; these structures simply emerged as the most efficient way to meet the query budget.
  • Exploiting Distribution: On uniform data, the model outperformed binary search by learning a version of interpolation search. Instead of starting in the middle, it learned to "guess" where a value might be based on its magnitude, requiring fewer lookups to reach high accuracy.
Accuracy comparison showing the end-to-end (E2E) model outperforming standard binary search.
  • Representation Learning: In the 3-digit MNIST experiment, the model learned to extract numerical features from raw pixels, sort the images by those numbers, and search them—all without being given the labels. It essentially built a visual index from scratch.
  • Human-in-the-loop Insights: By reverse-engineering the learned model for frequency estimation, the authors found it used a smaller "update delta" than the classical CountMinSketch algorithm. Applying this simple tweak back to the classical algorithm improved its performance on real IP traffic by 1.9x.

Key Results

  • 2D Nearest Neighbor: The E2E model achieved 75% accuracy compared to 52% for a standard k-d tree under the same lookup budget.
  • MNIST Search: The model reached ~98% accuracy in finding the numerically nearest neighbor image, significantly beating locality-sensitive hashing (LSH) baselines.
  • High-D Search: On SIFT and FashionMNIST, the model performed competitively with specialized "learning-to-hash" algorithms, proving the framework scales beyond simple toy problems.

Limitations and Future Work

The authors are transparent about the hurdles remaining before this can replace standard libraries:

  • Scalability: The current experiments are limited to small datasets ($N=100$ to $500$). The $O(N^2)$ complexity of the transformer pre-processing makes scaling to millions of points difficult.
  • Inference Cost: While the number of "lookups" is small, each lookup is decided by a neural network forward pass, which is much slower than a standard pointer-chase in C++.
  • Interpretability: While 1D and 2D solutions were easy to reverse-engineer, the logic the model uses in 100D remains a "black box."

Future Direction: The authors suggest using "AI-in-the-loop" workflows, where these models are used to discover new heuristics that humans then formalize into provably efficient, code-based data structures.

Summary updated

BibTeX
@inproceedings{salemohamed2025discovering,
  author = {Omar Salemohamed and Laurent Charlin and Shivam Garg and Vatsal Sharan and Gregory Valiant},
  title = {{Discovering Data Structures: Nearest Neighbor Search and Beyond}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=DOaqwuhEjc},
}
Figure