R
R copied to clipboard
Add Boruvka's MST in R
Overview
The boruvka_mst function constructs a minimum spanning tree (MST) for an undirected weighted graph.
It repeatedly adds the cheapest edge for each component, merging connected components until all vertices are included or no more edges can be added.
Key improvements over a naive implementation:
- Union-find with path compression for efficient component tracking
- Detects disconnected graphs and stops gracefully
- Returns both MST edges and total weight
Graphs are represented as:
V: number of verticesedges: data frame with columnsu,v,wfor edges and weights
Features
- Implements Boruvka’s MST algorithm with union-find optimization
- Supports weighted undirected graphs
- Tracks component membership and avoids cycles
- Detects disconnected graphs and warns if MST cannot span all vertices
- Returns edges in MST and total weight
- Includes example usage with a small test graph
Complexity
- Time Complexity: O(E log V) on average (due to union-find optimizations)
- Space Complexity: O(V + E) for storing edges, parent arrays, and MST