R icon indicating copy to clipboard operation
R copied to clipboard

Add Boruvka's MST in R

Open nachiket2005 opened this issue 2 months ago • 1 comments

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 vertices
  • edges: data frame with columns u, v, w for 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

nachiket2005 avatar Oct 18 '25 12:10 nachiket2005