Skip to content

Prune nested components in k_components reconstruction and add tests the private helpers - #8753

Merged
dschult merged 5 commits into
networkx:mainfrom
jtorrents:kcomponents-reconstruct-prune
Jul 24, 2026
Merged

dschult merged 5 commits into
networkx:mainfrom
jtorrents:kcomponents-reconstruct-prune

Conversation

@jtorrents

Copy link
Copy Markdown
Member

Follow-up to #8734 and its review discussion with @dschult and @rossbar. Two main things in this PR: a hardening fix in _reconstruct_k_components found 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 for test_kcomponents.py.

Reconstruction could emit a nested, non-maximal component

A single _consolidate pass 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, candidates A = {1..8} and B = {5..12} share four nodes, so they consolidate into A | B. The level-5 component c = {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 of A | B in 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:
    • propagates a (k+1)-level component that spans several k-level candidates without being a subset of any of them (the Fix k_components losing or truncating k-components #8734 fix; the pre-fix union-coverage check fails this test),
    • drops a propagated component that ends up nested inside a consolidated merge (test_reconstruct_k_components_drops_nested_candidate, the fix in this PR: fails on current main, passes here),
    • never returns a level containing a component that is a strict subset of another at the same level.
  • _generate_partition, checked over the biconnected components of the karate club, Davis Southern Women, and two seeded G(n, p) graphs:
    • never loses a node (the Fix k_components losing or truncating k-components #8734 bug 1 failure mode: every cutset node has a neighbor outside the cutset, or the cutset would not be minimum),
    • every part is a strict subset of the input part (the recursion's termination argument),
    • every part induces a connected subgraph.

Slow markers removed

The three pytest.mark.slow markers in this test file predate the all_node_cuts fix (#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 the k_components suite runs complete by default.

Doctest

The k_components docstring example claimed the Petersen graph result in a comment; it is now a real doctest asserting it.

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.
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 rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @jtorrents !

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@dschult
dschult merged commit e1193f6 into networkx:main Jul 24, 2026
45 checks passed
@dschult dschult added this to the 3.7 milestone Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants