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.
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:
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.
Examplesopen allclose all
Basic Examples (2)
Basic Uses (4)
Numerical Data (1)
By default, EuclideanDistance is used for numerical data:
EditDistance is used for strings:
GeoDistance is used for geo positions:
JaccardDissimilarity is used for Boolean data:
ColorDistance is used for colors:
ImageDistance is used for images:
The DistanceFunction can be given as a symbol:
Use Placed with symbolic locations to control label placement along an edge:
Get a list of built-in settings for EdgeShapeFunction:
Use Placed with symbolic locations to control label placement, including outside positions:
Get a list of built-in collections for VertexShapeFunction:
Use built-in settings for VertexShapeFunction in the "Basic" collection:
Use built-in settings for VertexShapeFunction in the "Rounded" collection:
Use built-in settings for VertexShapeFunction in the "Concave" collection:
Properties & Relations (7)
Compute the AdjacencyMatrix from a graph:
Generate a nearest neighbor graph within radius 0.2 by using SpatialGraphDistribution:
Wolfram Research (2015), NearestNeighborGraph, Wolfram Language function, 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.
Wolfram Language. (2015). NearestNeighborGraph. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NearestNeighborGraph.html