Skip to content

Merge the smaller component into the larger one when an edge joins two components - #502

Merged
Paebbels merged 1 commit into
devfrom
claude/graph-component-merge
Oct 4, 2026
Merged

Paebbels merged 1 commit into
devfrom
claude/graph-component-merge

Conversation

@pytooling-claude

Copy link
Copy Markdown

Speed-up from the Cython probe: profiling the Graph benchmark showed that 160 of 170 ms went into merging components, not into anything Cython could compile away. This PR fixes it in plain Python.

Changes

  • pyTooling.Graph:

    • An edge joining two components moves the vertices of the smaller component into the larger one (union by size). It moved the destination's vertices into the source's component whatever their sizes, so an edge from a new vertex into a big component moved the whole component - quadratic in the graph's size, e.g. 4.5 million set.add calls for 6000 edges.
    • The component kept is therefore the larger one, not always the source's; of two of equal size, still the source's. The other one is dropped with its name and key-value pairs, as before.

    Best of 5, Python 3.14, Linux, fresh process per side:

    Operation dev this PR
    Graph of 1000 vertices from graph_n1000_m1500 edge list 9.7 ms 4.3 ms
    Graph of 10000 vertices from graph_n10000_m15000 edge list 632.6 ms 54.5 ms
    10000 × root.EdgeFromNewVertex(i) (flat) 1829.9 ms 39.1 ms
    10000 × vertex.EdgeFromNewVertex(i) (linear chain) 1838.3 ms 41.5 ms
    10000 × root.EdgeToNewVertex(i) (merge was already cheap) 37.8 ms 36.1 ms

Documentation

  • doc/News.rst: the change, with the numbers.

Unit Tests

  • New class Components: test_Merge_SmallIntoLarge, test_Merge_LargeIntoSmall, test_Merge_EqualSize - which component survives, that the graph has one component and that it holds every vertex.
  • tests/performance/Graph/Graph.py: EdgeLinking.test_LinkFromNewVertex_Flat and test_LinkFromNewVertex_Linear, the direction that was quadratic (the existing two tests link to a new vertex).
  • 2398 passed.

…o components

An edge moved every vertex of the destination's component into the source's component, whatever their sizes. An
edge from a new vertex into a big component therefore moved the whole component, which made building a graph
quadratic in its size. The smaller component moves now (union by size): a graph of 10000 vertices from an edge list
takes 55 ms instead of 633 ms, and 10000 calls of EdgeFromNewVertex 39 ms instead of 1830 ms.

Co-Authored-By: Patrick Lehmann <Paebbels@gmail.com>
@pytooling-claude
pytooling-claude Bot requested a review from Paebbels as a code owner October 4, 2026 21:36
@pytooling-claude pytooling-claude Bot added Documentation Improvements or additions to documentation Enhancement New feature or request Graph pyTooling.Graph - Graph data structure. Unit Tests labels Oct 4, 2026
@pytooling-claude pytooling-claude Bot mentioned this pull request Oct 4, 2026
@codacy-production

codacy-production Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 29 complexity · 0 duplication

Metric Results
Complexity 29
Duplication 0

View in Codacy

🟢 Coverage 100.00% diff coverage · +0.01% coverage variation

Metric Results
Coverage variation ✅ +0.01% coverage variation (-1.00%)
Diff coverage ✅ 100.00% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (fc7b456) 14637 13129 89.70%
Head commit (62ba13f) 14639 (+2) 13132 (+3) 89.71% (+0.01%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#502) 2 2 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

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

codecov Bot commented Oct 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.97%. Comparing base (fc7b456) to head (62ba13f).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #502   +/-   ##
=======================================
  Coverage   86.96%   86.97%           
=======================================
  Files          59       59           
  Lines       14637    14639    +2     
  Branches     2267     2268    +1     
=======================================
+ Hits        12729    12732    +3     
+ Misses       1508     1507    -1     
  Partials      400      400           
Flag Coverage Δ
unittests 86.97% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@Paebbels
Paebbels merged commit 59f4bad into dev Oct 4, 2026
57 checks passed
@Paebbels
Paebbels deleted the claude/graph-component-merge branch October 4, 2026 22:51
This was referenced Oct 12, 2026
This was referenced Oct 20, 2026
@pytooling-claude pytooling-claude Bot added this to the v10.0 milestone Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Enhancement New feature or request Graph pyTooling.Graph - Graph data structure. Unit Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants