({{, (RefLink[NearestNeighborGraph, paclet:ref/NearestNeighborGraph])[{(Subscript[elem, 1]),(Subscript[elem, 2]),…}] 
gives a graph with vertices (Subscript[elem, 1]),(Subscript[elem, 2]),… and edges connecting each (Subscript[elem, i]) to its nearest neighbors.}, {, (RefLink[NearestNeighborGraph, paclet:ref/NearestNeighborGraph])[{(Subscript[elem, 1]),(Subscript[elem, 2]),…},k]
gives a graph connecting each (Subscript[elem, i]) to its k nearest neighbors.}, {, (RefLink[NearestNeighborGraph, paclet:ref/NearestNeighborGraph])[{(Subscript[elem, 1]),(Subscript[elem, 2]),…},{k,r}]
gives a graph connecting each (Subscript[elem, i]) to at most k nearest vertices within radius r of (Subscript[elem, i]).}, {, (RefLink[NearestNeighborGraph, paclet:ref/NearestNeighborGraph])[{(Subscript[elem, 1]),(Subscript[elem, 2]),…},{(RefLink[All, paclet:ref/All]),r}]
gives a graph connecting each (Subscript[elem, i]) to all vertices within radius r of (Subscript[elem, i]).}})

OpenersNotesSection(Spacer1)Details and Options

The nearest neighbor graph is also known as k-nearest neighbor graph (k-NNG).

An element (Subscript[elem, j]) is a nearest neighbor of an element (Subscript[elem, i]) whenever the distance from (Subscript[elem, i]) to (Subscript[elem, j]) is no larger than the distance from (Subscript[elem, i]) to any other element.

An element (Subscript[elem, j]) is a k-nearest neighbor of an element (Subscript[elem, i]) whenever the distance from (Subscript[elem, i]) to (Subscript[elem, j]) is among the k(^th) smallest distances from (Subscript[elem, i]) to any other element.

(RefLink[NearestNeighborGraph, paclet:ref/NearestNeighborGraph]) works for a variety of data, including numerical, geospatial, textual, and visual.

(RefLink[NearestNeighborGraph, paclet:ref/NearestNeighborGraph]) has the same options as (RefLink[Graph, paclet:ref/Graph]), with the following additions and changes:

(RefLink[NearestNeighborGraph, paclet:ref/NearestNeighborGraph]) takes the same (RefLink[DistanceFunction, paclet:ref/DistanceFunction]) as (RefLink[Nearest, paclet:ref/Nearest]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (115)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

A nearest neighbor graph:

NearestNeighborGraph[RandomReal[1,{50,2}]]

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

Table[NearestNeighborGraph[Range[10],k],{k,1,6}]

OpenersExampleSection1(Spacer1)Scope  (10)

OpenersExampleSubsection0(Spacer1)Basic Uses  (4)

A nearest neighbor graph for integers in the range 1 to 10:

NearestNeighborGraph[Range[10]]

Generate the 2-nearest neighbor graph:

NearestNeighborGraph[Range[10],2]

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

NearestNeighborGraph[Range[10],{3,2}]

Generate the nearest neighbor graph within radius 3:

NearestNeighborGraph[Range[10],{All,3}]

OpenersExampleSubsection1(Spacer1)Numerical Data  (1)

A nearest neighbor graph for two-dimensional numerical data:

NearestNeighborGraph[RandomReal[1,{50,2}]]

Three-dimensional dataset:

NearestNeighborGraph[RandomReal[1,{50,3}]]

OpenersExampleSubsection2(Spacer1)Boolean Data  (1)

A nearest neighbor graph for a set of Boolean data:

NearestNeighborGraph[RandomChoice[{True,False},{50,3}],DistanceFunction->JaccardDissimilarity]

OpenersExampleSubsection3(Spacer1)Geo Positions  (1)

Generate the nearest geodesic distance neighbor graph:

NearestNeighborGraph[CountryData["SouthAmerica"],VertexShapeFunction->"Name"]

OpenersExampleSubsection4(Spacer1)Strings  (1)

Generate the 2-nearest strings neighbor graph:

NearestNeighborGraph[{"aaaa","abaa","bbbb","aaaba"},2,VertexShapeFunction->"Name"]

OpenersExampleSubsection5(Spacer1)Images  (1)

Generate the 2-nearest images neighbor graph:

g=;

NearestNeighborGraph[Flatten@ImagePartition[g,50],2,VertexShapeFunction->"Name",VertexSize->Large]

OpenersExampleSubsection6(Spacer1)Colors  (1)

Generate the 2-nearest colors neighbor graph:

NearestNeighborGraph[{,,,,,,,,,,,,,,},2,VertexShapeFunction->"Name"]

OpenersExampleSection2(Spacer1)Options  (89)

OpenersExampleSubsection7(Spacer1)DistanceFunction  (7)

By default, (RefLink[EuclideanDistance, paclet:ref/EuclideanDistance]) is used for numerical data:

NearestNeighborGraph[RandomReal[1,{50,2}]]

(RefLink[EditDistance, paclet:ref/EditDistance]) is used for strings:

NearestNeighborGraph[{"abc","xyz","bca","wxyz"}]

(RefLink[GeoDistance, paclet:ref/GeoDistance]) is used for geo positions:

NearestNeighborGraph[CountryData["SouthAmerica"]]

(RefLink[JaccardDissimilarity, paclet:ref/JaccardDissimilarity]) is used for Boolean data:

NearestNeighborGraph[RandomChoice[{True,False},{20,2}],DistanceFunction->JaccardDissimilarity]

(RefLink[ColorDistance, paclet:ref/ColorDistance]) is used for colors:

NearestNeighborGraph[{,,,,,,,,,,,,,,}]

(RefLink[ImageDistance, paclet:ref/ImageDistance]) is used for images:

g=;

NearestNeighborGraph[Flatten@ImagePartition[g,50]]

The (RefLink[DistanceFunction, paclet:ref/DistanceFunction]) can be given as a symbol:

dist[{u_,v_}, {x_,y_}] := 3Abs[u-x]+2Abs[v-y]

NearestNeighborGraph[RandomReal[2,{50,2}],DistanceFunction->dist]

Or as a pure function:

NearestNeighborGraph[RandomReal[2,{50,2}], DistanceFunction->(3Abs[#1[[1]]-#2[[1]]]+2Abs[#1[[2]]-#2[[2]]]&)]

OpenersExampleSubsection8(Spacer1)Method  (1)

Compare different methods for machine-precision data:

methods = {Automatic,"Scan", "KDtree", "Octree"};

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

data3 = RandomReal[1,{10^4,3}];

TableForm[Table[{
First[AbsoluteTiming[NearestNeighborGraph[data3,Method->m];]]},{m, methods}],TableHeadings->{methods, {"Timing"}}]

In 20 dimensions, a simple scan is faster:

methods = {Automatic,"Scan", "KDtree"};

data20 = RandomReal[1,{10^4,20}];

TableForm[Table[{
First[AbsoluteTiming[NearestNeighborGraph[data20,Method->m];]]},{m, methods}],TableHeadings->{methods, {"Timing"}}]

OpenersExampleSubsection9(Spacer1)WorkingPrecision  (1)

Using (RefLink[WorkingPrecision, paclet:ref/WorkingPrecision])->(RefLink[MachinePrecision, paclet:ref/MachinePrecision]) ensures the fastest evaluation method is used:

data = RandomReal[1,{100,2}];

First[AbsoluteTiming[NearestNeighborGraph[data]]]

First[AbsoluteTiming[Nearest[data,WorkingPrecision->MachinePrecision]]]

OpenersExampleSubsection10(Spacer1)DirectedEdges  (1)

By default, an undirected graph is generated:

NearestNeighborGraph[{"aa","ab","bb"},2]

Use (RefLink[DirectedEdges, paclet:ref/DirectedEdges])->(RefLink[True, paclet:ref/True]) to generate a directed graph:

NearestNeighborGraph[{"aa","ab","bb"},2,DirectedEdges->True]

OpenersExampleSubsection11(Spacer1)EdgeLabels  (7)

Label the edge 1<->2:

NearestNeighborGraph[Range[3],EdgeLabels->{1\[UndirectedEdge]2->"Hello"}]

Label all edges individually:

el=EdgeList@NearestNeighborGraph[{"aa","ab","bb"},2]

NearestNeighborGraph[{"aa","ab","bb"},2,EdgeLabels->Table[el[[i]]->(Subscript["e", i]),{i,Length[el]}]]

Use any expression as a label:

NearestNeighborGraph[Range[4],EdgeLabels->{1\[UndirectedEdge]2->,2\[UndirectedEdge]3->,3\[UndirectedEdge]4-> }]

Use (RefLink[Placed, paclet:ref/Placed]) with symbolic locations to control label placement along an edge:

Table[NearestNeighborGraph[Range[4],EdgeLabels->{3\[UndirectedEdge]4->Placed["■■■",p]},PlotLabel->p],{p,{"Start","Middle","End"}}]

Use explicit coordinates to place labels:

Table[NearestNeighborGraph[Range[4],EdgeLabels->{2\[UndirectedEdge]3->Placed["■■■",p]},PlotLabel->p,BaselinePosition->Bottom],{p,{0,1/4,1/3}}]

Vary positions within the label:

Table[NearestNeighborGraph[Range[4],EdgeLabels->{2\[UndirectedEdge]3->Placed["■■■",{1/2,p}]},PlotLabel->p,BaselinePosition->Bottom],{p,{{0,0},{1/2,1/2},{1,1}}}]

Place multiple labels:

NearestNeighborGraph[Range[4],EdgeLabels->{1\[UndirectedEdge]2->Placed[{"lbl1","lbl2"},{"Start","End"}]}]

NearestNeighborGraph[Range[4],EdgeLabels->{1\[UndirectedEdge]2->Placed[{"lbl1","lbl2","lbl3"},{"Start","Middle","End"}]}]

Use automatic labeling by values through (RefLink[Tooltip, paclet:ref/Tooltip]) and (RefLink[StatusArea, paclet:ref/StatusArea]):

NearestNeighborGraph[Range[4],EdgeLabels->Placed["Name",Tooltip]]

NearestNeighborGraph[Range[4],EdgeLabels->Placed["Name",StatusArea]]

OpenersExampleSubsection12(Spacer1)EdgeShapeFunction  (6)

Get a list of built-in settings for (RefLink[EdgeShapeFunction, paclet:ref/EdgeShapeFunction]):

GraphElementData["Edge"]

Undirected edges including the basic line:

NearestNeighborGraph[{"aa","ab","bb"},2,EdgeShapeFunction->"Line"]

Lines with different glyphs on the edges:

Table[NearestNeighborGraph[{"aa","ab","bb"},2,EdgeShapeFunction->GraphElementData[ef,"ArrowSize"->0.1],PlotLabel->ef],{ef,{"BoxLine","DiamondLine","DotLine"}}]

Directed edges including solid arrows:

Table[NearestNeighborGraph[{"aa","ab","bb"},2,EdgeShapeFunction->GraphElementData[ef,"ArrowSize"->0.1],PlotLabel->ef],{ef,GraphElementData["Edge","FilledArrow"]}]

Line arrows:

Table[NearestNeighborGraph[{"aa","ab","bb"},2,EdgeShapeFunction->GraphElementData[ef,"ArrowSize"->0.1],PlotLabel->ef],{ef,GraphElementData["Edge","UnfilledArrow"]}]

Open arrows:

Table[NearestNeighborGraph[{"aa","ab","bb"},2,EdgeShapeFunction->GraphElementData[ef,"ArrowSize"->0.1],PlotLabel->ef],{ef,GraphElementData["Edge","CarvedArrow"]}]

Specify an edge function for an individual edge:

NearestNeighborGraph[Range[3],EdgeShapeFunction->{1\[UndirectedEdge]2->"DotLine"}]

Combine with a different default edge function:

NearestNeighborGraph[Range[3],EdgeShapeFunction->{1\[UndirectedEdge]2->"BoxLine","DotLine"}]

Draw edges by running a program:

ef[pts_List,e_]:=
Block[{s=0.015,g=},{Arrowheads[{{s,0.33,g},{s,0.67,g}}],Arrow[pts]}]

NearestNeighborGraph[{"aa","ab","bb"},2,EdgeShapeFunction->ef]

(RefLink[EdgeShapeFunction, paclet:ref/EdgeShapeFunction]) can be combined with (RefLink[EdgeStyle, paclet:ref/EdgeStyle]):

NearestNeighborGraph[{"aa","ab","bb"},2,EdgeStyle->Blue,EdgeShapeFunction->(Line[#1]&)]

(RefLink[EdgeShapeFunction, paclet:ref/EdgeShapeFunction]) has higher priority than (RefLink[EdgeStyle, paclet:ref/EdgeStyle]):

NearestNeighborGraph[{"aa","ab","bb"},2,EdgeStyle->Blue,EdgeShapeFunction->({Red,Line[#1]}&)]

OpenersExampleSubsection13(Spacer1)EdgeStyle  (2)

Style all edges:

Table[NearestNeighborGraph[{"aa","ab","bb"},2,EdgeStyle->style,PlotLabel->style],{style,{Gray,Dashed,Thick}}]

Style individual edges:

NearestNeighborGraph[Range[3],EdgeStyle->{1\[UndirectedEdge]2->Blue,2\[UndirectedEdge]3->Dashed}]

OpenersExampleSubsection14(Spacer1)EdgeWeight  (2)

Specify a weight for all edges:

NearestNeighborGraph[{"aa","ab","bb"},2,EdgeWeight->RandomInteger[5,3]]

WeightedAdjacencyMatrix[%]//MatrixForm

Use any numeric expression as a weight:

NearestNeighborGraph[{"aa","ab","bb"},2,EdgeWeight->{a,b,c}]

WeightedAdjacencyMatrix[%]//MatrixForm

OpenersExampleSubsection15(Spacer1)GraphHighlight  (3)

Highlight the vertex 1:

NearestNeighborGraph[Range[3],VertexSize->Tiny,GraphHighlight->{1}]

Highlight the edge 2<->3:

NearestNeighborGraph[Range[3],VertexSize->Tiny,GraphHighlight->{2\[UndirectedEdge]3}]

Highlight vertices and edges:

NearestNeighborGraph[Range[3],VertexSize->Tiny,GraphHighlight->{1,2,1\[UndirectedEdge]2,2\[UndirectedEdge]3}]

OpenersExampleSubsection16(Spacer1)GraphHighlightStyle  (2)

Get a list of built-in settings for (RefLink[GraphHighlightStyle, paclet:ref/GraphHighlightStyle]):

GraphElementData["GraphHighlightStyle"]

Use built-in settings for (RefLink[GraphHighlightStyle, paclet:ref/GraphHighlightStyle]):

NearestNeighborGraph[Range[3],GraphHighlight->{1,2\[UndirectedEdge]3},VertexSize->Small,GraphHighlightStyle->#,PlotLabel->#]&/@Select[GraphElementData["GraphHighlightStyle"],#=!=Automatic&]

OpenersExampleSubsection17(Spacer1)GraphLayout  (5)

By default, the layout is chosen automatically:

NearestNeighborGraph[Range[10],GraphLayout->Automatic]

Specify layouts on special curves:

Table[NearestNeighborGraph[RandomReal[1,{30,2}],GraphLayout->l,PlotLabel->l],{l,{"CircularEmbedding","SpiralEmbedding"}}]

Specify layouts that satisfy optimality criteria:

Table[NearestNeighborGraph[Range[10],GraphLayout->l,PlotLabel->l],{l,{"SpringEmbedding","SpringElectricalEmbedding","HighDimensionalEmbedding"}}]

(RefLink[VertexCoordinates, paclet:ref/VertexCoordinates]) overrides (RefLink[GraphLayout, paclet:ref/GraphLayout]) coordinates:

{NearestNeighborGraph[Range[3],GraphLayout->"SpringElectricalEmbedding"],
NearestNeighborGraph[Range[3],GraphLayout->"SpringElectricalEmbedding",VertexCoordinates->Table[{i,i},{i,0,2}]]}

Use (RefLink[AbsoluteOptions, paclet:ref/AbsoluteOptions]) to extract (RefLink[VertexCoordinates, paclet:ref/VertexCoordinates]) computed using a layout algorithm:

NearestNeighborGraph[Range[3]]

AbsoluteOptions[%,VertexCoordinates]

OpenersExampleSubsection18(Spacer1)PlotTheme  (4)

OpenersExampleSubsubsection0(Spacer1)Base Themes  (2)

Use a common base theme:

NearestNeighborGraph[Range[3],PlotTheme->"Business"]

Use a monochrome theme:

NearestNeighborGraph[Range[3],PlotTheme->"Monochrome"]

OpenersExampleSubsubsection1(Spacer1)Feature Themes  (2)

Use a large graph theme:

NearestNeighborGraph[Range[3],PlotTheme->"LargeGraph"]

Use a classic diagram theme:

NearestNeighborGraph[Range[3],PlotTheme->"ClassicDiagram"]

OpenersExampleSubsection19(Spacer1)Properties  (2)

Specify a property for vertices:

NearestNeighborGraph[Range[3],Properties->{1->{VertexLabels-> "hello"}}]

Edges:

NearestNeighborGraph[Range[3],Properties->{1\[UndirectedEdge]2->{EdgeLabels-> "hello"}}]

OpenersExampleSubsection20(Spacer1)VertexCoordinates  (3)

By default, any vertex coordinates are computed automatically:

NearestNeighborGraph[Range[3]]

Extract the resulting vertex coordinates using (RefLink[AbsoluteOptions, paclet:ref/AbsoluteOptions]):

AbsoluteOptions[%,VertexCoordinates]

Specify a layout function along an ellipse:

ellipseLayout[n_,{a_,b_}]:=Table[{a Cos[2Pi/n u],b Sin[2Pi/n u]},{u,1,n}]

Graphics[Point[ellipseLayout[20,{2,1}]]]

Use it to generate vertex coordinates for a graph:

NearestNeighborGraph[Range[10],VertexCoordinates->ellipseLayout[10,{2,1}]]

(RefLink[VertexCoordinates, paclet:ref/VertexCoordinates]) has higher priority than (RefLink[GraphLayout, paclet:ref/GraphLayout]):

NearestNeighborGraph[Range[3],VertexCoordinates->Table[{i,i},{i,3}],GraphLayout->"CircularEmbedding"]

OpenersExampleSubsection21(Spacer1)VertexLabels  (13)

Use vertex names as labels:

NearestNeighborGraph[Range[3],VertexLabels->"Name"]

Label individual vertices:

NearestNeighborGraph[Range[3],VertexLabels->{2->"one"}]

Label all vertices:

NearestNeighborGraph[Range[4],VertexLabels->Table[i->(Subscript[v, i]),{i,4}]]

Use any expression as a label:

NearestNeighborGraph[Range[3],VertexLabels->{1->,2->,3->},ImagePadding->20]

Use (RefLink[Placed, paclet:ref/Placed]) with symbolic locations to control label placement, including outside positions:

Table[NearestNeighborGraph[Range[2],VertexSize->0.1,VertexShapeFunction->"Square",VertexLabels->Table[i->Placed["■■■",p],{i,2}],PlotLabel->p,ImagePadding->20],{p,{Before,After,Below,Above}}]

Symbolic outside corner positions:

pl={{Before,Below},{After,Below},{Before,Above},{After,Above}};

Table[NearestNeighborGraph[Range[2],VertexSize->0.1,VertexShapeFunction->"Square",ImagePadding->20,VertexLabels->Table[i->Placed["■■■",p],{i,2}],PlotLabel->p],{p,pl}]

Symbolic inside positions:

Table[NearestNeighborGraph[Range[2],VertexSize->0.45,VertexLabels->Table[i->Placed["■■■",p],{i,2}],VertexShapeFunction->"Square",PlotLabel->p],{p,{Left,Top,Right,Bottom}}]

Symbolic inside corner positions:

pl={{Left,Bottom},{Right,Bottom},{Left,Top},{Right,Top}};

Table[NearestNeighborGraph[Range[3],VertexSize->0.45,VertexShapeFunction->"Square",VertexLabels->Table[i->Placed["■■■",p],{i,2}],PlotLabel->p],{p,pl}]

Use explicit coordinates to place the center of labels:

Table[NearestNeighborGraph[Range[3],VertexSize->0.25,VertexShapeFunction->"Square",VertexLabels->Table[i->Placed[,p],{i,2}],PlotLabel->p,BaselinePosition->Bottom],{p,{{0,0},{1/2,1/2},{1,1}}}]

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

Table[NearestNeighborGraph[Range[3],VertexSize->0.35,VertexShapeFunction->"Square",VertexLabels->Table[i->Placed[,{{1,1},p}],{i,2}],PlotLabel->p,BaselinePosition->Bottom],{p,{{0,0},{1/2,1/2},{1,1}}}]

Place multiple labels:

NearestNeighborGraph[Range[3],VertexLabels->{1->Placed[{"lbl1","lbl2"},{Above,Below}]}]

Any number of labels can be used:

NearestNeighborGraph[Range[3],VertexLabels->{1->Placed[{"lbl1","lbl2","lbl3","lbl4"},{Above,After,Below,Before}]}]

Use the argument (RefLink[Placed, paclet:ref/Placed]) to control formatting including (RefLink[Tooltip, paclet:ref/Tooltip]):

NearestNeighborGraph[Range[3],VertexLabels->Placed["Name",Tooltip]]

Or (RefLink[StatusArea, paclet:ref/StatusArea]):

NearestNeighborGraph[Range[3],VertexLabels->Placed["Name",StatusArea]]

Use more elaborate formatting functions:

rotateLabel[lbl_]:=Rotate[lbl,45Degree]

NearestNeighborGraph[Range[3],VertexLabels->Table[i->Placed["xxx",Below,rotateLabel],{i,3}]]

panelLabel[lbl_]:=Panel[lbl,FrameMargins->0,Background->Lighter[Yellow,0.7]]

NearestNeighborGraph[Range[3],VertexLabels->Table[i->Placed["xxx",Center,panelLabel],{i,3}]]

hyperlinkLabel[lbl_]:=Hyperlink[lbl,"http://www.wolfram.com"]

NearestNeighborGraph[Range[3],VertexLabels->Table[i->Placed["xxx",Center,hyperlinkLabel],{i,3}]]

OpenersExampleSubsection22(Spacer1)VertexShape  (5)

Use any (RefLink[Graphics, paclet:ref/Graphics]), (RefLink[Image, paclet:ref/Image]), or (RefLink[Graphics3D, paclet:ref/Graphics3D]) as a vertex shape:

Table[NearestNeighborGraph[Range[3],VertexShape->s,VertexSize->Medium],{s,{,,}}]

Specify vertex shapes for individual vertices:

NearestNeighborGraph[Range[3],VertexShape->{2->},VertexSize->Medium]

(RefLink[VertexShape, paclet:ref/VertexShape]) can be combined with (RefLink[VertexSize, paclet:ref/VertexSize]):

Table[NearestNeighborGraph[Range[3],VertexSize->s,VertexShape->,PlotLabel->s],{s,{Small,Large}}]

(RefLink[VertexShape, paclet:ref/VertexShape]) is not affected by (RefLink[VertexStyle, paclet:ref/VertexStyle]):

NearestNeighborGraph[Range[3],VertexSize->0.2,VertexShape->,VertexStyle->Blue]

(RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) has higher priority than (RefLink[VertexShape, paclet:ref/VertexShape]):

NearestNeighborGraph[Range[3],VertexSize->0.2,VertexShapeFunction->"Square",VertexShape->]

OpenersExampleSubsection23(Spacer1)VertexShapeFunction  (10)

Get a list of built-in collections for (RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]):

GraphElementData["Vertex"]

Use built-in settings for (RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) in the "Basic" collection:

GraphElementData["Vertex","Basic"]

Simple basic shapes:

Table[NearestNeighborGraph[Range[3],VertexShapeFunction->vf,VertexSize->0.2,PlotLabel->vf],{vf,{"Triangle","Square","Rectangle","Pentagon","Hexagon","Octagon"}}]

Common basic shapes:

Table[NearestNeighborGraph[Range[3],VertexShapeFunction->vf,VertexSize->0.2,PlotLabel->vf],{vf,{"DownTrapezoid","UpTrapezoid","Parallelogram","FiveDown","Circle","Diamond","Star","Capsule"}}]

Use built-in settings for (RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) in the "Rounded" collection:

GraphElementData["Vertex","Rounded"]

Table[NearestNeighborGraph[Range[3],VertexShapeFunction->vf,VertexSize->0.2,PlotLabel->vf],{vf,GraphElementData["Vertex","Rounded"]}]

Use built-in settings for (RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) in the "Concave" collection:

GraphElementData["Vertex","Concave"]

Table[NearestNeighborGraph[Range[3],VertexShapeFunction->vf,VertexSize->0.2,PlotLabel->vf],{vf,GraphElementData["Vertex","Concave"]}]

Draw individual vertices:

NearestNeighborGraph[Range[3],VertexShapeFunction->{ 1->"Square"},VertexSize->0.2]

Combine with a default vertex function:

NearestNeighborGraph[Range[3],VertexShapeFunction->{ 1->"Square","Triangle"},VertexSize->0.2]

Draw vertices using a predefined graphic:

NearestNeighborGraph[Range[3],VertexShapeFunction->(Inset[,#]&)]

Draw vertices by running a program:

vf[{xc_,yc_},name_,{w_,h_}]:=
Block[{xmin=xc-w,xmax=xc+w,ymin=yc-h,ymax=yc+h},
Polygon[{{xmin,ymin},{xmax,ymax},{xmin,ymax},{xmax,ymin}}]
];

NearestNeighborGraph[Range[3],VertexShapeFunction->vf,VertexSize->0.2]

(RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) can be combined with (RefLink[VertexStyle, paclet:ref/VertexStyle]):

vf1[{xc_,yc_},name_,{w_,h_}]:=Rectangle[{xc-w,yc-h},{xc+w,yc+h}]

NearestNeighborGraph[Range[3],VertexSize->0.2,VertexStyle->Blue,VertexShapeFunction->vf1]

(RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) has higher priority than (RefLink[VertexStyle, paclet:ref/VertexStyle]):

vf2[{xc_,yc_},name_,{w_,h_}]:={Red,Rectangle[{xc-w,yc-h},{xc+w,yc+h}]}

NearestNeighborGraph[Range[3],VertexSize->0.2,VertexStyle->Blue,VertexShapeFunction->vf2]

(RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) has higher priority than (RefLink[VertexSize, paclet:ref/VertexSize]):

NearestNeighborGraph[Range[3],VertexShapeFunction->"Star",VertexSize->{1->Small,Medium}]

(RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) has higher priority than (RefLink[VertexShape, paclet:ref/VertexShape]):

NearestNeighborGraph[Range[3],VertexSize->0.3,VertexShapeFunction->"Star",VertexShape->]

OpenersExampleSubsection24(Spacer1)VertexSize  (8)

By default, the size of vertices is computed automatically:

NearestNeighborGraph[Range[3],VertexSize->Automatic]

Specify the size of all vertices using symbolic vertex size:

Table[NearestNeighborGraph[Range[3],VertexSize->s,PlotLabel->s],{s,{Tiny,Small,Medium,Large}}]

Use a fraction of the minimum distance between vertex coordinates:

Table[NearestNeighborGraph[Range[3],VertexSize->s,PlotLabel->s],{s,0.1,1,0.3}]

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

Table[NearestNeighborGraph[Range[3],VertexSize->{"Scaled", s},PlotLabel->{"Scaled", s}],{s,0.1,1,0.3}]

Specify size in both the x and y directions:

Table[NearestNeighborGraph[Range[3],VertexSize->s,PlotLabel->s],{s,{{0.1,0.2},{0.2,0.1}}}]

Specify the size for individual vertices:

NearestNeighborGraph[Range[3],VertexSize->{1->0.2,2->0.3}]

(RefLink[VertexSize, paclet:ref/VertexSize]) can be combined with (RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]):

Table[NearestNeighborGraph[Range[3],VertexSize->s,VertexShapeFunction->"Square",PlotLabel->s],{s,{0.05,0.1,0.2}}]

(RefLink[VertexSize, paclet:ref/VertexSize]) can be combined with (RefLink[VertexShape, paclet:ref/VertexShape]):

Table[NearestNeighborGraph[Range[3],VertexSize->s,VertexShape->,PlotLabel->s],{s,{0.1,0.2,0.4}}]

OpenersExampleSubsection25(Spacer1)VertexStyle  (5)

Style all vertices:

Table[NearestNeighborGraph[Range[3],VertexStyle->style,VertexSize->0.3,PlotLabel->style],{style,{Yellow,EdgeForm[Dashed]}}]

Style individual vertices:

NearestNeighborGraph[Range[3],VertexStyle->{1->Blue,2->Red},VertexSize->0.2]

(RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) can be combined with (RefLink[VertexStyle, paclet:ref/VertexStyle]):

vf1[{xc_,yc_},name_,{w_,h_}]:=Rectangle[{xc-w,yc-h},{xc+w,yc+h}]

NearestNeighborGraph[Range[3],VertexSize->0.2,VertexStyle->Blue,VertexShapeFunction->vf1]

(RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]) has higher priority than (RefLink[VertexStyle, paclet:ref/VertexStyle]):

vf2[{xc_,yc_},name_,{w_,h_}]:={Red,Rectangle[{xc-w,yc-h},{xc+w,yc+h}]}

NearestNeighborGraph[Range[3],VertexSize->0.2,VertexStyle->Blue,VertexShapeFunction->vf2]

(RefLink[VertexStyle, paclet:ref/VertexStyle]) can be combined with (RefLink[BaseStyle, paclet:ref/BaseStyle]):

NearestNeighborGraph[Range[3],VertexStyle->LightBlue,BaseStyle->EdgeForm[Dotted],VertexSize->0.2]

(RefLink[VertexStyle, paclet:ref/VertexStyle]) has higher priority than (RefLink[BaseStyle, paclet:ref/BaseStyle]):

NearestNeighborGraph[Range[3],VertexStyle->LightBlue,BaseStyle->Gray,VertexSize->0.2]

(RefLink[VertexShape, paclet:ref/VertexShape]) is not affected by (RefLink[VertexStyle, paclet:ref/VertexStyle]):

NearestNeighborGraph[Range[3],VertexSize->0.2,VertexShape->,VertexStyle->Blue]

OpenersExampleSubsection26(Spacer1)VertexWeight  (2)

Set the weight for all vertices:

NearestNeighborGraph[Range[3],VertexWeight->{2,3,4}]

PropertyValue[{%,1},VertexWeight]

Use any numeric expression as a weight:

NearestNeighborGraph[Range[3],VertexWeight->{a,b,c}]

PropertyValue[{%,1},VertexWeight]

OpenersExampleSection3(Spacer1)Applications  (7)

OpenersExampleSubsection27(Spacer1)Basic Uses  (2)

Fixed-radius near neighbors:

NearestNeighborGraph[RandomReal[1,{50,3}],{All,0.2}]

ϵ-approximate nearest neighbor search:

NearestNeighborGraph[RandomReal[1,{50,2}],{All,0.2}]

OpenersExampleSubsection28(Spacer1)Graphs & Networks  (2)

Farthest neighbor graphs:

NearestNeighborGraph[Range[10],1,DistanceFunction->(If[#1==#2,0,10-EuclideanDistance[#1,#2]]&)]

Generate a spatial random graph within radius 0.2:

NearestNeighborGraph[RandomReal[1,{50,2}],{All,0.2}]

OpenersExampleSubsection29(Spacer1)Geometry  (1)

Simplification of point-sample surfaces:

mr=DiscretizeGraphics[ExampleData[{"Geometry3D","Cow"}]]

NearestNeighborGraph[MeshCoordinates[mr]]

OpenersExampleSubsection30(Spacer1)Proximity Searching  (2)

Search nearest image neighbors:

(g=ImagePartition[ , 32])// Grid

NearestNeighborGraph[Flatten@g,1,VertexShapeFunction->"Name",VertexSize->Large]

Search the dictionary neighbors:

dict=Nearest[DictionaryLookup[]]["pickel",10]

NearestNeighborGraph[dict,VertexLabels->"Name"]

OpenersExampleSection4(Spacer1)Properties & Relations  (7)

Use (RefLink[VertexCount, paclet:ref/VertexCount]) and (RefLink[EdgeCount, paclet:ref/EdgeCount]) to count vertices and edges:

g=NearestNeighborGraph[RandomReal[1,{50,2}]]

{VertexCount[g],EdgeCount[g]}

Use (RefLink[VertexList, paclet:ref/VertexList]) and (RefLink[EdgeList, paclet:ref/EdgeList]) to enumerate vertices and edges in standard order:

g=NearestNeighborGraph[{"aaaa","abaa","bbbb","aaaba"},2]

{VertexList[g],EdgeList[g]}

Compute the (RefLink[AdjacencyMatrix, paclet:ref/AdjacencyMatrix]) from a graph:

g=NearestNeighborGraph[RandomReal[1,{6,2}]];

AdjacencyMatrix[g]//MatrixForm

Distance metric generates undirected graphs:

NearestNeighborGraph[RandomReal[1,{50,2}]]

Dissimilarity generates directed graphs:

NearestNeighborGraph[RandomChoice[{True,False},{50,3}],DistanceFunction->JaccardDissimilarity]

Generate a nearest neighbor graph within radius 0.2 by using (RefLink[SpatialGraphDistribution, paclet:ref/SpatialGraphDistribution]):

RandomGraph[SpatialGraphDistribution[30,0.2]]

Use (RefLink[Nearest, paclet:ref/Nearest]) and (RefLink[NearestFunction, paclet:ref/NearestFunction]) get the nearest neighbor graph:

data = Range[10];

nf = Nearest[data]

edges = Table[v\[UndirectedEdge]Last[nf[v,2]],{v,data}];

SimpleGraph[Graph[%]]

This is equivalent to:

NearestNeighborGraph[data]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[DistanceMatrix, paclet:ref/DistanceMatrix])  ▪  (RefLink[Nearest, paclet:ref/Nearest])  ▪  (RefLink[ClusteringTree, paclet:ref/ClusteringTree])  ▪  (RefLink[SpatialGraphDistribution, paclet:ref/SpatialGraphDistribution])  ▪  (RefLink[RelationGraph, paclet:ref/RelationGraph])  ▪  (RefLink[NestGraph, paclet:ref/NestGraph])  ▪  (RefLink[GeoNearest, paclet:ref/GeoNearest])  ▪  (RefLink[DimensionReduce, paclet:ref/DimensionReduce])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Graph Construction & Representation, paclet:guide/GraphConstructionAndRepresentation])}, {▪(RefLinkPlain[Cluster Analysis, paclet:guide/ClusterAnalysis])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/22-machine-learning.html])(WebLink[: Machine Learning, https://www.wolfram.com/language/elementary-introduction/22-machine-learning.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/27-applying-functions-repeatedly.html])(WebLink[: Applying Functions Repeatedly, https://www.wolfram.com/language/elementary-introduction/27-applying-functions-repeatedly.html])}})}})