Python implementation of Kruskal's algorithm