-
Notifications
You must be signed in to change notification settings - Fork 0
/
commDet.py
210 lines (167 loc) · 3.71 KB
/
commDet.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import sys
from operator import itemgetter
from math import log10
from random import randint
from threading import *
Vert={}
G={}
visitednodes=[]
origin=[]
add=0
v=[]
print(sys.argv[1])
f=open("../datasets/"+sys.argv[1], "r")
lines=f.readlines()
for line in lines:
inp=line.split()
inp=[int(x) for x in inp]
if inp[0] in G:
G[inp[0]][0]+=[inp[1],]
G[inp[0]][1]+=[inp[2],]
else:
G[inp[0]]=[[inp[1]], [inp[2]]]
Vert[inp[0]]=0
Vert[inp[1]]=0
f.close()
print("Loaded Graph")
print(len(Vert))
numrank=[]
degrank=[]
for key, value in G.items():
numrank+=[[key, len(value[0])],]
degrank+=[[key, sum(value[1])],]
numrank=sorted(numrank, key=itemgetter(1))
degrank=sorted(degrank, key=itemgetter(1))
seeds=float(sys.argv[2])
'''
Enter seeds = 1-(k/100)
'''
numrank=numrank[int(len(numrank)*seeds):]
degrank=degrank[int(len(degrank)*seeds):]
numrank=[a[0] for a in numrank]
degrank=[a[0] for a in degrank]
numrank=sorted(numrank)
degrank=sorted(degrank)
origin=[]
i=len(degrank)-1
j=i
while i>=0:
if degrank[i]>numrank[j]:
i-=1
elif degrank[i]<numrank[j]:
j-=1
else:
origin+=[degrank[i],]
degrank[i]=-1
numrank[j]=-1
i-=1
j-=1
print("Origin initialized with ", len(origin), " nodes")
i=0
######################################################################################
#UPDATE : This step is unnecessary
'''
# Comment out this section for small graphs
conflicts=[]
knn=0
current=[]
for i in origin:
current+=[i, ]
def nextN(N, v):
global conflicts
global origin
source=[v]
vis=[v]
nex=[]
for i in range(N):
nbr=[]
for node in source:
if node in G:
nbr+=G[node][0]
nbr=list(set(nbr))
for vert in nbr:
if vert in origin and vert not in vis:
conflicts+=[(v, vert),]
vis+=nbr
source=[]
for vert in nbr:
source+=[vert,]
for o in origin:
nextN(1, o)
#print(conflicts)
conflicts=list(set(conflicts))
print(len(conflicts))
conf_map={}
for conflict in conflicts:
if conflict[1] in conf_map:
conf_map[conflict[1]]+=1
else:
conf_map[conflict[1]]=1
'''
#####################################################################################
#print(origin)
print("Spread Start with ", len(origin), " nodes")
for i in origin:
Vert[i]=i # Initialize labels
'''
Replace randomizer here!!!!!!!
Tested randomizers :
Python random library
Dump of n random numbers from random.org
TODO : Integrate with random.org API instead of using dump.
'''
add=0
rand=open("random", "r")
lines=rand.readlines()
i=0
def spread(arry, tLock):
global origin
global G
global lines
global visitednodes
global add
global Vert
global i
for v in arry:
if v in G:
c=0
for j in range(0,len(G[v][0])):
if Vert[G[v][0][j]]==0:
c+=1
if float(lines[(i%len(lines))])<=(((G[v][1][j])**(1/4))/(sum(G[v][1])**(1/4))): #REPLACE lines[] with random function
#if randint(0,100)/100 <= (((G[v][1][j])**(1/4))/(sum(G[v][1])**(1/4))):
tLock.acquire()
origin+=[G[v][0][j],]
Vert[G[v][0][j]]=Vert[v]
add=0
c-=1
tLock.release()
i+=1
if c==0:
origin.remove(v)
visitednodes+=[v,]
else:
origin.remove(v)
visitednodes+=[v,]
def chunkify(lst,n):
return [ lst[i::n] for i in range(n) ]
threadLock=Lock()
while add<=3:
add+=1
threads=[]
if len(origin)<=200:
for x in chunkify(origin, int(len(origin)/2)):
threads.append(Thread(target=spread, args=(x, threadLock)))
else:
for x in chunkify(origin,100):
threads.append(Thread(target=spread, args=(x, threadLock)))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
#out=open(sys.argv[1], "w")
out=open("../outputs/"+sys.argv[1]+"_"+str(seeds*100), "w")
for key, val in Vert.items():
out.write(str(key) + " " + str(val)+"\n")
out.close()