Boruvka’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 Kruskal’s Algorithm in time and space.

Hint

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

  • time and space, or
  • time and space.

Algorithm

Lemma

Let denote the set of minimum spanning trees of , denote the set of edges incident with . Then

For each in , choose an edge in . Then, for each in , if is not a self-loop, select , update to , and update accordingly. Repeat this process until .

Let be the other vertex incident with , then it is easy to prove that

Therefore, applying the lemma yields that the selected edges form a minimum spanning tree.

Using a Disjoint Set Union to maintain the structure of the graph yields an algorithm that solves the problem in time and space.

DSU dsu(n);
int sum = 0;
 
while (dsu.size(0) < n) {
	std::vector e(n, -1);
	for (int i = 0; i < m; i++) {
		if (dsu.same(u[i], v[i])) {
			continue;
		}
 
		for (int j : {dsu.find(u[i]), dsu.find(v[i])}) {
			if (e[j] == -1 || w[e[j]] > w[i]) {
				e[j] = i;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (dsu.find(i) == i && dsu.merge(u[e[i]], v[e[i]])) {
			sum += w[e[i]];
		}
	}
}
 
return sum;