问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/56569070/is-this-betweenness-calculation-correct