Skip to content

Fix sparse heuristic not applied to diagonal blocks in stochastic_block_model - #8557

Merged
dschult merged 2 commits into
networkx:mainfrom
dean985:fix-sbm-sparse-diagonal-blocks
Mar 21, 2026
Merged

dschult merged 2 commits into
networkx:mainfrom
dean985:fix-sbm-sparse-diagonal-blocks

Conversation

@dean985

@dean985 dean985 commented Mar 7, 2026 •

Copy link
Copy Markdown
Contributor

fixes #8536

Summary

  • The diagonal block (i==j) case in stochastic_block_model consumed the edges iterator before the sparse/dense branching logic, so the sparse heuristic was never used for diagonal blocks.
  • Removed the premature edge addition (lines 638-640) so diagonal blocks flow into the same sparse/dense logic as off-diagonal blocks.
  • Updated the expected edge count in the test to reflect the corrected behavior.

Test plan

  • All existing tests in test_community.py pass (22/22)
  • Updated test_stochastic_block_model expected edge count to match the corrected sparse heuristic behavior

…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.

@Peiffap Peiffap 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.

LGTM.

@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.

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 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.

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.

@dean985

dean985 commented Mar 13, 2026

Copy link
Copy Markdown
Contributor Author

@rossbar use a single-block SBM so the only block is diagonal (i==j). Then run it with sparse=True and sparse=False using the same seed. Since sparse and dense use the RNG differently (geometric skip vs Bernoulli per edge), the edge sets should differ if the sparse heuristic is actually being applied. Before the fix they'd be identical, which is exactly the bug.

  def test_sbm_sparse_applies_to_diagonal_blocks():
      # Single block = only a diagonal block (i==j)
      sizes = [50]
      probs = [[0.5]]

      G_sparse = nx.stochastic_block_model(sizes, probs, seed=42, sparse=True)
      G_dense = nx.stochastic_block_model(sizes, probs, seed=42, sparse=False)

      # These would be equal before the fix because diagonal blocks
      # always took the dense path. Now they differ because they use
      # different RNG consumption patterns.
      assert G_sparse.edges() != G_dense.edges()

@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.

Got it, thanks @dean985 - my mistake was assuming sparse=False was the default 🙃

Therefore the change in the test results is indeed as expected and this all looks good. Thanks for taking the time to point it out!

@dschult

dschult commented Mar 15, 2026

Copy link
Copy Markdown
Member

Can you add the test you show in the comment to the test file? I think it'd be good to continue to check that. You could reference gh-8557 in a comment so people can find this discussion if needed.

Thanks!

@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.

Thanks @dean985

@dschult
dschult merged commit b3c9b00 into networkx:main Mar 21, 2026
38 checks passed
@dschult dschult added this to the 3.7 milestone Mar 21, 2026
mcbosch pushed a commit to mcbosch/networkx that referenced this pull request Apr 7, 2026
…ck_model (networkx#8557)

* Fix sparse heuristic not applied to diagonal blocks in stochastic_block_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.

* add specific test that ensures dense and sparse paths differ

---------

Co-authored-by: Dan Schult <[email protected]>
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.

Not using sparse algorithm in stochastic_block_model code

4 participants