Repository navigation
Prune nested components in k_components reconstruction and add tests the private helpers - #8753
Merged
Merged
Conversation
A single _consolidate pass merges on pairwise overlaps of its input sets, so a level can still end up containing a component that is a strict subset of a merged one: a propagated (k+1)-level component sharing fewer than k nodes with each of two level-k candidates is not merged with their union. Drop such nested sets at each level; every emitted set is k-connected, so a nested one is never maximal. This completes the more permissive propagation introduced by networkx#8734 (which is preserved verbatim) and turns the never-observed redundant-subset scenario into a structural impossibility. No known construction reaches it; validated with the full suite plus differential and brute-force fuzzing. Also turn the k_components docstring example into a real doctest.
Follow-up to the networkx#8734 review discussion: both bugs fixed there lived in private helpers whose contracts were only implicit, and neither was reachable by end-to-end tests on the small classic graphs alone. State the contracts explicitly and test the helpers directly: - _reconstruct_k_components propagates a (k+1)-level component that spans several k-level candidates (the networkx#8734 fix), drops a component nested inside a consolidated merge (the fix in this branch; the test synthesizes the input that graph-level fuzzing never produced and fails without the fix), and never emits a component that is a strict subset of another at the same level. - _generate_partition never loses a node, yields only strict subsets of the input, and every part induces a connected subgraph.
The three pytest.mark.slow markers predate the all_node_cuts fix (networkx#8586), which is what made these tests slow. The whole file now runs in under 2 seconds with --runslow and the slowest single test (test_random_gnp[50-0.2]) takes under a second, so run the complete k_components suite by default.
amcandio
reviewed
Jul 11, 2026
rossbar
reviewed
Jul 12, 2026
Replace the parametrized contract test for _generate_partition with a single explicit example, as suggested in review. The Davis Southern Women graph is biconnected, so no bicomponent loop or conditionals are needed, and every expected value is stated explicitly: the node connectivity, the two minimum cutsets, and the exact four parts. The pre-networkxgh-8734 code silently dropped the three women that attended only cutset events. Also drop a redundant comment block above the private-helper unit tests.
This reverts commit ae5d6af, as agreed in review: keep the slow marks for now, and revisit removing them in a separate PR once the k_components work has been merged, so that the effect on CI test balance can be evaluated.
rossbar
approved these changes
Jul 22, 2026
rossbar
left a comment
Contributor
There was a problem hiding this comment.
LGTM, thanks @jtorrents !
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.
Follow-up to #8734 and its review discussion with @dschult and @rossbar. Two main things in this PR: a hardening fix in
_reconstruct_k_componentsfound while writing the helper-level tests, and the unit tests for the private helpers themselves. ALso a bit of house keeping: a proper doctest for k_components and removing slowest markers fortest_kcomponents.py.Reconstruction could emit a nested, non-maximal component
A single
_consolidatepass merges sets on pairwise overlaps of at least k nodes, so its output can still contain a set nested inside a merged one. Concretely: a component propagated down from level k+1 that shares fewer than k nodes with each of two level-k candidates is not merged with either of them, yet when those two candidates merge with each other it ends up a strict subset of their union. A nested set is never a maximal k-connected subgraph, so it should not appear in the decomposition.Minimal input that reproduces the bug at
test_reconstruct_k_components_drops_nested_candidate, this test fails on current main and passes on this branch: at level 4, candidatesA = {1..8}andB = {5..12}share four nodes, so they consolidate intoA | B. The level-5 componentc = {2, 3, 4, 9, 10, 11}propagates down to level 4 but shares only three nodes with each of A and B, so it merges with neither; and lands as a strict subset ofA | Bin the output.Unit tests for the private helpers
As discussed in the #8734 review: both bugs fixed there lived in private helpers (
_generate_partition,_reconstruct_k_components) whose contracts were only implicit, and neither was reachable by end-to-end tests on the small classic graphs alone. This PR adds direct unit tests that state the contracts:_reconstruct_k_components:test_reconstruct_k_components_drops_nested_candidate, the fix in this PR: fails on current main, passes here),_generate_partition, checked over the biconnected components of the karate club, Davis Southern Women, and two seeded G(n, p) graphs:Slow markers removed
The three
pytest.mark.slowmarkers in this test file predate theall_node_cutsfix (#8586), which is what made these tests slow in the first place. They are fast now (the whole file runs in under 2 seconds with--runslow, and the slowest single test (test_random_gnp[50-0.2]) takes 0.96s) so the markers are removed and thek_componentssuite runs complete by default.Doctest
The
k_componentsdocstring example claimed the Petersen graph result in a comment; it is now a real doctest asserting it.