Categories
Uncategorized

Graph Degree Centrality iGraph – Pythom

This Python code will read a Pajek graph and calculate its degree centrality using this formula …

To test the result I will be using example 17 from Freeman’s (1978) Centrality in Social Networks: Conceptual Clarification. Freeman’s calculation for this example is 0.42.

The graph is written in a file named graph17.net, which reads:

*Vertices 5

*Edges
1 2
1 5
5 2
5 4
4 3

This is the Python code, using the igraph module.

import igraph

# load grpah
g=igraph.read("graph17.net",format="pajek")

# simplify
sg=g.simplify()

# find largest degree value
degMax=max(sg.degree())

# get list of all degrees
allDegs=sg.degree()

# create list to store results
calcList=[]

# loop for subtracting degree from 
# maximum degree for all nodes
for x in allDegs:
	calc=degMax-x
	calcList.append(calc)

# sum all results for enumerator
degr=sum(calcList)

# calculate denominator
deno=(sg.vcount()-1.0)*(sg.vcount()-2.0)

# divide them
cDeg=degr/deno

# format results
gCentr="gDc: "+str("%.3f" % cDeg)

# print results
print gCentr

That should return

gDc: 0.417

Which is the correct answer!.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s