6
votes

I try to calculate betweenness for all nodes for the path from 2 to 6 in this simple graph.

G=nx.Graph()
edge=[(1,5),(2,5),(3,5),(4,5),(4,6),(5,7),(7,6)]
G.add_edges_from(edge)
btw=nx.betweenness_centrality_subset(G,[2],[6])

However the result is:

{1: 0.0, 5: 0.5, 2: 0.0, 3: 0.0, 4: 0.25, 6: 0.0, 7: 0.25}

I was wondering why the betweenness for node 5 is 0.5 while it should be 1 since the number of total shortest path is 2 and both of them include 5 and node 4 and 7 should be 0.5

2
As noted by @abc, this looks like a bug. I've submitted a bug report. Until the bug is fixed in a later update, the correction is to multiply all results by 2 (as long as the graph is undirected). Luckily this bug will not affect any rankings. - Joel
There is another problem I found and I'm not sure why it's in this way. If you consider a graph with edges (1,2) (1,5) (2,3) (3,6) (6,5) (4,2) then the betweenness should be {1:1.5, 2:2.5, 3:2.5, 4:0, 5:1, 6:1}. However, the results are these but multiply by 0.1. I can't understand why - Mohammad.sh
For this new graph, it's not clear to me what betweenness you are calculating (between which nodes?). The betweenness should be greater than 1 - the betweenness of v is the probability a shortest path from source to target goes through v. - Joel
well, I calculated the betweenness for all possible pairs for each node. However, the problem is that the output from networkx is less than 1. the output is : {1: 0.15, 2: 0.25, 3: 0.25, 4: 0, 5: 0.1, 6: 0.1} - Mohammad.sh
what command did you run to get that output? - Joel

2 Answers

4
votes

It looks like a bug.

Here my guess. The bug seems coming from the _rescale function. Here, if the graph is indirected the computed values are multiplied by 0.5.

Since in the general betweenness_centrality a node is considered twice (shortest paths are computed for each node in the graph) for the betweenness_centrality_sub this is not necessary since shortest paths are only computed for the sources nodes.

Example:

nx.betweenness_centrality_subset(G,[2,6],[2,6])
# {1: 0.0, 5: 1.0, 2: 0.0, 3: 0.0, 4: 0.5, 6: 0.0, 7: 0.5}

So, if my guess is right, you just need to multiply by 2 the computed result.

0
votes

I believe this is not a bug.

First of all, be careful about normalization of the results. When not normalized you get the number of paths through each node. When normalized you get the fraction of paths. By default nx.betweenness_centrality(G) is normalized. By default nx.betweenness_centrality_subset(G,[2],[6]) is NOT normalized.

Secondly, for undirected graphs, the non-normalized betweenness_centrality values count undirected paths. This means each directed path counts as one half of an undirected path. That is why your original post have values 0.5, and 0.25 rather than 1.0 and 0.5. The paths are counted this way to make sure that you never get more paths than the total number of undirected paths in the network. When you normalize, this is not an issue.