You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Prim's Algorithm for Minimum Spanning Tree (petgraph#625)
Prim's algorithm: https://en.wikipedia.org/wiki/Prim%27s_algorithm
This PR adds another algorithm to find the Minimum Spanning Tree of a
graph, an alternative to Kruskal's.
1. Prim's algorithm has some limitations, such as the input graph must
be undirected and it should not have disconnected components.
I'm wondering if this can be enforced with traits, if anyone can help on
this.
2. I would like to suggest adding another algorithm as well,
`min_spanning_forest_prim`, that iterates through this algorithm to find
min spanning tree for all input graph components.
`min_spanning_forest_kruskal` would be trivial to implement, since
current `min_spanning_tree` function, using Kruskal's algorithm, already
finds a min spanning forest. Any thoughts on this?
This PR also aims to fix current `min_spanning_tree` benches, since they
currently do not iterate through the created `MinSpanningTree`
structure, not giving the actual bench for Kruskal algorithm.
3. I added a simple function to iterate through the generated tree
elements, but I'm not very familiar with Rust ecosystem, so I would
appreciate if anyone knows a better way to force the iteration:
```Rust
// Current Bench:
bench.iter(|| (min_spanning_tree(&a), min_spanning_tree(&b)));
// Suggested Bench:
bench.iter(|| (iterate_mst_kruskal(&a), iterate_mst_kruskal(&b)));
// Force Tree Iteration:
fn iterate_mst_kruskal<G>(g: G) -> bool
where
G: Data + IntoEdges + IntoNodeReferences + IntoEdgeReferences + NodeIndexable,
G::NodeWeight: Clone,
G::EdgeWeight: Clone + PartialOrd,
{
let mst = min_spanning_tree(g);
mst.into_iter().all(|_| true)
}
```
---------
Co-authored-by: Sambinelli <[email protected]>
0 commit comments