Implemented Kruskal's algorithm.