I have a code that generates coordinate points for a 2 dimensional lattice. So the coordinates are of the form {x,y}. Now, I generate a graph out of these coordinates by connecting the nearest neighbors with an edge and translating it to a graph problem.
Let us say the data points are stored in object data. Then calculating the distances between the two coordinates, I determine if they are neighbors if the distance is d1, else they are not. This information is stored inside file mat.
This is my MWE:
distance[z1_,z2_]=1/2 ArcCosh[1 + (2 Abs[z1 - z2]^2)/((1 - Abs[z1]^2) (1 - Abs[z2]^2))];
(Generating a graph from data points )
mat = 0 IdentityMatrix[1*Length[data]];
DistributeDefinitions[d1, math, data];
(Constructing the graph )
mat =
ParallelTable[
Table[If[0 < dist[data[[i]], data[[j]]] < 1.01 d1,
mat[[i, j]] = 1, mat[[i, j]] = 0], {i,
Length[data]}], {j, Length[data]}];
NearestNeighborGraph[data, {All, 1.01 d0}, DistanceFunction -> dist, VertexLabels -> None]
However, if the file size of data i.e., is stored in data is huge like 155k or so. The above code fails miserably, and it even crashes sometimes. Is there a way to write more efficiently and faster?
If I use NearestNeighborGraph, the problem is, instead of having an edge between the points, I am having two directed lines in opposite directions between the vertices. This only happens for large values of data.
The part of the graph that has the issue. It means there is only one directed arrow to some points!!! However if I write a Adjacency matrix by finding the distance between the points, and to connect them if it equals to some value, then it works! Where am I going wrong?
CORRECTIONS:
Finally, the code is working with this new change NearestNeighborGraph[data, {All, 1.01 d0}, DistanceFunction -> dist, VertexLabels -> None]. However, it is pathetically slow than my table code.

NearestNeighborGraph. – Henrik Schumacher Aug 05 '22 at 14:02ParallelTable, there is no need to setmat[[ ... ]]inside, If you want to do that, removemat =and useParallelDo. For creating a zero matrix, you can useConstantArray. – Ben Izd Aug 05 '22 at 14:03distance[z1_,z2_]=1/2 ArcCosh[1 + (2 Abs[z1 - z2]^2)/((1 - Abs[z1]^2) (1 - Abs[z2]^2))];, wherezstores coordinates point in 2D. – Shamina Aug 05 '22 at 14:08DistanceFunctionto set any distance function you want, but a complicated enough distance function may end up looking at all pairs of distances. – Barry Carter Aug 05 '22 at 14:11DistanceFunctionwill still work even if the data point set is one dimensional i.e., without any partitioning? – Shamina Aug 05 '22 at 17:13NearestNeighborGraphthe optionDirectedEdges -> False, does it work? I'd imagine it's happening due to numerical errors causing thedistancefunction to be asymmetrical, though, and if so those errors might warrant correction somehow instead of just being "papered over" by settingDirectedEdges -> False. I'm not sure, though. – thorimur Aug 11 '22 at 02:23