Skip to content

Conversation

@BryanCruz
Copy link
Contributor

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.

  1. 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:
// 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)
}

@ABorgna
Copy link
Member

ABorgna commented Apr 27, 2024

Thanks for the PR!

  1. Kruskal's doc states
    The input graph is treated as if undirected.
    perhaps we could do the same here
  2. That could be useful.
    It is a bit inconsistent that Kruscal always returns a forest, but leaving it as undefined behaviour if there is more than one component sounds OK.

Comment on lines 90 to 107
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)
Copy link
Member

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.

Suggested change
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);
}

@BryanCruz
Copy link
Contributor Author

  1. Kruskal's doc states
    The input graph is treated as if undirected.
    perhaps we could do the same here

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 IntoEdgeReferences, to access edges that goes to a certain node

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?

@daehiff
Copy link
Contributor

daehiff commented Oct 10, 2024

Hey, since we can use this feature in a different project:
Is there any due date to this feature or any open points that need to addressed, if yes, do you need help with that?

@BryanCruz
Copy link
Contributor Author

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.

@BryanCruz
Copy link
Contributor Author

Limited indexmap version in project config since its latest release broke petgraph MSRV

@BryanCruz BryanCruz requested a review from ABorgna November 13, 2024 03:04
Copy link
Member

@ABorgna ABorgna left a 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.

@ABorgna ABorgna merged commit 278c53b into petgraph:master Feb 5, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants