Fix sparse heuristic not applied to diagonal blocks in stochastic_block_model#8557
Open
dean985 wants to merge 1 commit intonetworkx:mainfrom
Open
Fix sparse heuristic not applied to diagonal blocks in stochastic_block_model#8557dean985 wants to merge 1 commit intonetworkx:mainfrom
dean985 wants to merge 1 commit intonetworkx:mainfrom
Conversation
…ck_model The diagonal block (i==j) case consumed the edges iterator before the sparse/dense branching logic, so the sparse heuristic was never used for diagonal blocks. Remove the premature edge addition so diagonal blocks flow into the same sparse/dense logic as off-diagonal blocks.
855f99d to
0b48a4b
Compare
rossbar
reviewed
Mar 9, 2026
Contributor
There was a problem hiding this comment.
Presumably the change in results is a result of the random state changing due to the removal of special handling of the i==j case. I think this is fine, though it's worth noting that this change will have an impact even outside of the sparse codepath as a result. Again this is probably fine, but just pointing it out so folks more familiar with the stochastic block model can take a look and double check!
rossbar
reviewed
Mar 9, 2026
Contributor
There was a problem hiding this comment.
Also - it'd be nice to have a test that illustrates the original issue in the sparse branch (presumably that the sparsity algorithm wasn't being applied to the diagonal blocks) and illustrates the fix.
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.
fixes #8536
Summary
i==j) case instochastic_block_modelconsumed theedgesiterator before the sparse/dense branching logic, so the sparse heuristic was never used for diagonal blocks.Test plan
test_community.pypass (22/22)test_stochastic_block_modelexpected edge count to match the corrected sparse heuristic behavior