minimum_spanning_tree

Function minimum_spanning_tree 

Source
pub fn minimum_spanning_tree<T, S>(
    graph: &S,
    algorithm: &str,
    return_tree: bool,
) -> SparseResult<(T, Option<CsrArray<T>>, Array1<isize>)>
where T: Float + Debug + Copy + 'static, S: SparseArray<T>,
Expand description

Compute the minimum spanning tree of a graph

§Arguments

  • graph - The graph as a sparse matrix (must be undirected and connected)
  • algorithm - MST algorithm to use
  • return_tree - Whether to return the MST as a sparse matrix

§Returns

A tuple containing:

  • Total weight of the MST
  • Optional MST as a sparse matrix (if requested)
  • Array of parent indices in the MST

§Examples

use scirs2_sparse::csgraph::minimum_spanning_tree;
use scirs2_sparse::csr_array::CsrArray;

// Create a weighted symmetric graph
let rows = vec![0, 0, 1, 1, 2, 2];
let cols = vec![1, 2, 0, 2, 0, 1];
let data = vec![2.0, 3.0, 2.0, 1.0, 3.0, 1.0];
let graph = CsrArray::from_triplets(&rows, &cols, &data, (3, 3), false).unwrap();

let (total_weight, mst, parents) = minimum_spanning_tree(&graph, "kruskal", true).unwrap();