Skip to content

Instantly share code, notes, and snippets.

@fredrik
Created October 19, 2010 09:27
Show Gist options
  • Save fredrik/633915 to your computer and use it in GitHub Desktop.
Save fredrik/633915 to your computer and use it in GitHub Desktop.
calculate centrality measures for a network using networkx
#!/usr/bin/env python
import networkx as nx
# for each node:
# + degree centrality
# + closeness centrality
# + betweenness centrality
# + eigenvector centrality
# + page rank
def centralissimo(G):
print 'oh.'
centralities = []
centralities.append(nx.degree_centrality(G)); print 'degree centrality: check.'
centralities.append(nx.closeness_centrality(G)); print 'closeness centrality: check.'
centralities.append(nx.betweenness_centrality(G)); print 'betweenness centrality: check.'
centralities.append(nx.eigenvector_centrality(G)); print 'eigenvector centrality: check.'
centralities.append(nx.pagerank(G)); print 'page rank: check.'
for node in G.nodes_iter():
measures = ("\t").join(map(lambda f: str(f[node]), centralities))
print "%s: %s" % (node, measures)
if __name__ == '__main__':
import os
G = nx.read_edgelist(os.path.abspath(os.path.dirname(__file__)) + '/graph.txt', comments='#', delimiter='\t')
centralissimo(G)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment