-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
56 lines (44 loc) · 1.38 KB
/
index.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
var createCommunityGraph = require('./lib/createCommunityGraph.js');
var createCommunity = require('./lib/createCommunity.js');
module.exports = modularity;
function modularity(ngraph, options) {
var graph = createCommunityGraph(ngraph);
var community = createCommunity(graph, options);
var originalModularity = community.modularity();
var modularityImproved = community.optimizeModularity();
var newModularity = community.modularity();
return {
canCoarse: canCoarse,
originalModularity: originalModularity,
newModularity: newModularity,
/**
* Given original node id returns its class id (community)
*/
getClass: getClass,
/**
* Returns a map from community id to array of neighbor ids.
*/
getAllCommunities: getAllCommunities
};
function canCoarse() {
// If there was movement last turn - we can coarse graph further.
return modularityImproved;
}
function getClass(nodeId) {
var node = graph.getNodeIdFromNgraph(nodeId);
return community.getClass(node);
}
function getAllCommunities() {
var communities = new Map();
ngraph.forEachNode(function(node) {
var classId = getClass(node.id);
let neighbors = communities.get(classId);
if (!neighbors) {
neighbors = [];
communities.set(classId, neighbors);
}
neighbors.push(node.id);
});
return communities;
}
}