forked from ishani198sheth/Community-Partitioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ishani_sheth_communities.py
144 lines (125 loc) · 4.55 KB
/
ishani_sheth_communities.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
import networkx as nx
import community as com
import sys
import operator
import matplotlib.pyplot as plt
import itertools
import random
random.seed(0)
#reading what file is to be considered as input from the terminal
input_file = sys.argv[1]
input_datafile = open(input_file, "r")
image_file= sys.argv[2]
#initializing an empty list of nodes and edges
list_nodes=[]
list_edges=[]
#creating an empty graph
graph= nx.Graph()
#reading the input file and adding the nodes and edges to its respective list
for line in input_datafile:
data= line.strip().split()
if int(data[0]) not in list_nodes:
list_nodes.append(int(data[0]))
if int(data[1]) not in list_nodes:
list_nodes.append(int(data[1]))
edge=(int(data[0]),int(data[1]))
list_edges.append(edge)
list_nodes.sort()
#adding the nodes and edges to the graph from the input file
graph.add_nodes_from(list_nodes)
graph.add_edges_from(list_edges)
#nx.draw(graph)
#plt.show()
def random_color():
r=hex(random.randint(16,255)).split('x')[1]
g=hex(random.randint(16,255)).split('x')[1]
b=hex(random.randint(16,255)).split('x')[1]
color='#'+r+g+b
return color
#calculating betweeness and removing edges with max betweenness
def betweeness_calculation(graph):
betweeness = nx.edge_betweenness(graph, k=None, normalized=False, weight=None, seed=None)
#print betweeness
maxval = max(betweeness.iteritems(), key=operator.itemgetter(1))[1]
keys = [k for k,v in betweeness.items() if v==maxval]
#print keys
for edge in keys:
graph.remove_edge(*edge)
#print max(betweeness.iteritems(), key=operator.itemgetter(1))[0]
return graph
#for calculating the partition of the graph
#partition returns a dictionary with nodes in a community as key and community name as value
#eg 2:0 where 2 is the node in community 0
def partition(graph):
partition={}
summation_edges=0
graphs = list(nx.connected_component_subgraphs(graph))
for i in range(len(graphs)):
summation_edges=summation_edges+len(list(graphs[i].edges())) #sum of total edges in the graph
community=list(graphs[i].nodes())
for nodes in community:
partition[nodes]=i
return partition,summation_edges
total_edges= graph.number_of_edges()
initial_partition=partition(graph)
initial_modularity= com.modularity(initial_partition[0],graph)
graph_modified=betweeness_calculation(graph)
#print graph_modified.number_of_edges()
modularity_partition_mapping=[]
modularity_partition_mapping.append([initial_modularity,initial_partition[0]])
while(total_edges>0):
#calculate partition on graph_modified
partitions=partition(graph_modified)
total_edges=partitions[1]
if total_edges==0:
#print partitions
break
#calculating modularity
modularity=com.modularity(partitions[0],graph)
#calculate betweenness for a graph
modularity_partition_mapping.append([modularity,partitions[0]])
graph_modified1=betweeness_calculation(graph_modified)
#appending all modularities to list and getting the max modularity out of it
total_modularities=[]
for elements in modularity_partition_mapping:
total_modularities.append(elements[0])
#print modularity_partition_mapping
max_modularity=max(total_modularities)
#getting the partition associated with maximum modularity
max_partition={}
for element in modularity_partition_mapping:
if element[0]==max_modularity:
max_partition=element[1]
#print element
#getting all the communities from the partition in a dictionary with communities as key and list of elements in it as values
v = {}
for key, value in sorted(max_partition.iteritems()):
v.setdefault(value, []).append(key)
# printing all the communities
for items in v:
print v[items]
#creating the final graph
final_graph=nx.Graph()
for items in v:
#edges=list(itertools.combinations(v[items],2))
final_graph.add_nodes_from(v[items])
final_graph.add_edges_from(list_edges)
pos=nx.spring_layout(final_graph)
for items in v:
#edges_colored=list(itertools.combinations(v[items],2))
nx.draw_networkx_nodes(final_graph,pos,
nodelist=v[items],
node_color=random_color(),
node_size=500,
alpha=1)
nx.draw_networkx_edges(final_graph,pos,
edgelist=list_edges,
width=1,alpha=1,edge_color='k')
#nx.draw(final_graph)
labels={}
for i in list_nodes:
labels[i]=i
nx.draw_networkx_labels(final_graph,pos,labels,font_size=12)
plt.axis('off')
plt.savefig(image_file)
plt.show()