Function kruskals

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

Kruskals Algorithm - Generate MST for any graph using the Kruskal’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::kruskals(g); // get the mst using kruskals algorithm

// kruskals 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
}