Repository navigation
perf: components by a union-find over the edges - #63
Merged
Merged
Conversation
The labelling walked the neighbours of every vertex, which is a call to Edge.next for every incident edge, and for a directed graph a second list of predecessors. Looking at the two ends of each edge once, with a union-find, needs neither, ignores direction by construction, and gives the same labels: the root of a component is its first vertex, so numbering the roots in the order of the vertices keeps the numbering. The labels are written directly to the vertices, not through the coordinate hook. 10,000 vertices, 30,000 edges: 8.7 -> about 4 ms; 100,000 vertices, 200,000 edges: 136 -> about 40 ms. The test of recomputation poisons the labels and sees whether they survive, since it no longer calls adjacent(); new tests of the numbering and of agreement with a traversal written out in the test. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 30 |
| Duplication | 2 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #63 +/- ##
==========================================
+ Coverage 97.24% 97.26% +0.01%
==========================================
Files 3 3
Lines 1560 1570 +10
==========================================
+ Hits 1517 1527 +10
Misses 43 43 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
The labelling of components (
nc,component,samecomponent,Vertex.label) was O(V + E) but slow in practice: profiling at 10,000 vertices put 55% of the time inEdge.next, called once per incident edge fromadjacent()(and again frompredecessors()for a directed graph), each reading two properties.It is now one pass over the edges with a union-find (path halving), reading the two ends of each edge directly:
_labelis written directly, not throughVertex.__setattr__.The "not recomputed when unchanged" test spied on
Vertex.adjacent, which this does not call, so it now poisons the labels and checks that they survive until the graph changes. New tests: numbering follows the first vertex whatever the order of the edges, loops and parallel edges, removals and additions are changes, and agreement with an independent traversal on 30 random graphs of both kinds.755 tests pass, mypy clean.
🤖 Generated with Claude Code