Skip to content

perf(prim): index the edges by endpoint instead of rescanning them - #804

Merged
samueltardieu merged 1 commit into
evenfurther:mainfrom
tachsin:perf/prim-adjacency
Sep 9, 2026
Merged

perf(prim): index the edges by endpoint instead of rescanning them#804
samueltardieu merged 1 commit into
evenfurther:mainfrom
tachsin:perf/prim-adjacency

Conversation

@tachsin

@tachsin tachsin commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

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. visited also moves to the crate's hasher.

Checking

Verified against kruskal over 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.

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
samueltardieu added this pull request to the merge queue Sep 9, 2026
Merged via the queue into evenfurther:main with commit 7b69e37 Sep 9, 2026
12 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.

prim rescans the whole edge list for every node added, making it O(V*E)

2 participants