NearestNeighborGraphCopy to clipboard.
✖
NearestNeighborGraph
gives a graph with vertices elem1,elem2,… and edges connecting each elemi to its nearest neighbors.
gives a graph connecting each elemi to its k nearest neighbors.
gives a graph connecting each elemi to at most k nearest vertices within radius r of elemi.
gives a graph connecting each elemi to all vertices within radius r of elemi.
Details and Options

- The nearest neighbor graph is also known as k-nearest neighbor graph (k-NNG).
- An element elemj is a nearest neighbor of an element elemi whenever the distance from elemi to elemj is no larger than the distance from elemi to any other element.
- An element elemj is a k-nearest neighbor of an element elemi whenever the distance from elemi to elemj is among the k
smallest distances from elemi to any other element.
- NearestNeighborGraph works for a variety of data, including numerical, geospatial, textual, and visual.
- NearestNeighborGraph has the same options as Graph, with the following additions and changes: [List of all options]
-
DirectedEdges Automatic whether edges should be taken to be directed DistanceFunction Automatic the distance metric or dissimilarity to use WorkingPrecision Automatic precision to use for numeric data - NearestNeighborGraph takes the same DistanceFunction as Nearest.
-
AlignmentPoint Center the default point in the graphic to align with AnnotationRules {} annotations for graph, edges and vertices AspectRatio Automatic ratio of height to width Axes False whether to draw axes AxesLabel None axes labels AxesOrigin Automatic where axes should cross AxesStyle {} style specifications for the axes Background None background color for the plot BaselinePosition Automatic how to align with a surrounding text baseline BaseStyle {} base style specifications for the graphic ContentSelectable Automatic whether to allow contents to be selected CoordinatesToolOptions Automatic detailed behavior of the coordinates tool DirectedEdges Automatic whether edges should be taken to be directed DistanceFunction Automatic the distance metric or dissimilarity to use EdgeLabels None labels and label placements for edges EdgeLabelStyle Automatic style to use for edge labels EdgeShapeFunction Automatic generate graphic shapes for edges EdgeStyle Automatic style used for edges EdgeWeight Automatic weights for edges Epilog {} primitives rendered after the main plot FormatType TraditionalForm the default format type for text Frame False whether to put a frame around the plot FrameLabel None frame labels FrameStyle {} style specifications for the frame FrameTicks Automatic frame ticks FrameTicksStyle {} style specifications for frame ticks GraphHighlight {} graph elements to highlight GraphHighlightStyle Automatic style for highlight GraphLayout Automatic how to lay out vertices and edges GridLines None grid lines to draw GridLinesStyle {} style specifications for grid lines ImageMargins 0. the margins to leave around the graphic ImagePadding All what extra padding to allow for labels etc. ImageSize Automatic the absolute size at which to render the graphic LabelStyle {} style specifications for labels Method Automatic details of graphics methods to use PerformanceGoal Automatic aspects of performance to try to optimize PlotLabel None an overall label for the plot PlotRange All range of values to include PlotRangeClipping False whether to clip at the plot range PlotRangePadding Automatic how much to pad the range of values PlotRegion Automatic the final display region to be filled PlotTheme $PlotTheme overall theme for the graph PreserveImageOptions Automatic whether to preserve image options when displaying new versions of the same graphic Prolog {} primitives rendered before the main plot RotateLabel True whether to rotate y labels on the frame Ticks Automatic axes ticks TicksStyle {} style specifications for axes ticks VertexCoordinates Automatic coordinates for vertices VertexLabels None labels and placements for vertices VertexLabelStyle Automatic style to use for vertex labels VertexShape Automatic graphic shape for vertices VertexShapeFunction Automatic generate graphic shapes for vertices VertexSize Medium size of vertices VertexStyle Automatic styles for vertices VertexWeight Automatic weights for vertices WorkingPrecision Automatic precision to use for numeric data

List of all options




Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Scope (10)Survey of the scope of standard use cases
Basic Uses (4)
A nearest neighbor graph for integers in the range 1 to 10:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-lycen

Generate the 2-nearest neighbor graph:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bmww1q

Generate the at most 3-nearest neighbor graph within radius 2:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-9xgq8

Generate the nearest neighbor graph within radius 3:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bgfuuc

Numerical Data (1)
Boolean Data (1)
Geo Positions (1)
Strings (1)
Images (1)
Options (89)Common values & functionality for each option
DistanceFunction (7)
By default, EuclideanDistance is used for numerical data:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-ht0408

EditDistance is used for strings:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-dblvrb

GeoDistance is used for geo positions:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-ftvh20

JaccardDissimilarity is used for Boolean data:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-tdyhe

ColorDistance is used for colors:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bc80bd

ImageDistance is used for images:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-45g7q

https://wolfram.com/xid/0dbkyjvuo8183sweqi-ddurq3

The DistanceFunction can be given as a symbol:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-ogqdwv

https://wolfram.com/xid/0dbkyjvuo8183sweqi-fqzks2


https://wolfram.com/xid/0dbkyjvuo8183sweqi-vs537w

Method (1)
Compare different methods for machine-precision data:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-600woj
In three dimensions, the "KDtree" method is faster:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-7olkka

https://wolfram.com/xid/0dbkyjvuo8183sweqi-c4x9ps

In 20 dimensions, a simple scan is faster:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-jr6hns

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cj7qhr

https://wolfram.com/xid/0dbkyjvuo8183sweqi-87agv1

WorkingPrecision (1)
Using WorkingPrecision->MachinePrecision ensures the fastest evaluation method is used:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-l64hgz

https://wolfram.com/xid/0dbkyjvuo8183sweqi-4do5ct


https://wolfram.com/xid/0dbkyjvuo8183sweqi-vkllhi

AnnotationRules (2)
DirectedEdges (1)
By default, an undirected graph is generated:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-iwq8nh

Use DirectedEdges->True to generate a directed graph:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-c8lbw

EdgeLabels (7)

https://wolfram.com/xid/0dbkyjvuo8183sweqi-z5279


https://wolfram.com/xid/0dbkyjvuo8183sweqi-1t3zd


https://wolfram.com/xid/0dbkyjvuo8183sweqi-hr4015

Use any expression as a label:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-b7zkda

Use Placed with symbolic locations to control label placement along an edge:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-fnltmw

Use explicit coordinates to place labels:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-ndfa94

Vary positions within the label:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bg84f5


https://wolfram.com/xid/0dbkyjvuo8183sweqi-33q72


https://wolfram.com/xid/0dbkyjvuo8183sweqi-hb7fqh

Use automatic labeling by values through Tooltip and StatusArea:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-dhn5kr


https://wolfram.com/xid/0dbkyjvuo8183sweqi-bd54a9

EdgeShapeFunction (6)
Get a list of built-in settings for EdgeShapeFunction:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-hfmhqu

Undirected edges including the basic line:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-fjhml

Lines with different glyphs on the edges:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-lxjrsx

Directed edges including solid arrows:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-lvr0ss


https://wolfram.com/xid/0dbkyjvuo8183sweqi-qslbt


https://wolfram.com/xid/0dbkyjvuo8183sweqi-f6ch7m

Specify an edge function for an individual edge:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-eczrhp

Combine with a different default edge function:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-jwjk2c

Draw edges by running a program:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-j0dznf

https://wolfram.com/xid/0dbkyjvuo8183sweqi-lsgwlp

EdgeShapeFunction can be combined with EdgeStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-gt52v9

EdgeShapeFunction has higher priority than EdgeStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cfn5oz

EdgeStyle (2)
EdgeWeight (2)
Specify a weight for all edges:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-8fxi6


https://wolfram.com/xid/0dbkyjvuo8183sweqi-baprxk

Use any numeric expression as a weight:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bcptub


https://wolfram.com/xid/0dbkyjvuo8183sweqi-fibbsw

GraphHighlight (3)

https://wolfram.com/xid/0dbkyjvuo8183sweqi-t1013k


https://wolfram.com/xid/0dbkyjvuo8183sweqi-ewa020


https://wolfram.com/xid/0dbkyjvuo8183sweqi-bz747

GraphHighlightStyle (2)
Get a list of built-in settings for GraphHighlightStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-lu7p68

Use built-in settings for GraphHighlightStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-qwjuvn

GraphLayout (5)
By default, the layout is chosen automatically:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bhm12s

Specify layouts on special curves:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-f5qcu6

Specify layouts that satisfy optimality criteria:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cxyo06

VertexCoordinates overrides GraphLayout coordinates:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-b8781p

Use AbsoluteOptions to extract VertexCoordinates computed using a layout algorithm:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bmls5g


https://wolfram.com/xid/0dbkyjvuo8183sweqi-bzoeiz

PlotTheme (4)
Base Themes (2)
VertexCoordinates (3)
By default, any vertex coordinates are computed automatically:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-5spor

Extract the resulting vertex coordinates using AbsoluteOptions:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-2whoh

Specify a layout function along an ellipse:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-mwyuvu

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cvx31k

Use it to generate vertex coordinates for a graph:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-uukkr

VertexCoordinates has higher priority than GraphLayout:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-hdij4u

VertexLabels (13)

https://wolfram.com/xid/0dbkyjvuo8183sweqi-c9ka50


https://wolfram.com/xid/0dbkyjvuo8183sweqi-i40mcj


https://wolfram.com/xid/0dbkyjvuo8183sweqi-whkpg

Use any expression as a label:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-kokbex

Use Placed with symbolic locations to control label placement, including outside positions:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-csc6y

Symbolic outside corner positions:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-405x2

https://wolfram.com/xid/0dbkyjvuo8183sweqi-fjgxt8


https://wolfram.com/xid/0dbkyjvuo8183sweqi-fg0pin

Symbolic inside corner positions:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-kfrv7

https://wolfram.com/xid/0dbkyjvuo8183sweqi-dr87wv

Use explicit coordinates to place the center of labels:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cqfi4r

Place all labels at the upper-right corner of the vertex and vary the coordinates within the label:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-20sv


https://wolfram.com/xid/0dbkyjvuo8183sweqi-kusfg3

Any number of labels can be used:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-fqezh

Use the argument Placed to control formatting including Tooltip:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cxzeiw

Or StatusArea:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-nddwmk

Use more elaborate formatting functions:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bd9m40

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cggp96


https://wolfram.com/xid/0dbkyjvuo8183sweqi-l3s7yb

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cljxny


https://wolfram.com/xid/0dbkyjvuo8183sweqi-cqkdbb

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bjoam1

VertexShape (5)
Use any Graphics, Image, or Graphics3D as a vertex shape:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-dvz661

Specify vertex shapes for individual vertices:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-i1s9c7

VertexShape can be combined with VertexSize:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-jqe83v

VertexShape is not affected by VertexStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-3ef8f

VertexShapeFunction has higher priority than VertexShape:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-budnxj

VertexShapeFunction (10)
Get a list of built-in collections for VertexShapeFunction:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cc7w4d

Use built-in settings for VertexShapeFunction in the "Basic" collection:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-kqz1mq


https://wolfram.com/xid/0dbkyjvuo8183sweqi-hshluc


https://wolfram.com/xid/0dbkyjvuo8183sweqi-wm2s7

Use built-in settings for VertexShapeFunction in the "Rounded" collection:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-je3pt2


https://wolfram.com/xid/0dbkyjvuo8183sweqi-cj5ftp

Use built-in settings for VertexShapeFunction in the "Concave" collection:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-f4ea1p


https://wolfram.com/xid/0dbkyjvuo8183sweqi-bfhgv4


https://wolfram.com/xid/0dbkyjvuo8183sweqi-fhb0zi

Combine with a default vertex function:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-fmawhn

Draw vertices using a predefined graphic:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-6fiu

Draw vertices by running a program:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-oroxkx

https://wolfram.com/xid/0dbkyjvuo8183sweqi-lt54lp

VertexShapeFunction can be combined with VertexStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-c5uihz

https://wolfram.com/xid/0dbkyjvuo8183sweqi-djz15m

VertexShapeFunction has higher priority than VertexStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-h9wrh2

https://wolfram.com/xid/0dbkyjvuo8183sweqi-ir0ry

VertexShapeFunction has higher priority than VertexSize:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-e414xo

VertexShapeFunction has higher priority than VertexShape:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-lnnzsy

VertexSize (8)
By default, the size of vertices is computed automatically:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-dsy1ve

Specify the size of all vertices using symbolic vertex size:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-lskxsr

Use a fraction of the minimum distance between vertex coordinates:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-3lxk1

Use a fraction of the overall diagonal for all vertex coordinates:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-my5omo

Specify size in both the and
directions:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-c7twgs

Specify the size for individual vertices:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-h6r4ja

VertexSize can be combined with VertexShapeFunction:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-b3jw4t

VertexSize can be combined with VertexShape:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-dego1v

VertexStyle (5)

https://wolfram.com/xid/0dbkyjvuo8183sweqi-ehbi7i


https://wolfram.com/xid/0dbkyjvuo8183sweqi-dtkst6

VertexShapeFunction can be combined with VertexStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-om30z

https://wolfram.com/xid/0dbkyjvuo8183sweqi-nnfnwx

VertexShapeFunction has higher priority than VertexStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-kfv4n

https://wolfram.com/xid/0dbkyjvuo8183sweqi-cldcke

VertexStyle can be combined with BaseStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bby1h9

VertexStyle has higher priority than BaseStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-d0df6

VertexShape is not affected by VertexStyle:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-3rqw0

VertexWeight (2)
Set the weight for all vertices:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-coy9ti


https://wolfram.com/xid/0dbkyjvuo8183sweqi-jjkrix

Use any numeric expression as a weight:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-lxjpz1


https://wolfram.com/xid/0dbkyjvuo8183sweqi-pqum6

Applications (7)Sample problems that can be solved with this function
Basic Uses (2)
Graphs & Networks (2)
Geometry (1)
Proximity Searching (2)
Search nearest image neighbors:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-fx2eao


https://wolfram.com/xid/0dbkyjvuo8183sweqi-f6tp4g

Search the dictionary neighbors:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-dm38lp


https://wolfram.com/xid/0dbkyjvuo8183sweqi-oyti9v

Properties & Relations (7)Properties of the function, and connections to other functions
Use VertexCount and EdgeCount to count vertices and edges:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-ewldet


https://wolfram.com/xid/0dbkyjvuo8183sweqi-buy2ca

Use VertexList and EdgeList to enumerate vertices and edges in standard order:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-l0u8q


https://wolfram.com/xid/0dbkyjvuo8183sweqi-l06m

Compute the AdjacencyMatrix from a graph:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-bil8h7

https://wolfram.com/xid/0dbkyjvuo8183sweqi-fr4mw2

Distance metric generates undirected graphs:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-gplhb

Dissimilarity generates directed graphs:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-jz23nv

Generate a nearest neighbor graph within radius 0.2 by using SpatialGraphDistribution:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-e87nl2

Use Nearest and NearestFunction get the nearest neighbor graph:

https://wolfram.com/xid/0dbkyjvuo8183sweqi-eaeqzb

https://wolfram.com/xid/0dbkyjvuo8183sweqi-eam8mb


https://wolfram.com/xid/0dbkyjvuo8183sweqi-lka5l0

https://wolfram.com/xid/0dbkyjvuo8183sweqi-rr9mn


https://wolfram.com/xid/0dbkyjvuo8183sweqi-bc00f3

Wolfram Research (2015), NearestNeighborGraph, Wolfram Language function, https://reference.wolfram.com/language/ref/NearestNeighborGraph.html.
Text
Wolfram Research (2015), NearestNeighborGraph, Wolfram Language function, https://reference.wolfram.com/language/ref/NearestNeighborGraph.html.
Wolfram Research (2015), NearestNeighborGraph, Wolfram Language function, https://reference.wolfram.com/language/ref/NearestNeighborGraph.html.
CMS
Wolfram Language. 2015. "NearestNeighborGraph." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NearestNeighborGraph.html.
Wolfram Language. 2015. "NearestNeighborGraph." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NearestNeighborGraph.html.
APA
Wolfram Language. (2015). NearestNeighborGraph. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NearestNeighborGraph.html
Wolfram Language. (2015). NearestNeighborGraph. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NearestNeighborGraph.html
BibTeX
@misc{reference.wolfram_2025_nearestneighborgraph, author="Wolfram Research", title="{NearestNeighborGraph}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/NearestNeighborGraph.html}", note=[Accessed: 15-April-2025
]}
BibLaTeX
@online{reference.wolfram_2025_nearestneighborgraph, organization={Wolfram Research}, title={NearestNeighborGraph}, year={2015}, url={https://reference.wolfram.com/language/ref/NearestNeighborGraph.html}, note=[Accessed: 15-April-2025
]}