This algorithm removes the checked nodes to decrease processing time. I didn't try it on your problem but I'm sure it will help you because I tested it on another scenario and it works perfectly.

// to hold sets of neighbour nodes
Dictionary<int, List<Node>> relatedCollectionsDictionary = new Dictionary<int, List<Node>>();
int index = 0;

List<Node> nList;

while (nList.Any())
    var relatedCollection = nList.Where(n => (Math.Sqrt(Math.Pow((n.x - nList.First().x), 2) + Math.Pow((n.y - nList.First().y), 2) + Math.Pow((n.z - nList.First().z), 2)) <= gap));

    List<Node> relatedCollectionList = relatedCollection.ToList();
    List<Node> relatedCollectionListForDictionary = relatedCollection.ToList();

    relatedCollectionsDictionary.Add(index++, relatedCollectionListForDictionary);

    while (relatedCollectionList.Any())

The idea is that you do a lot of processing and iterate through all nodes every time. But using this scenario you don't iterate through any item more than one time.

Related Articles