# Modifying edge weights in nearest neighbor graph with custom distance function

Hello community,

I am currently working on a project in Mathematica that involves the creation of a graph using nearest neighbors. I have a simplified version of my code, and I am facing some challenges that I hope the community can help me with.

Here’s the code:

data = {{7.3, -4.9, 0.2}, {7.5, -3.5, 0.1}, {4.1, -2.8, 0.3}, {0.2, 7.3, 0.1}, {7.2, -4.4, 0.3}, {-0.0, 3.5, 0.4}, {-3.2, -1.6, 0.2}, {-2.0, 4.7, 0.2}, {-7.3, 0.0, 0.3}, {-6.4, 1.6, 0.3}, {-0.4, 3.1, 0.2}, {-4.3, 0.7, 0.5}};

distanceF[u_, v_] := Sqrt[(u[[1]] - v[[1]])^2 + (u[[2]] - v[[2]])^2] + 2 k1/(k2 + (u[[3]] + v[[3]]));

k1 = 0.1;
k2 = 0.001;

nnG = NearestNeighborGraph[data[[All, 1 ;; 2]], 5, DirectedEdges -> False, DistanceFunction -> EuclideanDistance]

In this code, I am using the NearestNeighborGraph function to create a graph based on the Euclidean distances of a set of points using 2D information. I have also defined a custom distance function (distanceF) that takes into account the third coordinate of each point.

My goal is to incorporate this custom distance function to later calculate some paths based on the new weights, but I’m facing challenges in achieving this since I need the 3rd component of the original data, not considered to build the original graph.

I would greatly appreciate any guidance or suggestions from the community on how to modify my code to achieve this goal.

I am also open to use the R implementation of iGraph, but I think it lacks the ability to directly create a nearest neighbors graph.