Kruskal’s Algorithm is an algorithm that computes the weight of the minimum spanning tree of a connected undirected graph with edge weights in time and space.

Hint

This problem can also be solved by Prim’s Algorithm in

  • time and space, or
  • time and space.

Hint

This problem can also be solved by Boruvka’s Algorithm in time and space.

Algorithm

Lemma

Let denote the set of minimum spanning trees of , then

Repeatedly select an edge in and update to until . Applying the lemma yields that the selected edges form a minimum spanning tree.

Applying Introsort to sort the edges and using a Disjoint Set Union to maintain the structure of the graph yield an algorithm that solves the problem in time and space.

std::ranges::sort(e);
 
DSU dsu(n);
int sum = 0;
 
for (auto [w, u, v] : e) {
	if (dsu.merge(u, v)) {
		sum += w;
	}
}
 
return sum;