Calculating Freeman’s degree centrality for each node…
…and for the graph.
All the information is contained in an external file graph.csv
, which contains this
# graph.csv "nodes","freq" "n1",3 "n2",1 "n3",1 "n4",2 "n5",1
Where the “freq” column is the number of edges adjacent to the node.
To find node centrality
#!/usr/bin/python # Node Degree Centrality import pandas,numpy # import file print ":: Node Degree Centrality ::" fileName=raw_input("Enter file name:\n") df=pandas.read_csv(fileName) # Calculations gLength=len(df)-1.0 df["CD"]=(df["freq"]/gLength) # Output outputFile="data_"+fileName df.to_csv(outputFile,index=False)
This script outputs the file data_graph.csv
with the following information:
# data_graph.csv nodes,freq,CD n1,3,0.75 n2,1,0.25 n3,1,0.25 n4,2,0.5 n5,1,0.25
Where CD
is the node’s degree centrality.
The graph’s degree centrality is measured with this
#!/usr/bin/python # Graph Degree Centrality import pandas,numpy # import file print ":: Graph Degree Centrality ::" fileName=raw_input("Enter file name:\n") df=pandas.read_csv(fileName) # calculations maxVal=df["freq"].max() df["f1"]=maxVal-df["freq"] addFreq=sum(df["f1"]) dfLength=len(df) deno=(dfLength-1.0)*(dfLength-2.0) gDcent=addFreq/deno # output print "Graph CD =",gDcent
which returns the following
Graph CD = 0.583333333333