This is really just a "fancy" distance function, so it doesn't really require anything special from GH and probably wouldn't be faster either (since GH and you would probably both use the DistanceTo function). Anyway, here's some basic psuedo code if you need it...
For each point
Find distance to all other points
Sort according to distance
Cull based on Min/Max dist if desired,
Find the N shortest distances and make lines.
If you're really looking for speed, distance functions are notorious for offering some good speed increases. Basically, since distance functions require taking the square root of a value, they tend to be very slow because of that. However, if you write your own distance function without the square root and account for that in the other places within your code (by squaring the test distances for instance) you can get around taking that square root and speed things up.
In the case of this function, you can easily scrap the square root, since you're only looking for how many points are the closest. Even if you do want to implement the min/max radius thing, squaring those values will still allow you to bypass the square root. HTH