-
Notifications
You must be signed in to change notification settings - Fork 0
/
louvain.js
287 lines (253 loc) · 8.7 KB
/
louvain.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Graphology Louvain Algorithm
* ============================
* NOTES :
* ~ 'altered' set heuristic
* A set of altered communities is stored and used at each iteration of the phase 1.
* Indeed, every time a movement is made from C1 to C2
* Then for the next iteration through every node,
* each movement from a not-altered to another not-altered community is pointless to check
* because the ∆Q would be the same (negative movement then)
* A old set is used to store the altered comm. from the previous phase 1 iteration
* A new set is used to store the altered comm. of the current phase 1 iteration
* A flag is used to handle the first phase-1 iteration
* ~ ...
*/
var defaults = require('lodash/defaultsDeep'),
isGraph = require('graphology-utils/is-graph');
var DEFAULTS = {
attributes: {
weight: 'weight',
community: 'community',
},
};
/**
* Function returning
* an object mapping the respective community to each node
*
* @param {Boolean} assign - mutate the node attributes directly if true
* @param {Graph} graph - Target graph.
* @param {Object} options - Object of differents execution optinos
* @return Object
*/
function louvain(assign, graph, options) {
if (!isGraph(graph))
throw new Error('graphology-louvain: the given graph is not a valid graphology instance.');
if (graph.multi)
throw new Error('graphology-louvain: MultiGraph are not handled');
if (!graph.size)
throw new Error('graphology-louvain: the graph has no edges');
// Attributes name
options = defaults({}, options, DEFAULTS);
var nodes = graph.nodes(),
edges,
dendogram = {};
// Pass variables
var pgraph = graph,
bgraph,
M,
belongings,
indegree,
outdegree,
altered,
enhancingPass,
possessions,
w, weight, weights;
// Phase 1 variables
var bufferDQ,
deltaQ,
moveMade,
neighbors,
nextCommunity,
between,
oldc, newc,
stack,
visited;
// Iterations variables
var i, l1,
j, l2,
k, l3,
keys,
node, node2, edge, edge2, bounds,
community, community2;
for (i = 0, l1 = nodes.length; i < l1; i++)
dendogram[nodes[i]] = [nodes[i]];
/**
* Starting passes
* ***************
*/
do {
// Pass initialization
enhancingPass = false;
nodes = pgraph.nodes();
edges = pgraph.edges();
M = 0;
belongings = {};
possessions = {};
weights = {};
indegree = {};
outdegree = {};
altered = {prev: {}, curr: {}, flag: false}; // see top notes
for (i = 0, l1 = nodes.length; i < l1; i++) {
node = nodes[i];
belongings[node] = node;
possessions[node] = {};
possessions[node][node] = true;
indegree[node] = 0;
outdegree[node] = 0;
}
for (i = 0, l1 = edges.length; i < l1; i++) {
edge = edges[i];
bounds = pgraph.extremities(edge);
w = pgraph.getEdgeAttribute(edge, options.attributes.weight);
weight = isNaN(w) ? 1 : w;
weights[edge] = weight;
outdegree[bounds[0]] += weight;
indegree[bounds[1]] += weight;
if (pgraph.undirected(edge) && bounds[0] !== bounds[1]) {
indegree[bounds[0]] += weight;
outdegree[bounds[1]] += weight;
M += 2 * weight;
}
else
M += weight;
}
/**
* Phase 1 :
* For each node, it looks for the best move to one if its neighbors' community
* and it does the best one - according to the modularity addition value
*
* After every node has been visited and each respective move - or not - has been done,
* it iterates again until no enhancing move has been done through any node
* -------------------------------------------------------------------------------------
*/
do {
moveMade = false;
// see top notes
altered.prev = altered.curr;
altered.curr = {};
for (i = 0, l1 = nodes.length; i < l1; i++) {
node = nodes[i];
community = belongings[node];
deltaQ = 0;
bufferDQ = 0;
visited = {};
visited[community] = true;
between = {old: 0, new: 0};
oldc = {in: 0, out: 0};
// Computing current community values
stack = Object.keys(possessions[community]);
for (j = 0, l2 = stack.length; j < l2; j++) {
node2 = stack[j];
if (node !== node2) {
oldc.in += indegree[node2];
oldc.out += outdegree[node2];
between.old += weights[pgraph.getEdge(node, node2)] || 0;
between.old += weights[pgraph.getEdge(node2, node)] || 0;
}
}
// Iterating through neighbors
neighbors = pgraph.neighbors(node);
for (j = 0, l2 = neighbors.length; j < l2; j++) {
community2 = belongings[neighbors[j]];
if (visited[community2])
continue;
visited[community2] = true;
// see top notes
if (altered.flag && !altered.prev[community] && !altered.prev[community2])
continue;
between.new = 0;
newc = {in: 0, out: 0};
stack = Object.keys(possessions[community2]);
for (k = 0, l3 = stack.length; k < l3; k++) {
node2 = stack[k];
newc.in += indegree[node2];
newc.out += outdegree[node2];
between.new += weights[pgraph.getEdge(node, node2)] || 0;
between.new += weights[pgraph.getEdge(node2, node)] || 0;
}
deltaQ = (between.new - between.old) / M;
deltaQ += indegree[node] * (oldc.out - newc.out) / (M * M);
deltaQ += outdegree[node] * (oldc.in - newc.in) / (M * M);
if (deltaQ > bufferDQ) {
bufferDQ = deltaQ;
nextCommunity = community2;
}
}
// If a positive mode has been found
if (bufferDQ > 0) {
moveMade = true;
enhancingPass = true;
altered.curr[community] = true; // see top notes
altered.curr[nextCommunity] = true;
delete possessions[community][node];
if (Object.keys(possessions[community]).length === 0)
delete possessions[community];
belongings[node] = nextCommunity;
possessions[nextCommunity][node] = node;
}
}
altered.flag = true; // SEE NOTES AT THE TOP
} while (moveMade);
/**
* Phase 2 :
* If a move has been made, we create a new graph,
* nodes being communities and edges the links betweem them
* -------------------------------------------------------------------------------------
*/
if (enhancingPass) {
bgraph = pgraph.emptyCopy();
// Adding the nodes
keys = Object.keys(possessions);
for (i = 0, l1 = keys.length; i < l1; i++)
bgraph.addNode(keys[i]);
// Adding the edges
for (i = 0, l1 = edges.length; i < l1; i++) {
edge = edges[i];
bounds = pgraph.extremities(edge);
community = belongings[bounds[0]];
community2 = belongings[bounds[1]];
w = weights[edge];
edge2 = bgraph.getDirectedEdge(community, community2);
if (edge2 === undefined)
bgraph.addDirectedEdge(community, community2, {weight: w});
else {
weight = bgraph.getEdgeAttribute(edge2, options.attributes.weight);
bgraph.setEdgeAttribute(edge2, options.attributes.weight, weight + w);
}
if (pgraph.undirected(edge) && bounds[0] !== bounds[1]) {
edge2 = bgraph.getDirectedEdge(community2, community);
if (edge2 === undefined)
bgraph.addDirectedEdge(community2, community, {weight: w});
else {
weight = bgraph.getEdgeAttribute(edge2, options.attributes.weight);
bgraph.setEdgeAttribute(edge2, options.attributes.weight, weight + w);
}
}
}
// Updating the dendogram
nodes = Object.keys(dendogram);
for (i = 0, l1 = nodes.length; i < l1; i++) {
node = nodes[i];
community = belongings[dendogram[node][dendogram[node].length - 1]];
dendogram[node].push(community);
}
// Now using the new graph
pgraph = bgraph;
}
} while (enhancingPass);
nodes = Object.keys(dendogram);
// Assigning
if (assign)
for (i = 0, l1 = nodes.length; i < l1; i ++) {
node = nodes[i];
graph.setNodeAttribute(node, options.attributes.community, dendogram[node][dendogram[node].length - 1]);
}
// Standard case ; getting the final partitions from the dendogram
for (node in dendogram)
dendogram[node] = dendogram[node][dendogram[node].length - 1];
return dendogram;
}
var fn = louvain.bind(null, false);
fn.assign = louvain.bind(null, true);
module.exports = fn;