Kruskal algorithm modified