Repository navigation
Fix all_node_cuts: correct Even-Tarjan reduction for Kanevsky's algorithm - #8586
Conversation
|
I decided against changing Other uses of |
Benchmark:
|
| Graph family | Crossover (NX ~ igraph) | Determining factor |
|---|---|---|
| Grid NxN | ~300 nodes (18x18) | Few cuts (4), flow-dominated |
| Watts-Strogatz | ~160 nodes | Few cuts, sparse |
| Barabasi-Albert m=2 | ~200 nodes | Moderate cuts, sparse |
| Barabasi-Albert m=3 | ~200+ nodes | Approaching at n=160 |
| 3-regular | >200 nodes | High cut count slows convergence |
| Circular ladder | ~400+ nodes | Cuts grow linearly with n |
| Cycles | No crossover | Cuts grow quadratically (~n^2/4) |
The pattern: NX overtakes igraph when the number of minimum cuts is small relative to graph size. For sparse, real-world-like graphs (grids, scale-free, small-world) with few cuts, NX wins at 150-300 nodes. When cuts grow with n (circular ladders) the crossover moves higher. When cuts grow quadratically (cycles), igraph's C based implementation keeps the advantage.
All 61 graphs tested: 100% correctness match with igraph.
|
Hello @jtorrents !!!! Welcome Back! Your explanations make good sense -- and the outcome looks promising. Thanks!! |
|
Hi @dschult !! Very nice to talk to you after such a long time. All is well! Thanks. Glad to to be back and be able to contribute a bit again, at least help squashing my own bugs ;) NetworkX is better than ever. You and the rest of NetworkX maintainers are doing a terrific job! |
…ithm. The Even-Tarjan auxiliary digraph built by build_auxiliary_node_connectivity sets capacity=1 on all edges. This is correct for computing the node connectivity value, but Kanevsky's algorithm depends on the structure of the residual graph, which requires infinite capacity on external edges as the original Even-Tarjan reduction specifies. With unit-capacity external edges, these edges get saturated by the flow, producing a residual graph with the wrong structure. The condensation DAG has too many nodes, and the antichain count explodes exponentially. This bug (introduced by me in the original NetworkX implementation) was not identified when correctness bugs were reported in networkx#3025. The fix in PR networkx#3039 addressed the symptoms (missing cuts) by adding compensating filters: VE1 incident-node filtering, transitive closure on the full residual graph, and internal-edge validation; and also introduced two additional bugs: - Removing backward arcs from the residual graph (capacity=0 edges with negative flow that represent cancellable flow and must be preserved) - Building closed sets using predecessors instead of successors in the condensation DAG, producing the wrong side of the cut partition These filters discarded most of the millions of spurious antichains generated by the broken condensation DAG, making results mostly correct but at enormous computational cost. This fix addresses the root cause and restores the algorithm to match the paper's specification: - Set infinite capacity on external edges before building the residual - Remove only edges with zero residual capacity (capacity == flow) - Build successor-closed sets from antichins of the condensation DAG - Find cut edges from S to ~S using H successors. The compensating code is no longer needed and has been removed. Performance on grid_2d_graph(11,11) improves from 180s to 0.16s (n=121) (1000x+), and now scales polynomially as predicted by the paper. Results verified identical to igraph's C implementation at all tested sizes. Fixes networkx#7994. Tests previously marked @pytest.mark.slow are now fast enough to run by default, and MAX_CUTSETS_TO_TEST is restored from 4 to 100.
9ba9555 to
7c46600
Compare
|
I squashed the two commits into one and force-pushed to my branch because I inadvertently used my work email in the author field for the two original commits. |
There was a problem hiding this comment.
This is excellent @jtorrents , thanks so much for putting in all the effort to clearly and concretely summarize the underlying problem (a very interesting read) and how it is addressed directly by these changes.
With unit-capacity external edges, these edges get saturated by the flow, producing a residual graph with the wrong structure. The condensation DAG has too many nodes, and the antichain count explodes exponentially.
It's not often that issues can be cleanly traced back to so concise an algorithmic detail, which then can be fixed up so neatly. This is a very satisfying outcome! The subsequent analysis (and removal) of the ad-hoc patches that were added to try to address the symptoms of the underlying issue is similarly very interesting - thanks for including in the writeup!
Tests previously marked @pytest.mark.slow are now fast enough to run by default, and MAX_CUTSETS_TO_TEST is restored from 4 to 100.
IMO this is more than sufficient to test the fact that the scaling issue has been fixed, even though it's only indirect.
As a AI disclaimer: I used Claude to explore the problem, build many scripts that printed step by step the state of the data structures used, create benchmarks that compare time and correctness between the old implementation, these fixes, and iGraph. In summary, Claude has been instrumental for fixing this long standing bug (introduced by yours truly nine years ago).
I just wanted to take the opportunity to highlight that this PR is perfectly in-line with NetworkX's ai policy and is an obvious example of a positive outcome. IMO as a reviewer, the thing that makes this PR great is not the code that was written but how well the underlying issue and solution were communicated. The work done in this PR to highlight not only the root-cause of the issue, but also the development history surrounding attempts at patching the symptoms of the original bug, really helps drive home that the problem has been wholistically understood, digested, and addressed in an effective way. It provides excellent context that really helps in reviewing and building confidence that this is a (if not the) correct way of addressing the issue.
I can't claim to be an expert in Kanevsky's algorithm, but the code changes are consistent with the description of the underlying issue and the outcomes (both in the test suite and in the PR description) are quite compelling that this does indeed fix the reported algorithm scaling issues. Thanks again @jtorrents for putting so much thought/effort into communication in this great PR!
|
Thanks for the kind words @rossbar ! It's been a tough bug to squash, and getting to the bottom of it was not easy. The key was looking at the internal state of data structures at each step of the algorithm. |
dschult
left a comment
There was a problem hiding this comment.
Thanks very much for this @jtorrents !!
The speed up makes these methods usable. And the correctness is of course the fundamental goal. So tracing through the patches made for correctness to find the underlying issue is really impressive and appreciated.
We should make a splash about this improvement in the release notes, too when the time comes for that.. :)
…s the private helpers (#8753) * Drop nested components in reconstruction, add doctest 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 #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. * Add unit tests for private k-components helpers Follow-up to the #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 #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. * Remove slow markers from k_components tests The three pytest.mark.slow markers predate the all_node_cuts fix (#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. * Use the Davis Southern Women graph in the partition test 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-gh-8734 code silently dropped the three women that attended only cutset events. Also drop a redundant comment block above the private-helper unit tests. * Restore the slow markers on the k_components 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.
The Even-Tarjan auxiliary digraph built by build_auxiliary_node_connectivity sets capacity=1 on all edges. This is correct for computing the node connectivity value, but Kanevsky's algorithm depends on the structure of the residual graph, which requires infinite capacity on external edges as the original Even-Tarjan reduction specifies.
With unit-capacity external edges, these edges get saturated by the flow, producing a residual graph with the wrong structure. The condensation DAG has too many nodes, and the antichain count explodes exponentially.
This bug (introduced by me in the original NetworkX implementation) was not identified when correctness bugs were reported in #3025. The fix in PR #3039 addressed the symptoms (missing cuts) by adding compensating filters: VE1 incident-node filtering, transitive closure on the full residual graph, and internal-edge validation; and also introduced two additional bugs:
These filters discarded most of the millions of spurious antichains generated by the broken condensation DAG, making results mostly correct but at enormous computational cost.
This fix addresses the root cause and restores the algorithm to match the paper's specification:
The compensating code is no longer needed and has been removed.
Performance on grid_2d_graph(11,11) improves from 180s to 0.16s (n=121) (1000x+), and now scales polynomially as predicted by the paper. Results verified identical to igraph's C implementation.
Fixes #7994.
Tests previously marked @pytest.mark.slow are now fast enough to run by default, and MAX_CUTSETS_TO_TEST is restored from 4 to 100.
As a AI disclaimer: I used Claude to explore the problem, build many scripts that printed step by step the state of the data structures used, create benchmarks that compare time and correctness between the old implementation, these fixes, and iGraph. In summary, Claude has been instrumental for fixing this long standing bug (introduced by yours truly nine years ago).
For instance, a really nice benchmark that shows that this implementation is now two times faster than iGraph for a grid 25x25
Output: