A k-nearest neighbor graph, usually written as KNN graph or KNNG, is a data structure where every point in a dataset is connected by an edge to the k other points most similar to it. If you have ten thousand images and set k to 10, each image gets linked to the ten images closest to it according to some measure of distance or similarity. The result is a network that encodes local relationships across the entire dataset, and it turns out to be one of the most versatile structures in modern data science. KNN graphs sit underneath dimensionality reduction tools like UMAP, drive clustering in single-cell genomics, power recommendation engines, and serve as the backbone of vector search systems used in large language model pipelines.
How a KNN Graph Gets Built
The conceptually simplest way to build a KNN graph is brute force: for every point, compute its distance to every other point, sort the results, and keep the k smallest. This works fine for a few thousand points, but the cost grows with the square of the dataset size. Double the number of points and you roughly quadruple the work. For datasets in the millions or billions, brute force is impractical, which is why most real-world KNN graph construction relies on approximate methods.
A common two-step framework starts by building a rough initial graph and then refining it through a process called neighborhood propagation. The idea behind propagation is intuitive: if point A is near point B, and point B is near point C, there is a decent chance A and C are also near each other. By repeatedly checking the neighbors of neighbors, the graph improves its accuracy over several rounds without recalculating all pairwise distances. Research into this framework has explored using navigable small-world structures as the initial graph, followed by propagation-based refinement, and found that the quality of the starting graph strongly influences the final result.1arXiv. Revisiting k-Nearest Neighbor Graph Construction on High-Dimensional Data: Experiments and Analyses
The Hubness Problem in High Dimensions
KNN graphs behave differently when the data lives in many dimensions. In high-dimensional spaces, distances between points start to converge, meaning the gap between the nearest and farthest neighbor shrinks. One side effect is a phenomenon called hubness: certain points end up appearing in the neighbor lists of a disproportionately large number of other points, while some points barely appear at all. These “hubs” and “anti-hubs” distort the graph’s structure and can degrade the performance of algorithms that rely on it.
Hubness directly undermines the neighborhood propagation step used in many graph construction algorithms. When a few hub points dominate the neighbor lists, the propagation rounds keep revisiting the same connections rather than discovering genuinely new nearby points. Research has confirmed a strong link between the degree of hubness in a dataset and the degraded performance of propagation-based methods like NN-Descent, which is one of the most widely used approximate KNN graph builders.2International Journal on Artificial Intelligence Tools. The Influence of Hubness on NN-Descent This is not just a theoretical curiosity. Genomics data, text embeddings, and image feature vectors routinely have hundreds or thousands of dimensions, placing them squarely in the zone where hubness matters.
Approximate Methods for Large-Scale Construction
Because exact KNN graph construction is too expensive for most real datasets, a family of approximate algorithms has emerged, each trading a small amount of accuracy for a large savings in compute time. Two prominent strategies are graph-based navigable structures and locality-sensitive hashing.
Hierarchical Navigable Small World graphs, known as HNSW, build a multi-layered graph where higher layers contain fewer, more widely spaced points and lower layers are denser. A search starts at the top layer and hops downward, narrowing in on the target region. HNSW was originally designed for approximate nearest neighbor search, but the graph it constructs along the way is itself a high-quality KNN graph that can be used directly or as a starting point for further refinement.3arXiv. Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs
Locality-sensitive hashing takes a different approach. It uses hash functions designed so that similar points are more likely to land in the same hash bucket. By only comparing points within the same bucket, the algorithm avoids most of the unnecessary distance calculations. One implementation, Variable Resolution LSH, tunes the hash resolution automatically to balance accuracy against cost, producing an approximate KNN graph at significantly reduced computational expense.4ACM Transactions on Intelligent Systems and Technology. Fast Distributed k NN Graph Construction Using Auto-tuned Locality-sensitive Hashing
How the Choice of k Shapes the Graph
Setting k is one of those decisions that looks minor but has outsized effects. A small k produces a sparse graph where each point connects only to its immediate neighborhood. This captures fine-grained local structure but can fragment the graph into disconnected clusters, especially in noisy data. A large k creates a denser graph that is better connected but starts to blur the boundaries between distinct groups.
Theoretical work on this tradeoff has produced a somewhat counterintuitive finding. For the purpose of identifying clusters in noisy data, k needs to be much higher than most practitioners assume. Analysis using random geometric graph theory showed that to maximize the probability of correctly identifying clusters, k should grow roughly in proportion to the number of data points, not logarithmically as is often assumed.5Theoretical Computer Science. Optimal construction of k-nearest-neighbor graphs for identifying noisy clusters In practice, most users pick k somewhere between 5 and 50 based on convention or quick experimentation, so there is likely a gap between common practice and theoretically optimal settings, at least for clustering tasks.
Mutual KNN Graphs and Other Variants
A standard KNN graph is directed: point A might list point B as a neighbor without B reciprocating. This asymmetry is a natural consequence of the construction process and is closely related to the hubness problem. A hub has many incoming edges from points that consider it a neighbor, but it only has k outgoing edges of its own.
A mutual KNN graph keeps only the edges where both endpoints consider each other neighbors. This eliminates hubs entirely, since no point can have more than k connections. The tradeoff is connectivity. In a study on music recommendation data, converting a standard KNN graph (with k set to 5) to a mutual version cut the number of edges from over 38,000 to under 6,000 and left a large fraction of points as anti-hubs with few or no connections. Reachability, meaning the share of the catalog a user could discover by following links, dropped to about 40%. Adding a minimum spanning tree to the mutual graph restored full reachability while keeping the hub-suppressing benefits, pushing reachability back to 100% with zero anti-hubs.6PubMed Central. Mutual proximity graphs for improved reachability in music recommendation
This connectivity-versus-hubness tradeoff shows up in any application where fairness of exposure matters. In a recommendation system, hubs correspond to items that get recommended to everyone, while anti-hubs are items nobody ever sees. The mutual variant with a spanning-tree patch is one practical solution.
Which Distance Metric to Use
A KNN graph is only as meaningful as the distance measure used to define “nearest.” Euclidean distance is the default in many implementations, but it is not always the best choice. For high-dimensional data, angular measures like cosine similarity often perform better because they are less sensitive to differences in overall magnitude and focus instead on the direction of the data vectors.
A systematic comparison across six different similarity network strategies for classifying biological data found that cosine similarity consistently outperformed more complex alternatives, including kernel-based and hybrid approaches, across multiple evaluation metrics. It also showed the lowest variability across different cross-validation splits, meaning it was more stable in addition to being more accurate.7PubMed Central. Effects of similarity networks in graph-based multi-omics classification The takeaway is not that cosine similarity is always the right answer, but that more elaborate distance measures do not automatically yield better graphs. The “right” metric depends on what structure in the data you care about, and simpler options deserve a fair trial.
KNN Graphs Inside UMAP and Dimensionality Reduction
If you have used UMAP to visualize a high-dimensional dataset as a two-dimensional scatter plot, you have used a KNN graph, whether you realized it or not. UMAP’s first step is building a weighted graph from the data where the edges reflect local neighborhood relationships. It constructs what is formally called a fuzzy simplicial complex, but in practical terms it is a KNN graph whose edge weights encode how strongly each pair of neighbors is connected. The second step optimizes a low-dimensional layout that preserves as much of that graph’s structure as possible.8PubMed Central. Parametric UMAP Embeddings for Representation and Semisupervised Learning
This means the quality of your UMAP plot depends directly on the quality of the underlying KNN graph. If k is too small, the graph is too sparse and the embedding can fracture continuous structures into artificial islands. If k is too large, fine-grained distinctions between subtypes get blurred. The k parameter in UMAP (called n_neighbors in the software) is one of the most consequential settings a user can adjust, and its effect flows entirely through the KNN graph.
Single-Cell Biology and KNN Graphs
KNN graphs have become the default data structure in single-cell genomics, where researchers measure gene expression in individual cells and need to identify distinct cell types within a tissue sample. A typical experiment might produce expression profiles for hundreds of thousands or even millions of cells, each described by thousands of gene measurements. Building a KNN graph over these cells and then running community detection on the graph is the standard approach in tools like Scanpy and Seurat.
The scale of modern single-cell datasets has pushed the development of specialized KNN-based clustering methods. One framework designed for million-cell-scale data reported clustering 1.46 million cells in an average of about six minutes, saving anywhere from a third to 99% of the running time compared to existing methods.9PubMed Central. CDSKNN XMBD: a novel clustering framework for large-scale single-cell data based on a stable graph structure Another approach uses graph neural networks layered on top of the KNN graph to capture cell-cell relationships that go beyond simple clustering, including trajectory inference and modeling how cells transition between states over time.10Nature Communications. scGNN is a novel graph neural network framework for single-cell RNA-Seq analyses
One persistent challenge is that a fixed k treats all parts of the dataset equally, which is a problem when cell types vary widely in abundance. A rare cell type with only a few dozen members gets overwhelmed when k is set to a value optimized for the dominant populations. Adaptive methods that vary k across the graph have been developed specifically to address this, allowing both abundant and rare cell types to be identified from the same graph.11PubMed Central. aKNNO: single-cell and spatial transcriptomics clustering with an optimized adaptive k-nearest neighbor graph
Semi-Supervised Learning on KNN Graphs
When you have labels for some of your data but not all of it, a KNN graph provides a natural way to spread label information from the known points to the unknown ones. The idea is straightforward: if most of a point’s neighbors carry the same label, there is a good chance the point itself belongs to that category. This is the basis of label propagation, one of the oldest and most intuitive semi-supervised methods.
More recent work has extended this idea by propagating not just hard labels but entire probability distributions. Instead of saying “this point is category A,” the method assigns something like “70% chance of A, 25% chance of B, 5% chance of C” and passes those soft labels along the graph edges. This approach, which uses the KNN graph as its propagation backbone, allows uncertainty to flow through the network rather than being forced into premature hard decisions.12Information Systems. Bayesian label distribution propagation: A semi-supervised probabilistic k nearest neighbor classifier The result is more robust classification, especially in border regions between clusters where the true label is genuinely ambiguous.
Dynamic Graphs for Point Clouds and 3D Data
Most KNN graphs are built once and then used as a static structure. But in some applications, the graph needs to evolve as the representation of the data changes. Point cloud processing for 3D object recognition is a clear example. A 3D object is represented as a cloud of points in space, and the initial KNN graph captures spatial proximity: which points are physically close together. But as a neural network processes the data and learns higher-level features, the notion of “nearby” shifts from geometric closeness to semantic similarity.
Dynamic Graph CNN, or DGCNN, recomputes the KNN graph at each layer of the network using the current feature representation rather than the original spatial coordinates. This allows the network to discover groupings that have nothing to do with physical proximity. Two points on opposite sides of an object that share the same surface texture or curvature profile might become neighbors in feature space even though they are spatially far apart. This dynamic recomputation enables information to propagate across long distances in the original point cloud, improving performance on tasks like shape classification and part segmentation.13ACM Transactions on Graphics. Dynamic Graph CNN for Learning on Point Clouds
Scaling to Billions of Points
The explosive growth of embedding-based systems, from search engines to recommendation platforms to retrieval-augmented generation in AI, has created demand for KNN graphs over datasets with a billion or more points. At this scale, even efficient approximate algorithms running on a single machine can take days. The solution has been to distribute the work across multiple machines equipped with GPUs or other accelerators.
A distributed system called SOLANET, designed for GPU-accelerated clusters, demonstrated how parallelism can slash construction times. On benchmark datasets with one to two billion points, it achieved roughly 11 times faster processing when scaling from 32 to 512 accelerator units on billion-point datasets, and about 7 times faster on a two-billion-point dataset across the same range.14arXiv. SOLANET: Distributed Neighbor Graph Construction on GPU-Accelerated Systems The scaling is not perfectly linear because of communication overhead between machines, but it brings construction times for billion-scale graphs from impractical to routine.
Making KNN Graphs More Robust
Because so many downstream tasks depend on the KNN graph, any errors or weaknesses in the graph propagate into whatever system uses it. Noisy data can introduce spurious edges that connect unrelated points, and adversarial inputs, whether deliberate or accidental, can distort the graph in ways that degrade clustering, classification, or search quality.
One line of defense is graph pruning guided by robustness analysis. Rather than accepting every edge the KNN construction produces, these methods evaluate which edges are most vulnerable to perturbation and remove them proactively. A spectral analysis approach identifies and prunes edges that would be most easily exploited by adversarial attacks, producing a sparser graph that resists noise and deliberate manipulation better than the unpruned version.15arXiv. Adversarial-Robustness-Guided Graph Pruning The pruned graph is not just a defensive measure; it often performs comparably or better on clean data too, since the removed edges were the weakest and most ambiguous connections in the first place.
This robustness concern matters especially in security-sensitive contexts like fraud detection or medical diagnosis, where an attacker might inject carefully crafted data points to manipulate the graph and, by extension, the decisions it feeds into. Building the graph is only step one; auditing and hardening it is increasingly recognized as step two.

