# Speeding and avoiding unwanted crash of nested Table by NearestNeighborGraph

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.

• Mathematica has functions to work with graphs directly reference.wolfram.com/language/ref/NearestNeighborGraph.html -- creating a table with 24 billion elements is not the way to go Aug 5 at 13:51
• You are probably looking for something like NearestNeighborGraph. Aug 5 at 14:02
• Besides the point BarryCarter pointed out, here are some notes on your code: you're building the matrix with ParallelTable, there is no need to set mat[[ ... ]] inside, If you want to do that, remove mat = and use ParallelDo. For creating a zero matrix, you can use ConstantArray. Aug 5 at 14:03
• Have you seen this already? Aug 7 at 23:31
• If you pass NearestNeighborGraph the option DirectedEdges -> False, does it work? I'd imagine it's happening due to numerical errors causing the distance function to be asymmetrical, though, and if so those errors might warrant correction somehow instead of just being "papered over" by setting DirectedEdges -> False. I'm not sure, though. yesterday