Our algorithm is divided into two phases that are repeated iteratively.
Assume that we start with a weighted network of N nodes. First, we
assign a different community to each node of the network. So, in this
initial partition there are as many communities as there are nodes.
Then, for each node i we consider the neighbours j of i and we evaluate
the gain of modularity that would take place by removing i from its
community and by placing it in the community of j. The node i is then
placed in the community for which this gain is maximum (in the case of
a tie we use a breaking rule), but only if this gain is positive. If
no positive gain is possible, i stays in its original community. This
process is applied repeatedly and sequentially for all nodes until
no further improvement can be achieved and the first phase is then
complete.
Reference:
Blondel, Vincent D; Guillaume, Jean-Loup; Lambiotte, Renaud; Lefebvre, Etienne (9 October 2008). "Fast unfolding of communities in large networks". Journal of Statistical Mechanics: Theory and Experiment. 2008 (10): P10008. arXiv:0803.0476 Freely accessible. doi:10.1088/1742-5468/2008/10/P10008.