-
Notifications
You must be signed in to change notification settings - Fork 430
Add Prim's Algorithm for Minimum Spanning Tree #625
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
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? |
feat: add graph6 encoder
* rename graph6 file to encoder * feat: impl graph6 decoder for common graph traits
Add mst prim random
|
Hey, since we can use this feature in a different project: |
otherwise will break MSVR
|
Thanks for the interest in this feature @daehiff 😄 I cleaned up this PR, it had some extra stuff from another PR I was working on. I don't think there are any open points to be addressed, just waiting for a review now. |
|
Limited |
ABorgna
left a comment
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.
Thanks!
Perhaps we should rename Kruskal to min_spanning_tree_kruskal to be more consistent, but that should be done later in a breaking release.
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_kruskalwould be trivial to implement, since currentmin_spanning_treefunction, using Kruskal's algorithm, already finds a min spanning forest. Any thoughts on this?This PR also aims to fix current
min_spanning_treebenches, since they currently do not iterate through the createdMinSpanningTreestructure, not giving the actual bench for Kruskal algorithm.