Implement Kruskal's algorithm