Skip to content

fix(prim): export from the prelude, and document MST behaviour on disconnected graphs - #836

Open
tachsin wants to merge 1 commit into
evenfurther:mainfrom
tachsin:fix/prim-prelude
Open

tachsin wants to merge 1 commit into
evenfurther:mainfrom
tachsin:fix/prim-prelude

Conversation

@tachsin

@tachsin tachsin commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Two small things about prim, found while looking through the library.

prim is missing from the prelude

Every other algorithm is re-exported there; prim is not. So this does not compile:

use pathfinding::prelude::*;
let mst = prim(&edges);       // error[E0425]: cannot find function `prim` in this scope

and callers have to write pathfinding::undirected::prim::prim instead, while kruskal, sitting right next to it in src/undirected/, works from the prelude. I ran into this writing a throwaway test, which is how it turned up. One line in src/lib.rs.

The two MST functions disagree on disconnected graphs, silently

let edges = vec![(1, 2, 1), (3, 4, 1)];   // two components

prim(&edges)               // [(1, 2, 1)]                -- one component
kruskal(&edges).count()    // 2                          -- both components

prim grows outwards from the first endpoint of the first edge, so it spans only that component. kruskal sorts all edges and unions, so it spans every component and returns a forest.

Both behaviours are reasonable, and neither is a bug. The problem is that both doc comments just say "minimum-spanning-tree", which does not distinguish them, so a caller who swaps one for the other on a disconnected graph gets a quietly different answer. This documents the difference on both sides, with a runnable example on prim.

No behaviour change; docs and one re-export.

…s do when the graph is disconnected

prim was the only algorithm not re-exported from the prelude, so
`use pathfinding::prelude::*` reached kruskal but not prim, and callers
had to name pathfinding::undirected::prim::prim in full.

The two also disagree on disconnected input without saying so. prim grows
outwards from one node and spans only that component; kruskal spans every
component and returns a forest. Both simply said "minimum-spanning-tree",
which is true of neither in that case without qualification. Document the
difference on both, with a runnable example.
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.

1 participant