Repository navigation
Fixed diameter/radius error for null graphs - #8454
Conversation
| if len(G) == 0: | ||
| raise nx.NetworkXPointlessConcept("Cannot compute diameter of null graph.") |
There was a problem hiding this comment.
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]: {}There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
null != empty A null graph is 0 nodes and 0 edges, an empty graph can have nodes!
There was a problem hiding this comment.
Thanks for the clarification on null vs empty graphs. i'll be precise with that.
|
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. |
|
Thanks for the review and the merge! Happy to contribute. |
This PR modifies
diameterandradiusinnetworkx/algorithms/distance_measures.pyto 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.NetworkXPointlessConceptwith a descriptive message ("Cannot compute diameter of null graph"), which aligns with how NetworkX handles other undefined metrics.Fixes #8453
Changes Made
if len(G) == 0todiameterandradius.test_diameter_radius_empty_graphintest_distance_measures.py.Raises: NetworkXPointlessConcept.Verification
pytest networkx/algorithms/tests/test_distance_measures.pyand all tests passed.