The Louvain algorithm aims at maximizing the modularity, which acts as the loss function. Starting from a graph where each node is assigned to its own community (Q=0), the algorithm will try to move nodes to the community of their neighbors, and keep this configuration only if it makes the modularity increase.
The algorithm performs two steps at each iteration. During the first one, an iteration over all nodes is performed. For each node n and each of its neighbors k, the algorithm tries to move the node n to the same community as k. The node n is moved to the community that leads to the highest increase in modularity. If it is not possible to increase the modularity with such an operation, the community of node n is left unchanged for this iteration.
In the second step, the algorithm will group together nodes belonging to the same community in order to create new nodes, and sum the weights of the inter-community edges in order to create the new...