Skip to content

Fixed diameter/radius error for null graphs - #8454

Merged
MridulS merged 6 commits into
networkx:mainfrom
DeepBuha06:fix-empty-graph-radius
Jan 12, 2026
Merged

MridulS merged 6 commits into
networkx:mainfrom
DeepBuha06:fix-empty-graph-radius

Conversation

@DeepBuha06

Copy link
Copy Markdown
Contributor

This PR modifies diameter and radius in networkx/algorithms/distance_measures.py to explicitly check for empty graphs.

Previously, calling these functions on an empty graph raised a generic ValueError: max() iterable argument is empty.
Now, they raise a specific nx.NetworkXPointlessConcept with a descriptive message ("Cannot compute diameter of null graph"), which aligns with how NetworkX handles other undefined metrics.

Fixes #8453

Changes Made

  • Added a length check if len(G) == 0 to diameter and radius.
  • Added a test test_diameter_radius_empty_graph in test_distance_measures.py.
  • Updated docstrings to include Raises: NetworkXPointlessConcept.

Verification

  • Verified the fix locally.
  • Ran pytest networkx/algorithms/tests/test_distance_measures.py and all tests passed.

Comment on lines +407 to +408
if len(G) == 0:
raise nx.NetworkXPointlessConcept("Cannot compute diameter of null graph.")

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.

I think these errors should be raised via eccentricity, as PointlessConcept is about the eccentricity of a null graph. Currently we just return the empty dictionary, it should raise NetworkXPointlessConcept.

In [1]: import networkx as nx

In [2]: G = nx.Graph()

In [3]: nx.eccentricity(G)
Out[3]: {}

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.

Understood. Moving the check to eccentricity makes sense as it's the primitive used by diameter/radius. I'll fix that to raise NetworkXPointlessConcept inside eccentricity for null graphs.

NetworkXError
If the graph consists of multiple components
NetworkXPointlessConcept
If the graph is empty

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.

null != empty A null graph is 0 nodes and 0 edges, an empty graph can have nodes!

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.

Thanks for the clarification on null vs empty graphs. i'll be precise with that.

@DeepBuha06

Copy link
Copy Markdown
Contributor Author

I have moved the null graph check to eccentricity as suggested and updated the documentation to explicitly refer to 'null graphs' (0 nodes) instead of 'empty graphs' to ensure precision. Ready for review.

@MridulS MridulS changed the title Fixed diameter/radius error on empty graphs Fixed diameter/radius error for null graphs Jan 12, 2026
@MridulS
MridulS merged commit d3e0182 into networkx:main Jan 12, 2026
46 checks passed
@dschult dschult added this to the 3.7 milestone Jan 12, 2026
@DeepBuha06

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the merge! Happy to contribute.

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.

Improve error handling for diameter/radius on empty graphs

4 participants