Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub mod prelude {
pub use crate::undirected::cliques::*;
pub use crate::undirected::connected_components::*;
pub use crate::undirected::kruskal::*;
pub use crate::undirected::prim::*;
pub use crate::utils::*;
}

Expand Down
5 changes: 5 additions & 0 deletions src/undirected/kruskal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ where
/// Find a minimum-spanning-tree. From a collection of
/// weighted edges, return an iterator of edges forming
/// a minimum-spanning-tree.
///
/// # Disconnected graphs
///
/// Every component is spanned, so the result is a spanning forest rather than a single tree.
/// [`prim`](super::prim::prim) differs here, and spans only the component it starts from.
pub fn kruskal<N, C>(edges: &[(N, N, C)]) -> impl Iterator<Item = (&N, &N, C)>
where
N: Hash + Eq,
Expand Down
19 changes: 19 additions & 0 deletions src/undirected/prim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ use std::hash::Hash;
///
/// Edges are undirected: `(a, b, c)` and `(b, a, c)` describe the same edge, and either form
/// may be used. The tree is grown from the first endpoint of the first edge.
///
/// # Disconnected graphs
///
/// The tree is grown outwards from one node, so only the component containing that node is
/// spanned; edges in any other component are not returned. [`kruskal`](super::kruskal::kruskal)
/// differs here, and returns a spanning forest covering every component.
///
/// ```
/// use pathfinding::prelude::{kruskal, prim};
///
/// // Two components: 1-2 and 3-4.
/// let edges = vec![(1, 2, 1), (3, 4, 1)];
///
/// // prim spans the component holding node 1, the first endpoint of the first edge.
/// assert_eq!(prim(&edges), vec![(&1, &2, 1)]);
///
/// // kruskal spans both.
/// assert_eq!(kruskal(&edges).count(), 2);
/// ```
pub fn prim<N, C>(edges: &[(N, N, C)]) -> Vec<(&N, &N, C)>
where
N: Hash + Eq + Ord,
Expand Down
Loading