Categories

# 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

# 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
```