This is the Kruskal algorithm with Union by rank, path compression and a break condition