NearestNeighborGraph

NearestNeighborGraph[{elem1,elem2,}]

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

NearestNeighborGraph[{elem1,elem2,},k]

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

NearestNeighborGraph[{elem1,elem2,},{k,r}]

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

NearestNeighborGraph[{elem1,elem2,},{All,r}]

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^(th) 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:
  • DirectedEdgesAutomaticwhether edges should be taken to be directed
    DistanceFunctionAutomaticthe distance metric or dissimilarity to use
    WorkingPrecisionAutomaticprecision to use for numeric data
  • NearestNeighborGraph takes the same DistanceFunction as Nearest.

Examples

open allclose all

Basic Examples  (2)

A nearest neighbor graph:

In[1]:=
Click for copyable input
Out[1]=

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

In[1]:=
Click for copyable input
Out[1]=

Scope  (10)

Options  (89)

Applications  (7)

Properties & Relations  (7)

See Also

DistanceMatrix  Nearest  ClusteringTree  SpatialGraphDistribution  RelationGraph  NestGraph  GeoNearest  DimensionReduce

Introduced in 2015
(10.2)