On Monday, April 29, 2013 5:41:51 PM UTC-5, Rajat Khanna wrote:
I am planning to estimate degree centrality for each node and use this to construct probability distribution...I can try to di it myself but was just wondering if someone has implemented it already...would be helpful to see some exmaples/applications...
There are many ways to define a distribution from degree centralities, and it depends on the type of graph you have as well. Let's assume you have a simple graph (no self-loops or multiple edges). Then, the centralities for each node are properly normalized in the output of nx.degree_centrality(). However, they do not sum to 1. So, we can normalize each by dividing by the sum. Here is demo code:
from __future__ import division
import networkx as nx
import numpy as np
def entropy(dist):
"""
Returns the entropy of `dist` in bits (base-2).
"""
dist = np.asarray(dist)
ent = np.nansum( dist * np.log2( 1/dist ) )
return ent
def centrality_distribution(G):
"""
Returns a centrality distribution.
Each normalized centrality is divided by the sum of the normalized
centralities. Note, this assumes the graph is simple.
"""
centrality = nx.degree_centrality(G).values()
centrality = np.asarray(centrality)
centrality /= centrality.sum()
return centrality
if __name__ == '__main__':
G = nx.barabasi_albert_graph(10, 2)
d = centrality_distribution(G)
print d
print entropy(d)