Function graphalgos::algos::boruvka
source · pub fn boruvka(g: Graph) -> Result<Graph, String>
Expand description
Boruvka’s Algorithm - Generate MST for any graph using the Boruvka’s Algorithm
Parameters:
- g - the graph that needs to be converted to MST. This will be of type
Graph
Return Value:
This function returns a result, which will be either a Graph - the MST that was generated using the algo or a Error<String>
in case of any error.
The common errors would be - if graph is directed or if MST cannot be generated for the given graph
Example Usage:
let mut G: graphs::Graph = graphs::Graph::new(false); // creates an undirected graph
// Add vertices
g.add_vertex(String::from("A")); // add vertex A
g.add_vertex(String::from("B")); // add vertex B
...
...
g.add_vertex(String::from("I")); // add vertex I
// Add edges
// Add multiple edges
g.add_edge(
(String::from("A"), String::from('B')),
GNumber::I32(4),
);
...
...
let mst = algos::boruvka(G); // get the mst using kruskals algorithm
// boruvka returns results, so use match statement to process it
match mst_kruskals {
Ok(g) => g.print(), // print the MST if generated successfully
Err(e) => println!("{}", e), // print the error if any
}