Here is some code - essentially the same as
networkx.degree_centrality() - that computes in- and out-degree
centrality.
Note that for multigraphs the standard normalization (divide by N-1,
where N is the number of nodes) means that nodes with parallel edges
can have degree centrality values greater than 1.
-----
def in_degree_centrality(G,v=None):
if v is not None:
return G.in_degree(v)/(G.order()-1.0)
in_degree_centrality={}
s=1.0/(G.number_of_nodes()-1.0)
for n,deg in G.in_degree_iter():
in_degree_centrality[n]=deg*s
return in_degree_centrality
def out_degree_centrality(G,v=None):
if v is not None:
return G.out_degree(v)/(G.order()-1.0)
out_degree_centrality={}
s=1.0/(G.number_of_nodes()-1.0)
for n,deg in G.out_degree_iter():
out_degree_centrality[n]=deg*s
return out_degree_centrality
if __name__ == '__main__':
import networkx
G=networkx.MultiDiGraph()
G.add_edge(1,2)
G.add_edge(1,2)
G.add_edge(2,3)
print "degree centrality"
print networkx.degree_centrality(G)
print "in degree centrality"
print in_degree_centrality(G)
print "out degree centrality"
print out_degree_centrality(G)
-----
Aric