perf(prim): index the edges by endpoint instead of rescanning them - #804
Merged
Merged
Conversation
Every time a node joins the tree, the loop walks the whole edge list to find the edges that touch it, so growing an MST costs one pass over the edges per node: O(V*E) before the queue does any work at all. Almost all of that scanning looks at edges that touch neither endpoint. Index the edges by endpoint once, up front, and offer only the edges leaving the node just added. The same candidates reach the queue in the same order, so the tree and the order of its edges are unchanged, including the tie-breaking the existing tests pin down. `visited` also moves to the crate's hasher. Checked against `kruskal` over 200 random connected graphs. About 86% off a 1500-node, 9500-edge graph. The saving grows with the edge count, since the work per node added no longer depends on it.
samueltardieu
force-pushed
the
perf/prim-adjacency
branch
from
September 9, 2026 07:40
7efcfda to
a7c08ad
Compare
samueltardieu
enabled auto-merge
September 9, 2026 07:40
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #802.
Indexes the edges by endpoint once, up front, and offers only the edges leaving the node just added, instead of walking the whole edge list every time a node joins the tree.
The same candidates reach the queue in the same order, so the tree and the order of its edges are unchanged, including the tie-breaking the existing tests pin down.
visitedalso moves to the crate's hasher.Checking
Verified against
kruskalover 200 random connected graphs. The three existing tests assert exact output and are unaffected.About 86% off a 1500-node, 9500-edge graph, measured against this branch's parent. The saving grows with the edge count, since the work per node added no longer depends on it.
Built on #797, which is included here as the first commit because the indexing change touches the same seeding code. Review that one first; once it lands I will rebase this so only the second commit remains.
All timings measured on Windows 11, Intel Core Ultra 7 265K, 64 GB RAM.