-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlouvain.min.js
1 lines (1 loc) · 3.68 KB
/
louvain.min.js
1
var Graph=(function(){function Graph(c,a){this._nodes={};for(var b=0;b<c.length;b++){this._nodes[c[b]]={}}for(var b=0;b<a.length;b++){this._nodes[a[b].a][a[b].b]={weight:a[b].score};this._nodes[a[b].b][a[b].a]={weight:a[b].score}}}Graph.prototype.copy=function(a){for(node in this._nodes){a.add_node(node);for(nb in this._nodes[node]){if(!a.get_node(nb)){a.add_node(nb)}a.add_edge(node,nb,this._nodes[node][nb].weight)}}};Graph.prototype.clear=function(){this._nodes={}};Graph.prototype.add_node=function(a){if(!this._nodes[a]){this._nodes[a]={}}};Graph.prototype.add_edge=function(d,c,e){if(this._nodes[d]===undefined){this.add_node(d)}if(this._nodes[c]===undefined){this.add_node(c)}this._nodes[d][c]=this._nodes[c][d]={weight:e}};Graph.prototype.remove_node=function(a){for(n in this._nodes){delete this._nodes[n][a]}delete this._nodes[a]};Graph.prototype.remove_edge=function(d,c){delete this._nodes[d][c];delete this._nodes[c][d]};Graph.prototype.get_edge=function(d,c){if(this._nodes[d]!==undefined){return this._nodes[d][c]}else{return undefined}};Graph.prototype.get_node=function(a){return this._nodes[a]};Graph.prototype.get_nodes=function(){return this._nodes};Graph.prototype.get_edges=function(){var a=[];var b={};for(node in this._nodes){for(nb in this._nodes[node]){if(!b[nb+node]){a.push({a:node,b:nb,data:this._nodes[node][nb]});b[node+nb]=true}}}return a};Graph.prototype.node_count=function(){return this.get_nodes().length};Graph.prototype.edge_count=function(){return this.get_edges().length};Graph.prototype.degree=function(c,a){var b=0;for(nb in this._nodes[c]){var d=a?this._nodes[c][nb].weight:1;b+=(nb===c)?d*2:d}return b};return Graph})();var louvain=function(){var l={};var t=0;var c={};var q={};var e={};var p={};var i=-1;var h=1e-7;var b=function(w,x){var u={};for(keys in w[0]){u[keys]=w[0][keys]}for(var v=1;v<x+1;v++){for(node in u){u[node]=w[v][u[node]]}}return u};var s=function(v){var u=m(v);return b(u,u.length-1)};var m=function(C){if(C.edge_count()==0){var w={};var u=C.get_nodes();var x=0;for(node in u){w[node]=x++}return[w]}var z=new Graph([],[]);C.copy(z);d(z);var A=k();var B=[];a(z);var v=k();var y=g(l);B.push(y);A=v;z=f(y,z);d(z);while(true){a(z);v=k();if(v-A<h){break}y=g(l);B.push(y);A=v;z=f(y,z);d(z)}return B};var f=function(B,D){var C=new Graph([],[]);for(key in B){C.add_node(B[key])}var x=D.get_edges();for(var y=0;y<x.length;y++){var z=x[y].data.weight;var v=B[x[y].a];var u=B[x[y].b];var A=C.get_edge(v,u);var w=A?A.weight:0;C.add_edge(v,u,w+z)}return C};var g=function(z){var x=0;var u={};var w={};for(key in z){var y=z[key];var v=w[y]===undefined?-1:w[y];if(v==-1){w[y]=x;v=x;x=x+1}u[key]=v}return u};var a=function(E){var x=true;var z=0;var A=k();var w=A;while(x&&z!=i){A=w;x=false;z++;var u=E.get_nodes();for(node in u){var D=l[node];var y=(q[node]||0)/(t*2);var C=j(node,E);o(node,D,C[D]||0);var B=D;var F=0;for(com in C){var v=C[com]-(c[com]||0)*y;if(v>F){F=v;B=com}}r(node,B,(C[B]||0));if(B!=D){x=true}}w=k();if(w-A<h){break}}};var j=function(w,y){var v={};var z=y.get_node(w);for(nb in z){if(nb!=w){var x=z[nb].weight;var u=l[nb];v[u]=(v[u]||0)+x}}return v};var o=function(v,u,w){c[u]=(c[u]||0)-(q[v]||0);e[u]=(e[u]||0)-w-(p[v]||0);l[v]=-1};var r=function(v,u,w){l[v]=u;c[u]=(c[u]||0)+(q[v]||0);e[u]=(e[u]||0)+w+(p[v]||0)};var k=function(){var v=t;var u=0;var y={};for(key in l){y[l[key]]={}}for(community in y){var x=e[community]||0;var w=c[community]||0;if(v>0){u=u+x/v-(Math.pow(w/(2*v),2))}}return u};var d=function(y){l={};t=0;c={};q={};e={};var x=0;var A=y.get_nodes();var z=y.get_edges();for(var v=0;v<z.length;v++){t+=z[v].data.weight}for(node in A){l[node]=x;var w=y.degree(node,true);c[x]=w;q[node]=w;var u=y.get_edge(node,node);p[node]=u?u.weight:0;e[x]=p[node];x++}};return{best_communities:s}}();