NetworkX中的中介中心性:逻辑错误

时间:2016-03-25 10:45:25

标签: python networkx

我通过以下方式计算佛罗伦萨家庭图的中介中心性:

import networkx as nx

# build up a graph
G = nx.florentine_families_graph()
bw_centrality = nx.betweenness_centrality(G, normalized=False)

摘自networkx中的betweenness_centrality(...)

  

节点v的中介中心性是通过v的所有对最短路径的分数之和:

因此,中介中心性应小于1。但是,我得到了结果:(红色节点的中介中心性,' Medici',是47.5

enter image description here

我计算中介中心性的方式如下,

node_and_times = dict.fromkeys(G.nodes(), 0) # a dict of node : the number of shortest path passing through node
sum_paths = 0

for s, t in itertools.product(G.nodes(), repeat=2): # all pair of nodes <s, t>
    paths = nx.all_shortest_paths(G, s, t) # generator of lists
    for path in paths:
        sum_paths += 1

        # stats nodes passing through shortest path
        for node in path[1:-1]: # intermediate nodes
            node_and_times[node] += 1

bw_centrality = {k : v*1.0/sum_paths for k, v in node_and_times.items()}

我得到了以下结果,

enter image description here

我是对的吗?

正如回答者所提到的,删除normalized=False会得到以下结果,这与我的计算不一致。

enter image description here

2 个答案:

答案 0 :(得分:6)

中介中心性的定义并不意味着它的值小于1.分数的总和不必小于1.

考虑以下示例: enter image description here

最短的路径和它们经过的节点是:

A -> B: None
A -> C: B
A -> D: B, C
A -> E: B, C, D
B -> C: None
B -> D: C
B -> E: C, D
C -> D: None
C -> E: D
D -> E: None

因此,计算来自formula的'B'的中介中心性:

enter image description here

我们得到:

shortest paths A -> C that go through B = 1
shortest paths A -> C = 1
shortest paths A -> D that go through B = 1
shortest paths A -> D = 1
shortest paths A -> E that go through B = 1
shortest paths A -> E = 1
1/1 + 1/1 + 1/1 = 3

因此所有节点的中介中心性:

A: 0
B: 3
C: 4
D: 3
E: 0

用Python计算:

import networkx as nx

# build up a graph
G = nx.Graph()
G.add_nodes_from(['A', 'B', 'C', 'D', 'E'])
G.add_edges_from([('A', 'B'), ('B','C'), ('C', 'D'), ('D', 'E')])
bw_centrality = nx.betweenness_centrality(G, normalized=False)

print bw_centrality
# returns {'A': 0.0, 'C': 4.0, 'B': 3.0, 'E': 0.0, 'D': 3.0}

因此,如果您想获得小于1的值,则必须使用normalized=Truewiki about normalization)。

取自here

的示例

答案 1 :(得分:4)

答案在于“normalized = False”。删除它,您将获得小于1的值。