Wolfram Language

Graphs and Networks

Nearest Neighbor Graphs

Generate the nearest neighbor graph of a collection of arbitrary data with the new NearestNeighborGraph function in Version 11.

Construct the nearest neighbor graph for the countries of South America.

In[1]:=
Click for copyable input
locs = CountryData["SouthAmerica"]; coords = EntityValue[locs, "Position"][[All, 1, {2, 1}]];
In[2]:=
Click for copyable input
g = NearestNeighborGraph[locs, VertexCoordinates -> coords]
Out[2]=

Style the network atop the continent.

show complete Wolfram Language input
In[3]:=
Click for copyable input
opts = { PlotTheme -> "Web", Prolog -> {EdgeForm[Thin], LightGray, EntityValue[locs, "Polygon"]}, PlotRange -> Reverse[GeoBounds[locs]] };
In[4]:=
Click for copyable input
Graph[g, opts]
Out[4]=

Show the 3-nearest neighbor graph for a collection of random colors.

In[5]:=
Click for copyable input
colors = RandomColor[50]
Out[5]=
In[6]:=
Click for copyable input
NearestNeighborGraph[colors, 3, VertexShapeFunction -> "Name"]
Out[6]=

Related Examples

de es fr ja ko pt-br ru zh