-
Notifications
You must be signed in to change notification settings - Fork 320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Prim's Algorithm for Minimum Spanning Tree #625
base: master
Are you sure you want to change the base?
Conversation
Thanks for the PR!
|
benches/min_spanning_tree.rs
Outdated
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) | ||
} | ||
|
||
fn iterate_mst_prim<G>(g: G) -> bool | ||
where | ||
G: Data + IntoEdges + IntoNodeReferences + IntoEdgeReferences + NodeIndexable, | ||
G::NodeWeight: Clone, | ||
G::EdgeWeight: Clone + PartialOrd, | ||
{ | ||
let mst = min_spanning_tree_prim(g); | ||
mst.into_iter().all(|_| true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Using black_box
is the best bet to avoid it getting optimised away.
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) | |
} | |
fn iterate_mst_prim<G>(g: G) -> bool | |
where | |
G: Data + IntoEdges + IntoNodeReferences + IntoEdgeReferences + NodeIndexable, | |
G::NodeWeight: Clone, | |
G::EdgeWeight: Clone + PartialOrd, | |
{ | |
let mst = min_spanning_tree_prim(g); | |
mst.into_iter().all(|_| true) | |
fn iterate_mst_kruskal<G>(g: G) | |
where | |
G: Data + IntoEdges + IntoNodeReferences + IntoEdgeReferences + NodeIndexable, | |
G::NodeWeight: Clone, | |
G::EdgeWeight: Clone + PartialOrd, | |
{ | |
for e in min_spanning_tree(g) { | |
std::hint::black_box(e); | |
} | |
} | |
fn iterate_mst_prim<G>(g: G) | |
where | |
G: Data + IntoEdges + IntoNodeReferences + IntoEdgeReferences + NodeIndexable, | |
G::NodeWeight: Clone, | |
G::EdgeWeight: Clone + PartialOrd, | |
{ | |
for e in min_spanning_tree_prim(g) { | |
std::hint::black_box(e); | |
} |
Thanks for the review @ABorgna :) I updated the docs to let more explicit what will happen if input graph is directed or has more than 1 component, and applied the suggested change for benches. I think we would need to convert a directed graph to an undirected one in order to Prim behave correctly on it. For example, a graph where edges are: (I) A-5->B , (II) B-10->C and (III) C-15->A, the valid MST computed by Prim would be {I and II} (undirected), but in order to actually implement this behavior we would need some sort of inverse of So I don't think it's worth the complexity, since there are other algorithms specifically for directed graphs that could be implemented. What do you think? |
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.
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.
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 currentmin_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 createdMinSpanningTree
structure, not giving the actual bench for Kruskal algorithm.