// File: community.h // -- community detection source file //----------------------------------------------------------------------------- // Community detection // Based on the article "Fast unfolding of community hierarchies in large networks" // Copyright (C) 2008 V. Blondel, J.-L. Guillaume, R. Lambiotte, E. Lefebvre // // This program must not be distributed without agreement of the above mentionned authors. //----------------------------------------------------------------------------- // Author : E. Lefebvre, adapted by J.-L. Guillaume // Email : jean-loup.guillaume@lip6.fr // Location : Paris, France // Time : February 2008 //----------------------------------------------------------------------------- // see readme.txt for more details #include "community.h" using namespace std; Community::Community(char * filename, char * filename_w, int type, int nbp, double minm) { g = Graph(filename, filename_w, type); size = g.nb_nodes; neigh_weight.resize(size,-1); neigh_pos.resize(size); neigh_last=0; n2c.resize(size); in.resize(size); tot.resize(size); for (int i=0 ; i> node >> comm; if (finput) { int old_comm = n2c[node]; neigh_comm(node); remove(node, old_comm, neigh_weight[old_comm]); unsigned int i=0; for ( i=0 ; i=0 && node=0 && node0) q += (double)in[i]/m2 - ((double)tot[i]/m2)*((double)tot[i]/m2); } return q; } void Community::neigh_comm(unsigned int node) { for (unsigned int i=0 ; i::iterator, vector::iterator> p = g.neighbors(node); unsigned int deg = g.nb_neighbors(node); neigh_pos[0]=n2c[node]; neigh_weight[neigh_pos[0]]=0; neigh_last=1; for (unsigned int i=0 ; i renumber(size, -1); for (int node=0 ; node::iterator, vector::iterator> p = g.neighbors(i); int deg = g.nb_neighbors(i); for (int j=0 ; j renumber(size, -1); for (int node=0 ; node renumber(size, -1); for (int node=0 ; node > comm_nodes(final); for (int node=0 ; node m; map::iterator it; int comm_size = comm_nodes[comm].size(); for (int node=0 ; node::iterator, vector::iterator> p = g.neighbors(comm_nodes[comm][node]); int deg = g.nb_neighbors(comm_nodes[comm][node]); for (int i=0 ; isecond+=neigh_weight; } } g2.degrees[comm]=(comm==0)?m.size():g2.degrees[comm-1]+m.size(); g2.nb_links+=m.size(); for (it = m.begin() ; it!=m.end() ; it++) { g2.total_weight += it->second; g2.links.push_back(it->first); g2.weights.push_back(it->second); } } return g2; } bool Community::one_level() { bool improvement=false ; int nb_moves; int nb_pass_done = 0; double new_mod = modularity(); double cur_mod = new_mod; vector random_order(size); for (int i=0 ; ibest_increase) { best_comm = neigh_pos[i]; best_nblinks = neigh_weight[neigh_pos[i]]; best_increase = increase; } } // insert node in the nearest community insert(node, best_comm, best_nblinks); if (best_comm!=node_comm) nb_moves++; } double total_tot=0; double total_in=0; for (unsigned int i=0 ; i0) improvement=true; } while (nb_moves>0 && new_mod-cur_mod>min_modularity); return improvement; }