Function prims

Source
pub fn prims(g: Graph) -> Result<Graph, String>
Expand description

Prim’s Algorithm - Generate MST for any graph using the Prim’s Algorithm

§Parameters:

  1. 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:

use graphalgos::algos;
use graphalgos::graphs;
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')),
    graphs::GNumber::I32(4),
);
let mst = algos::boruvka(g); // get the mst using prims algorithm
// prims returns results, so use match statement to process it
match mst {
    Ok(g) => g.print(),          // print the MST if generated successfully
    Err(e) => println!("{}", e), // print the error if any
}