implemented the Kruskal's algorithm