Optimized minimal spanning tree finder.