--------------------------------------------------------------------------------------------------
Graph of Nearest Neighbors of Undirected Weighted Graph from tabular data
--------------------------------------------------------------------------------------------------
Graph of Nearest Neighbors of Undirected Weighted Graph from tabular data
--------------------------------------------------------------------------------------------------
===== INPUT =====
e.g., from CSV file.
Expects list (rows) of triples, e.g. {“vertex1”, “vertex2”, edgeweight}.
Edge weights assumed numerical.
Undirected (symmetric weights) assumed.
Duplicity ok, will be reduced to single edge.
A zero entry means no connection.
A vertex in the input with only zero weight connection(s) is considered isolated.
e.g., from CSV file.
Expects list (rows) of triples, e.g. {“vertex1”, “vertex2”, edgeweight}.
Edge weights assumed numerical.
Undirected (symmetric weights) assumed.
Duplicity ok, will be reduced to single edge.
A zero entry means no connection.
A vertex in the input with only zero weight connection(s) is considered isolated.
In[]:=
dataTable={{"A","B",0.1},{"A","C",0.4},{"A","D",0.2},{"A","E",0},{"B","C",0.1},{"B","D",0.5},{"C","D",0.3},{"D","A",0.2},{"F","G",0.15},{"F","H",0.25},{"G","H",0.35}};
===== COMPUTATION =====
===== COMPUTATION =====
In[]:=
vertices=DeleteDuplicates[Sort[Flatten[{#[[1]],#[[2]]}&/@dataTable]]];
In[]:=
vcount=Length[vertices];
In[]:=
verticesEdgeWeights=Table[DeleteDuplicates[If[(#[[1]]vertices[[i]])||(#[[2]]vertices[[i]]),{(#[[1]]#[[2]])&[Sort[{#[[1]],#[[2]]}]],#[[3]]},Nothing]&/@dataTable],{i,1,vcount}];
In[]:=
vertexIsolated=Table[Total[Abs[#[[2]]]&/@verticesEdgeWeights[[i]]]0,{i,1,vcount}];
In[]:=
verticesMinWeights=Table[If[vertexIsolated[[i]],∞,Min[Select[((#[[2]])&/@(verticesEdgeWeights[[i]])),(#≠0)&]]],{i,1,vcount}];
In[]:=
nnedges=DeleteDuplicates[Flatten[Table[If[vertexIsolated[[i]],{{vertices[[i]]vertices[[i]],∞}},Select[verticesEdgeWeights[[i]],(#[[2]]verticesMinWeights[[i]])&]],{i,1,vcount}],1]];
In[]:=
nncount=Length[nnedges];
===== OUTPUT =====
===== OUTPUT =====
In[]:=
nnedges
Out[]=
{{AB,0.1},{BC,0.1},{AD,0.2},{EE,∞},{FG,0.15},{FH,0.25}}
In[]:=
leastedges=Annotation[#[[1]],EdgeWeight#[[2]]]&/@nnedges
Out[]=
{AB,BC,AD,EE,FG,FH}
nng=Graph[leastedges,VertexLabelsAll,VertexLabelStyle"Medium",EdgeLabels"EdgeWeight",EdgeLabelStyleMedium,ImageSizeSmall]
Out[]=