Advertisement
Guest User

Untitled

a guest
May 25th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. var modularity = require('../../');//require('ngraph.louvain');
  2. var coarsen = require('ngraph.coarsen');
  3.  
  4. module.exports = function (graph, maxDepth) {
  5. var clusters = modularity(graph);
  6.  
  7. var clustersByLevel = [];
  8.  
  9. maxDepth = maxDepth || 5;
  10.  
  11. var getNodesByCluster = function (graph, clusters) {
  12. var byCluster = {};
  13.  
  14. var nodeCnt = 0;
  15. graph.forEachNode(function (node) {
  16. var cls = clusters.getClass(node.id);
  17. if (!byCluster[cls]) byCluster[cls] = [];
  18. byCluster[cls].push(node.id);
  19. nodeCnt++;
  20. });
  21.  
  22. clustersByLevel.push(byCluster);
  23. };
  24.  
  25. var clusterAgain = function (clusters) {
  26. graph = coarsen(graph, clusters);
  27. clusters = modularity(graph);
  28.  
  29. getNodesByCluster(graph, clusters);
  30. return clusters;
  31. };
  32.  
  33. var depth = 0;
  34. getNodesByCluster(graph, clusters);
  35. while (clusters.canCoarse() && depth++ < (maxDepth - 1)) {
  36. clusters = clusterAgain(clusters);
  37. }
  38.  
  39. var clusterIdsByNode = {};
  40.  
  41. var explode = function (level, cid, parentIds) {
  42. var arr = clustersByLevel[level][cid];
  43. if (level === 0) {
  44. arr.forEach(function (nodeId, i) {
  45. clusterIdsByNode[nodeId] = parentIds;
  46. });
  47. } else {
  48. arr.forEach(function (nextId, i) {
  49. var pids = parentIds.slice(0);//clone
  50. pids.push(nextId);
  51. explode(level - 1, nextId, pids);
  52. });
  53. }
  54. };
  55.  
  56. Object.keys(clustersByLevel[clustersByLevel.length - 1]).forEach(function (cid) {
  57. explode(clustersByLevel.length - 1, cid, [cid]);
  58. });
  59.  
  60. return {
  61. getClass: function (nodeId) {
  62. if (!clusterIdsByNode[nodeId]) return null;
  63. return clusterIdsByNode[nodeId][0];
  64. }
  65. };
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement