Implementation of kruskal algorithm.