Louvain Algorithm for Community Detection

Node: open the accordion below for a detailed explanation of the algorithm's implementation

Modularity Update

The change in modularity by moving one node to the community of one of its neighbors is computed as follows. We first remove node \(i\) from its community \(D\) and then insert this node into community \(C\).

We first introduce some notation:

The update of modularity is given by

\[ \Delta Q = Q_{after} - Q_{before} \]

where

\[ Q_{before} = \frac{\Sigma_{C,in}}{2m} - \left(\frac{\Sigma_{C,tot}}{2m}\right)^2 + \frac{\Sigma_{D,in}}{2m} - \left(\frac{\Sigma_{D,tot}}{2m}\right)^2 \]

and

\[ \begin{align} Q_{after} =&\; \frac{\Sigma_{C,in} + 2 k_i(i \rightarrow C) + l_i}{2m} - \left(\frac{\Sigma_{C,tot} + k_i}{2m}\right)^2 \\ &\; + \frac{\Sigma_{D,in} - 2 k_i(i \rightarrow D) - l_i}{2m} - \left(\frac{\Sigma_{D,tot} - k_i}{2m}\right)^2 \end{align} \]

After some algebraic manipulations, we obtain

\[ \Delta Q = \frac{k_i(i \rightarrow C) - k_i(i \rightarrow D)}{m} + \frac{k_i}{2m} \left(\frac{\Sigma_{C,tot} - \Sigma_{D,tot} + k_i}{2m}\right) \]

The Louvain algorithm is an agglomerative method that is widely used due to its performance (aka it is fast). It consists of two phases:

Repeat until no further increase in modularity can be obtained.

Phase 1

Initialization

For each node:

Repeat this process until no gain in modularity can be measured.

Remarks

  • Each node will be processed many times.
  • The algorithm's output depends on the order in which the nodes are processed.
  • Computation time depends on the order of the nodes.
  • Modularity

    The change in modularity is given by

    \[ \Delta Q = \left[\frac{\Sigma_{in} + 2 k_{i,in}}{am} - \left(\frac{\Sigma_{tot} +k_i}{2m}\right)^2 \right] - \left[\frac{\Sigma_{in}}{2m} - \left(\frac{\Sigma_{tot}}{2m}\right)^2 - \left(\frac{k_i}{2m}\right)^2\right] \]

    where

    Phase 2

    Continue iteratively until \(\Delta Q \leq 0\). In each iteration, a new network is being created from the communities found in the previous iteration.



    The JavaScript Code