Skip to content
Advertisement

calculate indegree centralization of graph with python networkx

I have a graph and want to calculate its indegree and outdegree centralization. I tried to do this by using python networkx, but there I can only find a method to calculate indegree and outdegree centrality for each node. Is there a way to calculate in- and outdegree centralization of a graph in networkx?

Advertisement

Answer

Here’s the code. I’m assuming that in-degree centralization is defined as I describe below…

N=G.order()
indegrees = G.in_degree().values()
max_in = max(indegrees)
centralization = float((N*max_in - sum(indegrees)))/(N-1)**2

Note I’ve written this with the assumption that it’s python 2, not 3. So I’ve used float in the division. You can adapt as needed.


begin definition

Given a network G, define let y be the node with the largest in-degree, and use d_i(u) to denote the in-degree of a node u. Define H_G to be (I don’t know a better way to write mathematical formulae on stackoverflow – would appreciate anyone who knows to either edit this or give a comment)

H_G = sum_{u} d_i(y) - d_i(u)  
    =  N d_i(u) - sum_u d_i(u)

where u iterates over all nodes in G and N is the number of nodes of G.

The maximum possible value for a graph on N nodes comes when there is a single node to which all other nodes point to and no other nodes have edges to them. Then this H_G is (N-1)^2.

So for a given network, we define the centralization to be it’s value of H_G compared to the maximum. So C(G) = H_G/ (N-1)^2.

end definition

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement