In and Out Degree Centrality?

203 views
Skip to first unread message

Mike Edwards

unread,
Apr 17, 2009, 9:26:26 AM4/17/09
to networkx-discuss, mi...@onearmedman.com
I'm working on a MultiDiGraph of a set of social relationships. I've
used degree centrality to work out who is most connected in the group
I'm studying, but I'd really like to know, more specifically, the
degree centrality for edges going out and also for edges going in,
rather than just every connection. That way, I can tell which people
are "reporters" (lots of outward connections) and which are sources
(lots of inward connections) and maybe even compare these to what I'm
seeing with betweenness.

Is there a good way to do this in NetworkX?

Aric Hagberg

unread,
Apr 17, 2009, 10:26:45 AM4/17/09
to networkx...@googlegroups.com

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

Mike Edwards

unread,
Apr 29, 2009, 3:24:35 PM4/29/09
to networkx-discuss
Ah, that was tremendously helpful! Definitely a good addition to the
package, if that's possible. Thanks!

On Apr 17, 10:26 am, Aric Hagberg <aric.hagb...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages