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 Query