Skip to content

Add check for empty graphs in non_randomness - #7395

Merged
dschult merged 4 commits into
networkx:mainfrom
elshorbagyx:add-check-to-non-randomness
May 1, 2024
Merged

dschult merged 4 commits into
networkx:mainfrom
elshorbagyx:add-check-to-non-randomness

Conversation

@elshorbagyx

Copy link
Copy Markdown
Contributor

In the light of #7393, I have adopted the same approach to solve empty graph corner case in non_randomness as it was also producing ZeroDivisionError

import networkx as nx
G = nx.Graph()
G.add_node(1)
print(nx.non_randomness(G))

@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 for this!
I have a suggestion for a one-liner to create the graph with 1 node.

Co-authored-by: Dan Schult <[email protected]>

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

This one is a bit trickier than it appears: the only case that is not currently handled is when G has only one node and 0 edges. The is_connected check already handles all other graphs with no edges, so long as they have more than one node.

So, in order to be consistent with the current behavior, I think you'd have to add another condition, i.e. nx.is_empty(G) and len(G) == 1.

It's not a huge deal, especially given the exception class hierarchy (which, speaking of, I think the exception type on line 81 should be changed from NetworkXException to NetworkXError), but IMO it's always good to be explicit about where exactly exceptions are coming from and to preserve behavior as much as possible.

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

I took the liberty of pushing up the last couple tweaks from my previous review - this LGTM, thanks @mohamedrezk122 !

@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 all looks good to me!

@dschult
dschult merged commit 20eabd6 into networkx:main May 1, 2024
@jarrodmillman jarrodmillman added this to the 3.4 milestone May 1, 2024
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.

4 participants