Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroDivisionError in "nx.global_reaching_centrality" and "nx.local_reaching_centrality" caused by the graph contains only one node. #6914

Open
joyemang33 opened this issue Sep 7, 2023 · 2 comments

Comments

@joyemang33
Copy link

joyemang33 commented Sep 7, 2023

Hello! Sorry for bothering you again.
When testing NetworkX, I found a ZeroDivisonError crash in nx.global_reaching_centrality when the input graph only contains one node, with the following message:

File "/home/qiuyang/.local/lib/python3.10/site-packages/networkx/algorithms/centrality/reaching.py", line 196, in local_reaching_centrality
    return (len(paths) - 1) / (len(G) - 1)

I am not sure whether reaching_centrality has a good definition in such cases, but this ZeroDivisonError may not be good to show for users. Could you further help me confirm and investigate it, it would be highly appreciated.

The statement that triggers the exception is here.

Best regards,
Joye

Step to Reproduce

import networkx as nx

G = nx.DiGraph([(1, 1)])
G.add_node(1)
print(nx.global_reaching_centrality(G))
print(nx.local_reaching_centrality(G, v=1))

Result

ZeroDivisionError

Environment

NetworkX 3.1
Python 3.10

@joyemang33
Copy link
Author

joyemang33 commented Sep 7, 2023

After the investigation, I found that there are similar issues in nx.flow_hierarchy, non_randomness, approximate_current_flow_betweenness_centrality, edge_current_flow_betweenness_centrality, and current_flow_betweenness_centrality.

I found that Flow Hierarchy has no definition in such graphs, maybe we should return a NetworkX Exception to warn users in such cases. For flow_betweenness_centrality, I am not sure whether we should return a NetworkX Exception or calculate the centrality for such a graph.

import networkx as nx

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

G = nx.Graph()
G.add_node(0)
print(nx.current_flow_betweenness_centrality(G))

Result

ZeroDivisionError

Best regards,
Joye

@joyemang33
Copy link
Author

PS: HITS also failed in such a graph with the message:

File "/home/qiuyang/.local/lib/python3.10/site-packages/networkx/algorithms/link_analysis/hits_alg.py", line 82, in hits
    _, _, vt = sp.sparse.linalg.svds(A, k=1, maxiter=max_iter, tol=tol)
  File "/home/qiuyang/.local/lib/python3.10/site-packages/scipy/sparse/linalg/_eigen/_svds.py", line 443, in svds
    args = _iv(A, k, ncv, tol, which, v0, maxiter, return_singular_vectors,
  File "/home/qiuyang/.local/lib/python3.10/site-packages/scipy/sparse/linalg/_eigen/_svds.py", line 49, in _iv
    raise ValueError(message)
ValueError: `k` must be an integer satisfying `0 < k < min(A.shape)`.
import networkx as nx

G = nx.DiGraph()
G.add_node(1)
print(nx.hits(G))

Result

Exception has occurred: ValueError

Best regards,
Joye

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant