-
Notifications
You must be signed in to change notification settings - Fork 0
/
newLCDForAdjlist.py
170 lines (115 loc) · 4.08 KB
/
newLCDForAdjlist.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 21 16:13:46 2019
@author: georgiabaltsou
2016-Zhou-Local Community Detection Algorithm Based on Minimal Cluster
Edited for weighted graphs
keep the closer node but if there are more than 1 I look for the max weighted edge.
"""
import networkx as nx
import numpy as np
import csv
import os
import time
#find the neighbors of node u
def findNeighboorOfu(G,u):
return list((G.neighbors(u)))
#find the neighbors of community C
def findNeighboorOfC(G, C):
neighbors = []
neighborsOfC = []
for j in C:
for i in G.neighbors(j):
neighbors.append(i)
neighborsOfC = np.unique(neighbors)
return neighborsOfC
#find the minimal cluster containing the initial node and the closer neighbors of it
def minimalCluster(G, s, Gdict):
neighbors = []
maxNumber = 0
maxWeight = 0
global node
for u in findNeighboorOfu(G,s):
commonNeighbors = sorted(nx.common_neighbors(G, s, u))
if (len(commonNeighbors) > maxNumber):
maxNumber = len(commonNeighbors)
wmax = G.get_edge_data(s, u)
maxWeight = wmax['weight']
neighbors = commonNeighbors
neighbors.append(s)
neighbors.append(u)
node = u
elif (len(commonNeighbors) == maxNumber):
wcur = G.get_edge_data(s, u)
curWeight = wcur['weight']
if(curWeight > maxWeight):
maxNumber = len(commonNeighbors)
maxWeight = curWeight
neighbors = commonNeighbors
neighbors.append(s)
neighbors.append(u)
node = u
minCluster = np.unique(neighbors)
return minCluster
#calculation of local modularity M
def findM(G, LC, Gdict):
#cut
cut = nx.cut_size(G, LC, weight='weight')
#volume
vol = nx.cuts.volume(G, LC, weight='weight')
M = (vol - cut) / (2*cut)
return M
###### main program #######
def newLCD(seedsetFile, myFile, G, Gdict, method,l):
start_time = time.time()
alg = 'newLCD'
seedFile = open(seedsetFile, 'r')
seeds = seedFile.readline().rstrip('\n').split(" ")
lenS = len(seeds)
LCInitial = []
# initialize seed list
global s
s = []
for i in range(lenS):
LCInitial.append(seeds[i])
s.append(seeds[i])
# consider as LC the near to seed nodes, but if |seed| != 1 then LC=seed
if lenS == 1:
LC = minimalCluster(G, seeds[0], Gdict)
else:
LC = LCInitial
NLC = {}
NLCkeys = findNeighboorOfC(G, LC)
for i in NLCkeys:
NLC[i] = 0
previousNLC = {}
while (NLC != previousNLC and len(LC)<l):
tmpLC = list(LC)
previousNLC = NLC
curmax = 0
NLCscores = {}
for u in NLC:
tmpLC.append(u)
curM = findM(G, tmpLC, Gdict)
if curM > curmax:
NLCscores.clear()
NLCscores[u] = findM(G, tmpLC, Gdict)
curmax = curM
tmpLC.remove(u)
maximum = max(NLCscores, key=NLCscores.get)
if (type(LC) != list):
LC = LC.tolist()
if(maximum not in LC):
LC.append(maximum)
ΝLCtmp = findNeighboorOfC(G, LC)
NLCNew = np.setdiff1d(ΝLCtmp, LC)
for j in NLCNew:
NLC[j] = 0
print("NewLCD time: ", time.time() - start_time)
with open('./communities/'+str(myFile)+'_communities.csv', 'a') as out_file:
writer = csv.writer(out_file, delimiter=';')
if os.stat('./communities/'+str(myFile)+'_communities.csv').st_size == 0:
writer.writerow(["Algorithm", "Seed node", "Method", "Community"])
row = [alg] + ["\n".join(seeds)]+[method] + list(LC)
writer.writerow(row)