-
Notifications
You must be signed in to change notification settings - Fork 5
/
cluster.js
79 lines (64 loc) · 2.16 KB
/
cluster.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
var graph = require('./lib/graph');
var DEFAULT_SEARCH_DEPTH = 1000;
module.exports = function(set, similarity) {
function checkedSim(x, y) {
var sim = similarity(x, y);
if (typeof sim != 'number' ||
sim < 0)
throw new Error('Similarity function did not yield a number in the range [0, +Inf) when comparing ' + x + ' to ' + y + ' : ' + sim);
return sim;
}
var g = graph.create(set, checkedSim);
function stripNodes(realFunction) {
return function(/* args */) {
var nodes = realFunction.apply(this, Array.prototype.slice.call(arguments));
return nodes.map(graph.data);
}
}
function stripGraphs(realFunction) {
return function(/* args */) {
var graphs = realFunction.apply(this, Array.prototype.slice.call(arguments));
return graphs.map(function(g) {
return g.nodes.map(graph.data);
});
}
}
function groups(numGroups, searchDepth_) {
var searchDepth = searchDepth_ || DEFAULT_SEARCH_DEPTH;
var subGraphs = graph.divide(g, numGroups, searchDepth);
return subGraphs;
}
function representatives(numGroups, searchDepth_) {
var subGraphs = groups(numGroups, searchDepth_);
// Cut down to required size by removing the smallest groups.
subGraphs.sort(function(x, y) {
return y.nodes.length - x.nodes.length;
});
subGraphs.splice(numGroups);
var roots = subGraphs.map(graph.findCenter);
return roots;
}
function similarGroups(similarityIndex) {
var subGraphs = graph.connected(g, similarityIndex);
return subGraphs;
}
function evenGroups(numGroups, searchDepth_) {
var roots = representatives(numGroups);
var divisions = graph.growFromNuclei(g, roots);
var groups = divisions.graphs.map(function(g) {
return g.nodes.map(graph.data);
});
while (divisions.orphans.length) {
var o = graph.data(divisions.orphans.pop());
groups.sort(function(x, y) { return x.length - y.length; });
groups[0].push(o);
}
return groups;
}
return {
groups: stripGraphs(groups),
representatives: stripNodes(representatives),
similarGroups: stripGraphs(similarGroups),
evenGroups: evenGroups
};
}