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

Strange behavior in nx.effective_size for the single node graph #6916

Open
joyemang33 opened this issue Sep 7, 2023 · 4 comments · May be fixed by #7347
Open

Strange behavior in nx.effective_size for the single node graph #6916

joyemang33 opened this issue Sep 7, 2023 · 4 comments · May be fixed by #7347

Comments

@joyemang33
Copy link

joyemang33 commented Sep 7, 2023

Hello! It seems I've created quite a few issue reports, and I apologize for any inconvenience. Please feel free to review them at your own pace, or you can assign them to me if I can assist in creating PR to fix them.

According to the formula
image
The effective_size should have a well-defined value of 0 for a graph with a single node.
However, if we use Borgatti's method to calculate this graph, the value will lose its definition, and NetworkX will return some strange behavior:

For instance:

import networkx as nx

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

G = nx.Graph([(0, 0)])
print(nx.effective_size(G))

Result:

{0: nan}
ZeroDivisionError

I am not sure whether such behaviors are expected. So it would be highly appreciated if you could further confirm and investigate it.
If they are unexpected, I will create a PR to fix it by returning a single 0 if the graph contains only one node.

Best regards,
Joye

Environment

NetworkX 3.1
Python 3.10

@MridulS
Copy link
Member

MridulS commented Sep 8, 2023

If they are unexpected, I will create a PR to fix it by returning a single 0 if the graph contains only one node.

Yeah we should be catching this corner case. Thanks for your work on this!! Please do send a PR :)

@MridulS
Copy link
Member

MridulS commented Sep 13, 2023

Looking at this one again a code comment explicitly states that for the corner case of isolated nodes while using Borgatti's formula it should be undefined (nan)

# Effective size is not defined for isolated nodes
if len(G[v]) == 0:
effective_size[v] = float("nan")

But the other case is where I guess we need to make a choice. I think for consistency we put in undefined behavior for isolated nodes but according to the reference it should be zero.

# Effective size is not defined for isolated nodes
if len(G[v]) == 0:
effective_size[v] = float("nan")

Also the ZeroDivisionError is popping up because of the self loop which makes the node 0 not isolated and hence the zero division error isn't caught early on, well I guess the question is does a node with a self loop and no other connection an isolated node?

@joyemang33
Copy link
Author

Hi @MridulS. Thanks for your kind help in this issue.
In my opinion, a node with a self-loop and no other connection may need to be regarded as an isolated node, and we need to give a zero value in this case. This is because its effective_size computed by the above equation will still be zero (N(u) \ u = empty set).

That's only my personal opinion, so please feel free to tell me if I misunderstand the question.

Best regards,
Joye

@rossbar
Copy link
Contributor

rossbar commented Mar 13, 2024

IIUC, it seems that the returning nan for isolated nodes is agreed upon as the correct, or at least sensible, behavior. So the remaining question is isolated nodes with self-loops. I'm no expert, but looking at Borgatti's formula in the docstring, it seems that an isolated node with a self-edge would still result in 0 - 0/0, and therefore should also result in nan. Absent any explicit information on how self-loops are treated from the Borgatti/Burt papers, this is what I think makes the most sense.

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