Controlling the size of Louvain-generated communities?

Is there a way for me to run the following query in a way that forces communities to contain a certain number of nodes?

CALL gds.louvain.stream('myGraph', { relationshipWeightProperty: 'Weight',
includeIntermediateCommunities: True})
YIELD nodeId, communityId, intermediateCommunityIds
RETURN gds.util.asNode(nodeId).productId AS id, communityId, intermediateCommunityIds
ORDER BY communityId ASC

No, you can't specify the number of nodes in Louvain - it partitions the graph to maximize modularity (basically how densely nodes are connected within a community vs between communities).


With Louvain, we recommend using a weight property (which you have!), and considering the intermediate communities (if they're smaller). Otherwise, you can try running Louvain again on particularly large communities from your first result, to break them down into smaller subsets.


Another option is to try a different community detection algorithm like label propagation, speaker listener label propagation (which is ideal for overlapping communities), or strongly connected components.

1 Like