Skip to content

Proposal: Add density to nx.describe defaults. - #8569

Merged
dschult merged 1 commit into
networkx:mainfrom
rossbar:add-density-to-describe
Mar 20, 2026
Merged

dschult merged 1 commit into
networkx:mainfrom
rossbar:add-density-to-describe

Conversation

@rossbar

@rossbar rossbar commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

I propose to add nx.density to the list of default properties reported in nx.describe. Of course, it's already possible to get the density by explicitly passing it in via the property hooks. I think it's worth being included as a default property though: it's relatively cheap to compute and provides valuable information. For example, if you were a user considering converting to an adjacency matrix, you might look at the density to quickly decide whether a sparse or dense representation made more sense.

Anyways - this is purely subjective; happy to close if a lighter describe output is preferred!

else:
info["Number of connected components"] = nx.number_connected_components(G)
# Add density after number of components
info["Density"] = nx.density(G)

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.

We already have the number of edges (m) and number of nodes (n), so the density is a simple formula of those m/(n * (n - 1)). But we'd have to change the formula for directed case. And we should only do this if performance is at all a concern for the describe method -- which it probably isn't. But I wanted to bring it up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I had been thinking the same exact thing :) - I landed on minimizing the changes to describe instead of implementing the formula from scratch using the already-extracted m and n values. Happy to change tacks if others feel strongly!

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

This looks good to me!

@dschult
dschult merged commit 1744843 into networkx:main Mar 20, 2026
39 checks passed
@dschult dschult added this to the 3.7 milestone Mar 20, 2026
mcbosch pushed a commit to mcbosch/networkx that referenced this pull request Apr 7, 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.

3 participants