Skip to content

Conversation

@Peiffap
Copy link
Member

@Peiffap Peiffap commented Sep 5, 2025

Fixes #8227.

This PR adds the check suggested in the linked issue for zero-degree nodes, which cannot appear in a valid degree sequence.

To do so, a utility function is_valid_tree_degree_sequence has been added, which returns whether a degree sequence follows two basic rules: the number of nodes must be equal to one more than the number of edges, and there cannot be any negative degrees in the degree sequence unless the degree sequence represents the trivial graph. It also gives a string representing which of the two checks was failed, to simplify error reporting downstream; I wasn't sure whether returning the string was worth doing; perhaps downstream functions using this utility function should just raise with a generic "invalid degree sequence for tree" message.

This change affects random_powerlaw_tree_sequence in that it now becomes (significantly?) less likely to yield a "valid" sequence in the given number of tries.

Copy link
Member

@dschult dschult left a comment

Choose a reason for hiding this comment

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

This is great -- the addition of a missing useful utility for the tree suite of functions.
Thanks!

(I have one minor suggestion, but only if you prefer it -- and feel free to change the name choices to something else. :)

Co-authored-by: Dan Schult <dschult@colgate.edu>
@Peiffap
Copy link
Member Author

Peiffap commented Sep 6, 2025

Thanks Dan, your suggestion improves readability. This PR still needs a label by the way! :)

Comment on lines +192 to +193
t(nx.random_powerlaw_tree, 5, seed=seed, tries=5000)
t(nx.random_powerlaw_tree_sequence, 5, seed=seed, tries=5000)
Copy link
Member

Choose a reason for hiding this comment

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

Any particular reason for changing the n here?

Copy link
Member Author

Choose a reason for hiding this comment

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

These tests exceed the maximum number of tries with the old values now that invalid sequences are filtered more thoroughly.

@MridulS MridulS merged commit 2d4f1d0 into networkx:main Sep 7, 2025
47 of 48 checks passed
@dschult dschult added this to the 3.6 milestone Sep 7, 2025
@Peiffap Peiffap deleted the bug/dst branch September 7, 2025 09:12
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.

BUG: degree_sequence_tree accepts degree sequences with isolated nodes

3 participants