Implemented the Kruskal's algorithm.