-
Notifications
You must be signed in to change notification settings - Fork 2
/
SocialNetworkMeasures.py
180 lines (168 loc) · 5 KB
/
SocialNetworkMeasures.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import networkx as nx
import matplotlib.pyplot as plt
import sys
#------
# DATA
#------
G=nx.Graph()
with open("../Resource/"+sys.argv[1]) as f:
lines = f.readlines()
x = [int(line.split()[0]) for line in lines]
y = [int(line.split()[1]) for line in lines]
for i in range(len(x)):
G.add_edge(x[i],y[i])
#Show graph
nx.draw(G, node_color='green', with_labels = True)
plt.show()
def InformationOfGraph():
# Read Information of Graph
print("-------------------------------------------------")
print("InformationOfGraph:")
print("Nodes: ",G.nodes())
print("Edges: ",G.edges())
print("Number of Nodes: ",G.number_of_nodes())
print("Number of Edges: ",G.number_of_edges())
print("-------------------------------------------------")
def Density():
# Calculate Density
n=G.number_of_nodes()
m=G.number_of_edges()
D=m/(n*(n-1)/2)
print("-------------------------------------------------")
print("Density of this network : ",D)
print("-------------------------------------------------")
def DegreeCentrality():
# Calculate DegreeCentrality
TempCd=[]
Sum=0;
for i in range(0,len(G.nodes())):
m=len(G.edges(i))
n=G.number_of_nodes()
Cd=m/(n-1)
Sum+=Cd
TempCd.append(Cd);
if(i<10):
print("DegreeCentrality of Node",i," : ",Cd)
else:
print("DegreeCentrality of Node",i,": ",Cd)
# Find Average DegreeCentrality
Avg=Sum/len(G.nodes())
# Find Highest Lowest DegreeCentrality
Max=max(TempCd)
Min=min(TempCd)
# Identify that nodes
a=[]
b=[]
for i in range(0,len(TempCd)):
if(TempCd[i]==Max):
a.append(i);
if(TempCd[i]==Min):
b.append(i);
print("--------------------------------------------------")
print("Average DegreeCentrality : ",Avg)
print("Lowest DegreeCentrality : ",Min,"( Node",b,")")
print("Highest DegreeCentrality : ",Max,"( Node",a,")")
print("--------------------------------------------------")
def ClosenessCentrality():
# Calculate ClosenessCentrality
closeness=nx.closeness_centrality(G)
Sum=0;
TempCc=[]
for i in range(0,len(closeness)):
Sum+=closeness[i];
TempCc.append(closeness[i]);
if(i<10):
print("ClosenessCentrality of Node",i," : ",closeness[i])
else:
print("ClosenessCentrality of Node",i,": ",closeness[i])
# Find Average ClosenessCentrality
Avg=Sum/len(G.nodes())
# Find Highest Lowest ClosenessCentrality
Max=max(TempCc)
Min=min(TempCc)
a=[]
b=[]
for i in range(0,len(TempCc)):
if(TempCc[i]==Max):
a.append(i);
if(TempCc[i]==Min):
b.append(i);
# Identify that nodes
print("------------------------------------------------------")
print("Average ClosenessCentrality : ",Avg)
print("Lowest ClosenessCentrality : ",Min,"( Node",b,")")
print("Highest ClosenessCentrality : ",Max,"( Node",a,")")
print("------------------------------------------------------")
def BetweennessCentrality():
# Calculate BetweennessCentrality
betweenness=nx.betweenness_centrality(G)
Sum=0;
TempCb=[]
for i in range(0,len(betweenness)):
Sum+=betweenness[i];
TempCb.append(betweenness[i]);
if(i<10):
print("BetweennessCentrality of Node",i," : ",betweenness[i])
else:
print("BetweennessCentrality of Node",i,": ",betweenness[i])
# Find Average BetweennessCentrality
Avg=Sum/len(G.nodes())
# Find Highest Lowest BetweennessCentrality
Max=max(TempCb)
Min=min(TempCb)
a=[]
b=[]
for i in range(0,len(TempCb)):
if(TempCb[i]==Max):
a.append(i);
if(TempCb[i]==Min):
b.append(i);
# Identify that nodes
print("------------------------------------------------------")
print("Average BetweennessCentrality : ",Avg)
print("Lowest BetweennessCentrality : ",Min,"( Node",b,")")
print("Highest BetweennessCentrality : ",Max,"( Node",a,")")
print("------------------------------------------------------")
def ClusteringCentrality():
# Calculate ClusteringCentrality
clustering=nx.clustering(G)
Sum=0;
TempCclustering=[]
for i in range(0,len(clustering)):
Sum+=clustering[i];
TempCclustering.append(clustering[i]);
if(i<10):
print("ClusteringCentrality of Node",i," : ",clustering[i])
else:
print("ClusteringCentrality of Node",i,": ",clustering[i])
# Find Average ClusteringCentrality
Avg=Sum/len(G.nodes())
# Find Highest Lowest ClusteringCentrality
Max=max(TempCclustering)
Min=min(TempCclustering)
a=[]
b=[]
for i in range(0,len(TempCclustering)):
if(TempCclustering[i]==Max):
a.append(i);
if(TempCclustering[i]==Min):
b.append(i);
# Identify that nodes
print("------------------------------------------------------")
print("Average ClusteringCentrality : ",Avg)
print("Lowest ClusteringCentrality : ",Min,"( Node",b,")")
print("Highest ClusteringCentrality : ",Max,"( Node",a,")")
print("------------------------------------------------------")
#--------
# Result
#--------
InformationOfGraph()
Density()
print("\n")
DegreeCentrality()
print("\n")
ClosenessCentrality()
print("\n")
BetweennessCentrality()
print("\n")
ClusteringCentrality()