WOLFRAM

Copy to clipboard.
NearestNeighborGraph[{elem1,elem2,}]

gives a graph with vertices elem1,elem2, and edges connecting each elemi to its nearest neighbors.

Copy to clipboard.
NearestNeighborGraph[{elem1,elem2,},k]

gives a graph connecting each elemi to its k nearest neighbors.

Copy to clipboard.
NearestNeighborGraph[{elem1,elem2,},{k,r}]

gives a graph connecting each elemi to at most k nearest vertices within radius r of elemi.

Copy to clipboard.
NearestNeighborGraph[{elem1,elem2,},{All,r}]

gives a graph connecting each elemi to all vertices within radius r of elemi.

Details and Options

Examples

open allclose all

Basic Examples  (2)Summary of the most common use cases

A nearest neighbor graph:

Out[1]=1

Generate the k-nearest neighbor graph for integers in the range 1 to 10:

Out[1]=1

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:

Out[1]=1

Generate the 2-nearest neighbor graph:

Out[1]=1

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

Out[1]=1

Generate the nearest neighbor graph within radius 3:

Out[1]=1

Numerical Data  (1)

A nearest neighbor graph for two-dimensional numerical data:

Out[1]=1

Three-dimensional dataset:

Out[2]=2

Boolean Data  (1)

A nearest neighbor graph for a set of Boolean data:

Out[1]=1

Geo Positions  (1)

Generate the nearest geodesic distance neighbor graph:

Out[1]=1

Strings  (1)

Generate the 2-nearest strings neighbor graph:

Out[1]=1

Images  (1)

Generate the 2-nearest images neighbor graph:

Out[2]=2

Colors  (1)

Generate the 2-nearest colors neighbor graph:

Out[1]=1

Options  (89)Common values & functionality for each option

DistanceFunction  (7)

By default, EuclideanDistance is used for numerical data:

Out[3]=3

EditDistance is used for strings:

Out[1]=1

GeoDistance is used for geo positions:

Out[1]=1

JaccardDissimilarity is used for Boolean data:

Out[1]=1

ColorDistance is used for colors:

Out[1]=1

ImageDistance is used for images:

Out[2]=2

The DistanceFunction can be given as a symbol:

Out[2]=2

Or as a pure function:

Out[3]=3

Method  (1)

Compare different methods for machine-precision data:

In three dimensions, the "KDtree" method is faster:

In 20 dimensions, a simple scan is faster:

WorkingPrecision  (1)

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

Out[5]=5
Out[6]=6

AnnotationRules  (2)

Specify an annotation for vertices:

Out[1]=1

Edges:

Out[1]=1

DirectedEdges  (1)

By default, an undirected graph is generated:

Out[1]=1

Use DirectedEdges->True to generate a directed graph:

Out[2]=2

EdgeLabels  (7)

Label the edge 12:

Out[1]=1

Label all edges individually:

Out[1]=1
Out[2]=2

Use any expression as a label:

Out[1]=1

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

Out[1]=1

Use explicit coordinates to place labels:

Out[1]=1

Vary positions within the label:

Out[2]=2

Place multiple labels:

Out[1]=1
Out[2]=2

Use automatic labeling by values through Tooltip and StatusArea:

Out[1]=1
Out[2]=2

EdgeShapeFunction  (6)

Get a list of built-in settings for EdgeShapeFunction:

Out[1]=1

Undirected edges including the basic line:

Out[1]=1

Lines with different glyphs on the edges:

Out[2]=2

Directed edges including solid arrows:

Out[1]=1

Line arrows:

Out[2]=2

Open arrows:

Out[3]=3

Specify an edge function for an individual edge:

Out[1]=1

Combine with a different default edge function:

Out[2]=2

Draw edges by running a program:

Out[2]=2

EdgeShapeFunction can be combined with EdgeStyle:

Out[1]=1

EdgeShapeFunction has higher priority than EdgeStyle:

Out[2]=2

EdgeStyle  (2)

Style all edges:

Out[1]=1

Style individual edges:

Out[1]=1

EdgeWeight  (2)

Specify a weight for all edges:

Out[1]=1

Use any numeric expression as a weight:

Out[1]=1

GraphHighlight  (3)

Highlight the vertex 1:

Out[1]=1

Highlight the edge 23:

Out[1]=1

Highlight vertices and edges:

Out[1]=1

GraphHighlightStyle  (2)

Get a list of built-in settings for GraphHighlightStyle:

Out[1]=1

Use built-in settings for GraphHighlightStyle:

Out[1]=1

GraphLayout  (5)

By default, the layout is chosen automatically:

Out[1]=1

Specify layouts on special curves:

Out[1]=1

Specify layouts that satisfy optimality criteria:

Out[1]=1

VertexCoordinates overrides GraphLayout coordinates:

Out[1]=1

Use AbsoluteOptions to extract VertexCoordinates computed using a layout algorithm:

Out[1]=1
Out[2]=2

PlotTheme  (4)

Base Themes  (2)

Use a common base theme:

Out[1]=1

Use a monochrome theme:

Out[1]=1

Feature Themes  (2)

Use a large graph theme:

Out[1]=1

Use a classic diagram theme:

Out[1]=1

VertexCoordinates  (3)

By default, any vertex coordinates are computed automatically:

Out[1]=1

Extract the resulting vertex coordinates using AbsoluteOptions:

Out[2]=2

Specify a layout function along an ellipse:

Out[2]=2

Use it to generate vertex coordinates for a graph:

Out[3]=3

VertexCoordinates has higher priority than GraphLayout:

Out[1]=1

VertexLabels  (13)

Use vertex names as labels:

Out[1]=1

Label individual vertices:

Out[1]=1

Label all vertices:

Out[1]=1

Use any expression as a label:

Out[1]=1

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

Out[1]=1

Symbolic outside corner positions:

Out[2]=2

Symbolic inside positions:

Out[1]=1

Symbolic inside corner positions:

Out[2]=2

Use explicit coordinates to place the center of labels:

Out[1]=1

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

Out[1]=1

Place multiple labels:

Out[1]=1

Any number of labels can be used:

Out[2]=2

Use the argument Placed to control formatting including Tooltip:

Out[1]=1

Or StatusArea:

Out[2]=2

Use more elaborate formatting functions:

Out[2]=2
Out[4]=4
Out[6]=6

VertexShape  (5)

Use any Graphics, Image, or Graphics3D as a vertex shape:

Out[1]=1

Specify vertex shapes for individual vertices:

Out[1]=1

VertexShape can be combined with VertexSize:

Out[1]=1

VertexShape is not affected by VertexStyle:

Out[1]=1

VertexShapeFunction has higher priority than VertexShape:

Out[1]=1

VertexShapeFunction  (10)

Get a list of built-in collections for VertexShapeFunction:

Out[1]=1

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

Out[1]=1

Simple basic shapes:

Out[2]=2

Common basic shapes:

Out[3]=3

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

Out[1]=1
Out[2]=2

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

Out[1]=1
Out[2]=2

Draw individual vertices:

Out[1]=1

Combine with a default vertex function:

Out[2]=2

Draw vertices using a predefined graphic:

Out[1]=1

Draw vertices by running a program:

Out[2]=2

VertexShapeFunction can be combined with VertexStyle:

Out[2]=2

VertexShapeFunction has higher priority than VertexStyle:

Out[4]=4

VertexShapeFunction has higher priority than VertexSize:

Out[1]=1

VertexShapeFunction has higher priority than VertexShape:

Out[1]=1

VertexSize  (8)

By default, the size of vertices is computed automatically:

Out[1]=1

Specify the size of all vertices using symbolic vertex size:

Out[1]=1

Use a fraction of the minimum distance between vertex coordinates:

Out[1]=1

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

Out[1]=1

Specify size in both the and directions:

Out[1]=1

Specify the size for individual vertices:

Out[1]=1

VertexSize can be combined with VertexShapeFunction:

Out[1]=1

VertexSize can be combined with VertexShape:

Out[1]=1

VertexStyle  (5)

Style all vertices:

Out[1]=1

Style individual vertices:

Out[1]=1

VertexShapeFunction can be combined with VertexStyle:

Out[2]=2

VertexShapeFunction has higher priority than VertexStyle:

Out[4]=4

VertexStyle can be combined with BaseStyle:

Out[1]=1

VertexStyle has higher priority than BaseStyle:

Out[2]=2

VertexShape is not affected by VertexStyle:

Out[1]=1

VertexWeight  (2)

Set the weight for all vertices:

Out[1]=1
Out[2]=2

Use any numeric expression as a weight:

Out[1]=1
Out[2]=2

Applications  (7)Sample problems that can be solved with this function

Basic Uses  (2)

Fixed-radius near neighbors:

Out[1]=1

-approximate nearest neighbor search:

Out[1]=1

Graphs & Networks  (2)

Farthest neighbor graphs:

Out[22]=22

Generate a spatial random graph within radius 0.2:

Out[1]=1

Geometry  (1)

Simplification of point-sample surfaces:

Out[3]=3
Out[2]=2

Proximity Searching  (2)

Search nearest image neighbors:

Out[1]=1
Out[2]=2

Search the dictionary neighbors:

Out[1]=1
Out[2]=2

Properties & Relations  (7)Properties of the function, and connections to other functions

Use VertexCount and EdgeCount to count vertices and edges:

Out[1]=1
Out[2]=2

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

Out[1]=1
Out[2]=2

Compute the AdjacencyMatrix from a graph:

Distance metric generates undirected graphs:

Out[1]=1

Dissimilarity generates directed graphs:

Out[1]=1

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

Out[1]=1

Use Nearest and NearestFunction get the nearest neighbor graph:

Out[2]=2
Out[4]=4

This is equivalent to:

Out[5]=5
Wolfram Research (2015), NearestNeighborGraph, Wolfram Language function, https://reference.wolfram.com/language/ref/NearestNeighborGraph.html.
Copy to clipboard.
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.

Copy to clipboard.
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.

Copy to clipboard.
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

Copy to clipboard.
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 ]}

Copy to clipboard.
@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 ]}

Copy to clipboard.
@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 ]}