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 = 
   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?

enter image description here

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.

  • 6
    $\begingroup$ 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 $\endgroup$ Aug 5, 2022 at 13:51
  • 1
    $\begingroup$ You are probably looking for something like NearestNeighborGraph. $\endgroup$ Aug 5, 2022 at 14:02
  • 1
    $\begingroup$ 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. $\endgroup$
    – Ben Izd
    Aug 5, 2022 at 14:03
  • 1
    $\begingroup$ Have you seen this already? $\endgroup$ Aug 7, 2022 at 23:31
  • 1
    $\begingroup$ 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. $\endgroup$
    – thorimur
    Aug 11, 2022 at 2:23


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.