Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

networkx - meaning of weight in betwenness and current flow betweenness

I'm using Python Networkx 2.1 to calculate Betweenness Centrality and Current Flow Betweenness Centrality on an undirected graph, with weighted edges. My concern is about the meaning of the parameter 'weight' in the networkx functions. Please consider the graph given by the following example

G= nx.Graph()
G.add_path([1, 2,4])
G.add_path([1, 3,4])
G[1][2]['weight'] = 20
G[1][3]['weight'] = 1
G[2][4]['weight'] = 1
G[3][4]['weight'] = 1

for u,v,d in G.edges(data=True):
    if 'weight' in d:
        if d['weight'] != 0:
            d['reciprocal'] = 1/d['weight']

enter image description here Edges weight in my case is strength of relationship, therefore something positive. The idea is that edges with higher weights should contribute more to betweenness. Given this idea, am I correct in saying that the right formulas to calculate node Weighted Betweenness Centrality and Weighted Current Flow Betweenness Centrality are the following?

b = nx.betweenness_centrality(G, weight= 'reciprocal', normalized=False)
Out[46]: {1: 1.0, 2: 1.0, 3: 0.0, 4: 0.0}

f = nx.current_flow_betweenness_centrality(G, normalized= False, weight= 'weight', solver='lu')
Out[48]: 
{1: 1.3114754098360655,
 2: 1.3114754098360657,
 3: 0.6885245901639343,
 4: 0.6885245901639347}

Please note that in the first formula I used the reciprocal of edge weights as it is my feeling that these are interpreted by the algorithm as distances, so something 'bad'. In the second formula, on the other hand, I used the original weight, as in the algorithm of current flow betweenness it seems this gives more importance to nodes 1 and 2, as in betweenness. Therefore here weights seems to be 'positive'.

I'm wondering if I do something wrong. In fact, on larger graphs the two measures correlate more if I use the same weight parameter, and not the reciprocal. How is weight treated in these two algorithms?

like image 635
Forinstance Avatar asked May 23 '18 20:05

Forinstance


1 Answers

This probably isn't of much help to you now, but for others who have the same question...

Looking into the source code reveals that, if weights are provided, Dijkstra's algorithm is used for computing the shortest paths. Dijkstra's algorithm typically treats weights as distances (i.e. "bad"), and this implementation is no exception. Taking the reciprocal of edge weights would therefore be the correct approach. I'm not sure what you mean by "on larger graphs the two measures correlate more". That's going to depend on how similar your large graphs are to one another.

Source code: https://networkx.github.io/documentation/latest/_modules/networkx/algorithms/centrality/betweenness.html

like image 191
Tara Eicher Avatar answered Oct 17 '22 23:10

Tara Eicher