Algorithm
List boruvka(int V, List edges) {
UnionFind uf = new UnionFind(V);
List mst = new ArrayList<>();
int components = V;
while (components > 1) {
Edge[] cheapest = new Edge[V];
for (Edge e : edges) {
int cu = uf.find(e.u), cv = uf.find(e.v);
if (cu == cv) continue;
if (cheapest[cu] == null || e.w < cheapest[cu].w) cheapest[cu] = e;
if (cheapest[cv] == null || e.w < cheapest[cv].w) cheapest[cv] = e;
}
for (Edge e : cheapest)
if (e != null && uf.union(e.u, e.v)) {
mst.add(e); components--;
}
}
return mst;
} Advertisement
Why it works
Minimum edge from any component is in some MST (cut property). Adding all such doesn't create cycles among MST edges chosen.
Advertisement
Complexity
O(E log V) — number of rounds is O(log V), each round O(E).
Parallel-friendly
Each component's decision is independent. Great for distributed/parallel MST.