Skip to content

Add check for empty graphs in flow_hierarchy - #7393

Merged
rossbar merged 6 commits into
networkx:mainfrom
elshorbagyx:add-check-to-flow-hierarchy
Apr 18, 2024
Merged

rossbar merged 6 commits into
networkx:mainfrom
elshorbagyx:add-check-to-flow-hierarchy

Conversation

@elshorbagyx

Copy link
Copy Markdown
Contributor

The following snippet produces a division by zero error due to the lack of null graph catching condition, I just added it and updated the tests :)

import networkx as nx
G = nx.DiGraph()
G.add_node(1)
print(nx.flow_hierarchy(G))

@Schefflera-Arboricola Schefflera-Arboricola 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 @mohamedrezk122 !

I've left a few comments/suggestions below.

And I was curious about what we do with the self-loops here(we consider them). But, I took a quick glance at the paper in the references and I think those are not allowed(but, I might be wrong; I haven't gone through it very deeply). From pdf's page 6("RESULTS AND DISCUSSIONS" section)

No multiple uni-directed links between a chosen pair and
no self-links are allowed.

Comment thread networkx/algorithms/hierarchy.py Outdated
http://web.mit.edu/~cmagee/www/documents/28-DetectingEvolvingPatterns_FlowHierarchy.pdf
"""
# corner case: G has no edges
if G.size() == 0:

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.

Suggested change
if G.size() == 0:
if nx.is_empty(G):

here, using G.is_empty might be more efficient because we won't be iterating over all the edges.

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.

Nice -- I bet this idiom could be used in other places in the codebase too.

Comment thread networkx/algorithms/hierarchy.py Resolved
@elshorbagyx

Copy link
Copy Markdown
Contributor Author

Thanks @Schefflera-Arboricola for the review, I have pushed the changes.

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

Thanks @mohamedrezk122 - I went ahead and made some suggestions to reinforce @Schefflera-Arboricola 's comments from review (which were spot on, thanks!)

Comment thread networkx/algorithms/hierarchy.py Resolved
Comment thread networkx/algorithms/hierarchy.py Resolved
@elshorbagyx elshorbagyx changed the title Add check for null graphs in flow_hierarchy Add check for empty graphs in flow_hierarchy Apr 7, 2024

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

LGTM, thanks @mohamedrezk122 and @Schefflera-Arboricola & @dschult for the reviews!

@Schefflera-Arboricola Schefflera-Arboricola 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 @mohamedrezk122 !

  * Add match= to pytest.raises to ensure right code path is being hit
  * Parametrize on number of nodes for the empty graph.
@rossbar
rossbar merged commit 18f4230 into networkx:main Apr 18, 2024
@jarrodmillman jarrodmillman added this to the 3.4 milestone Apr 18, 2024
G = nx.DiGraph()
G.add_node(1)
pytest.raises(nx.NetworkXError, nx.flow_hierarchy, G)
@pytest.mark.parametrize("n", (0, 1, 3))

@Schefflera-Arboricola Schefflera-Arboricola Apr 19, 2024 •

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.

@rossbar why did you decide to test on 3 empty graphs here?
We just check if the graph is edgeless or non-edgeless. so then why do we need different cases of edgeless graphs(i.e. 0 nodes 0 edges, 1 node 0 edges, n nodes 0 edges) when they are all the same for this algorithm? "Same" in the sense, that they all lie in the "empty graph" corner-case space.

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.

Although these graphs have the same property in that they contain no edges, there's still enough distinction between these cases that it doesn't hurt to test them! In other words, I think that each one of these cases is "special" enough to be worth testing. For example, there are plenty of places in NX where there are checks on the number of nodes; with the numbers zero (null graph) and one (a singleton node) being special.

In the future, someone might decide to completely re-implement flow_hierarchy - during their rewriting, they may replace the is_empty check with something like G.number_of_nodes() < 1 (or <=). In those cases, the test suite would've passed for some, but missed the case where there are multiple nodes with no edges. One of the primary uses of the test suite is to ensure that changes to code don't unintentionally modify behavior - so tests that cover potential behavior changes are valuable!

All in all the above hypothetical is not a very likely scenario, but IMO it's a principle of good test design to think about the semantics you are trying to enforce, and make sure you probe them as completely as possible. Of course, there's always a tradeoff with test runtime, but in this case adding an extra check is extremely cheap so that's not a concern.

cvanelteren pushed a commit to cvanelteren/networkx that referenced this pull request Apr 22, 2024
* Add check for null graphs in flow_hierarchy

Co-authored-by: Ross Barnowski <[email protected]>
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.

5 participants