python - Unexpected results with betweenness_centrality -


below code create simple graph in networkx using python 2.7 call return betweenness_centrality:

import networkx nx g = nx.graph() g.add_nodes_from([1,3]) g.add_edge(1,2) g.add_edge(2,3) g.add_edge(1,3) g[1][2]['weight']=4400 g[2][3]['weight']=4100 g[1][3]['weight']=1500 print nx.betweenness_centrality(g,weight='weight') 

i expected see weights assigned, weights zero:

{1: 0.0, 2: 0.0, 3: 0.0}

i missing simple, , cannot see on-line documentation. thank you.

the default networkx.betweenness_centrality() (and arguably standard definition) not include counting endpoints. k3 graph betweenness on each node 0. if want count endpoints use

in [1]: import networkx nx  in [2]: g = nx.graph()  in [3]: g.add_nodes_from([1,3])  in [4]: g.add_edge(1,2)  in [5]: g.add_edge(2,3)  in [6]: g.add_edge(1,3)  in [7]: g[1][2]['weight']=4400  in [8]: g[2][3]['weight']=4100  in [9]: g[1][3]['weight']=1500  in [10]: print(nx.betweenness_centrality(g,weight='weight',endpoints=true)) {1: 2.0, 2: 2.0, 3: 2.0} 

note 'weight' attribute used find shortest path , not counted directly in betweenness score. example nonsymmetric paths in loop:

in [1]: import networkx nx  in [2]: g = nx.cycle_graph(4)  in [3]: nx.set_edge_attributes(g,'weight',1)  in [4]: print(nx.betweenness_centrality(g,weight='weight')) {0: 0.16666666666666666, 1: 0.16666666666666666, 2: 0.16666666666666666, 3: 0.16666666666666666}  in [5]: g[0][1]['weight']=5  in [6]: print(nx.betweenness_centrality(g,weight='weight')) {0: 0.0, 1: 0.0, 2: 0.6666666666666666, 3: 0.6666666666666666} 

Comments