Louvain
Glossary
 Directed

Directed trait. The algorithm is welldefined on a directed graph.
 Directed

Directed trait. The algorithm ignores the direction of the graph.
 Directed

Directed trait. The algorithm does not run on a directed graph.
 Undirected

Undirected trait. The algorithm is welldefined on an undirected graph.
 Undirected

Undirected trait. The algorithm ignores the undirectedness of the graph.
 Heterogeneous nodes

Heterogeneous nodes fully supported. The algorithm has the ability to distinguish between nodes of different types.
 Heterogeneous nodes

Heterogeneous nodes allowed. The algorithm treats all selected nodes similarly regardless of their label.
 Heterogeneous relationships

Heterogeneous relationships fully supported. The algorithm has the ability to distinguish between relationships of different types.
 Heterogeneous relationships

Heterogeneous relationships allowed. The algorithm treats all selected relationships similarly regardless of their type.
 Weighted relationships

Weighted trait. The algorithm supports a relationship property to be used as weight, specified via the relationshipWeightProperty configuration parameter.
 Weighted relationships

Weighted trait. The algorithm treats each relationship as equally important, discarding the value of any relationship weight.
Introduction
The Louvain method is an algorithm to detect communities in large networks. It maximizes a modularity score for each community, where the modularity quantifies the quality of an assignment of nodes to communities. This means evaluating how much more densely connected the nodes within a community are, compared to how connected they would be in a random network.
The Louvain algorithm is a hierarchical clustering algorithm, that recursively merges communities into a single node and executes the modularity clustering on the condensed graphs.
For more information on this algorithm, see:
Running this algorithm requires sufficient memory availability. Before running this algorithm, we recommend that you read Memory Estimation. 
Syntax
This section covers the syntax used to execute the Louvain algorithm in each of its execution modes. We are describing the named graph variant of the syntax. To learn more about general syntax variants, see Syntax overview.
CALL gds.louvain.stream(
graphName: String,
configuration: Map
)
YIELD
nodeId: Integer,
communityId: Integer,
intermediateCommunityIds: List of Integer
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. Nodes with any of the given labels will be included. 

List of String 

yes 
Filter the named graph using the given relationship types. Relationships with any of the given types will be included. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

yes 
An ID that can be provided to more easily track the algorithm’s progress. 

Boolean 

yes 
If disabled the progress percentage will not be logged. 

String 

yes 
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. 

String 

yes 
Used to set the initial community for a node. The property value needs to be a nonnegative number. 

maxLevels 
Integer 

yes 
The maximum number of levels in which the graph is clustered and then condensed. 
Integer 

yes 
The maximum number of iterations that the modularity optimization will run for each level. 

Float 

yes 
Minimum change in modularity between iterations. If the modularity changes less than the tolerance value, the result is considered stable and the algorithm returns. 

includeIntermediateCommunities 
Boolean 

yes 
Indicates whether to write intermediate communities. If set to false, only the final community is persisted. 
consecutiveIds 
Boolean 

yes 
Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). Cannot be used in combination with the 
minCommunitySize 
Integer 

yes 
Only nodes inside communities larger or equal the given value are returned. 
Name  Type  Description 

nodeId 
Integer 
Node ID. 
communityId 
Integer 
The community ID of the final level. 
intermediateCommunityIds 
List of Integer 
Community IDs for each level. 
CALL gds.louvain.stats(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
communityCount: Integer,
ranLevels: Integer,
modularity: Float,
modularities: List of Float,
communityDistribution: Map,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. Nodes with any of the given labels will be included. 

List of String 

yes 
Filter the named graph using the given relationship types. Relationships with any of the given types will be included. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

yes 
An ID that can be provided to more easily track the algorithm’s progress. 

Boolean 

yes 
If disabled the progress percentage will not be logged. 

String 

yes 
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. 

String 

yes 
Used to set the initial community for a node. The property value needs to be a nonnegative number. 

maxLevels 
Integer 

yes 
The maximum number of levels in which the graph is clustered and then condensed. 
Integer 

yes 
The maximum number of iterations that the modularity optimization will run for each level. 

Float 

yes 
Minimum change in modularity between iterations. If the modularity changes less than the tolerance value, the result is considered stable and the algorithm returns. 

includeIntermediateCommunities 
Boolean 

yes 
Indicates whether to write intermediate communities. If set to false, only the final community is persisted. 
consecutiveIds 
Boolean 

yes 
Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). Cannot be used in combination with the 
Name  Type  Description 

preProcessingMillis 
Integer 
Milliseconds for preprocessing the data. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
postProcessingMillis 
Integer 
Milliseconds for computing percentiles and community count. 
communityCount 
Integer 
The number of communities found. 
ranLevels 
Integer 
The number of supersteps the algorithm actually ran. 
modularity 
Float 
The final modularity score. 
modularities 
List of Float 
The modularity scores for each level. 
communityDistribution 
Map 
Map containing min, max, mean as well as p1, p5, p10, p25, p50, p75, p90, p95, p99 and p999 percentile values of community size for the last level. 
configuration 
Map 
The configuration used for running the algorithm. 
CALL gds.louvain.mutate(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
mutateMillis: Integer,
postProcessingMillis: Integer,
communityCount: Integer,
ranLevels: Integer,
modularity: Float,
modularities: List of Float,
nodePropertiesWritten: Integer,
communityDistribution: Map,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

mutateProperty 
String 

no 
The node property in the GDS graph to which the community ID is written. 
List of String 

yes 
Filter the named graph using the given node labels. 

List of String 

yes 
Filter the named graph using the given relationship types. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

yes 
An ID that can be provided to more easily track the algorithm’s progress. 

String 

yes 
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. 

String 

yes 
Used to set the initial community for a node. The property value needs to be a nonnegative number. 

maxLevels 
Integer 

yes 
The maximum number of levels in which the graph is clustered and then condensed. 
Integer 

yes 
The maximum number of iterations that the modularity optimization will run for each level. 

Float 

yes 
Minimum change in modularity between iterations. If the modularity changes less than the tolerance value, the result is considered stable and the algorithm returns. 

includeIntermediateCommunities 
Boolean 

yes 
Indicates whether to write intermediate communities. If set to false, only the final community is persisted. 
consecutiveIds 
Boolean 

yes 
Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). Cannot be used in combination with the 
Name  Type  Description 

preProcessingMillis 
Integer 
Milliseconds for preprocessing the data. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
mutateMillis 
Integer 
Milliseconds for adding properties to the projected graph. 
postProcessingMillis 
Integer 
Milliseconds for computing percentiles and community count. 
communityCount 
Integer 
The number of communities found. 
ranLevels 
Integer 
The number of supersteps the algorithm actually ran. 
modularity 
Float 
The final modularity score. 
modularities 
List of Float 
The modularity scores for each level. 
nodePropertiesWritten 
Integer 
Number of properties added to the projected graph. 
communityDistribution 
Map 
Map containing min, max, mean as well as p1, p5, p10, p25, p50, p75, p90, p95, p99 and p999 percentile values of community size for the last level. 
configuration 
Map 
The configuration used for running the algorithm. 
CALL gds.louvain.write(
graphName: String,
configuration: Map
)
YIELD
preProcessingMillis: Integer,
computeMillis: Integer,
writeMillis: Integer,
postProcessingMillis: Integer,
nodePropertiesWritten: Integer,
communityCount: Integer,
ranLevels: Integer,
modularity: Float,
modularities: List of Float,
communityDistribution: Map,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String 

no 
The name of a graph stored in the catalog. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. 
Name  Type  Default  Optional  Description 

List of String 

yes 
Filter the named graph using the given node labels. Nodes with any of the given labels will be included. 

List of String 

yes 
Filter the named graph using the given relationship types. Relationships with any of the given types will be included. 

Integer 

yes 
The number of concurrent threads used for running the algorithm. 

String 

yes 
An ID that can be provided to more easily track the algorithm’s progress. 

Boolean 

yes 
If disabled the progress percentage will not be logged. 

Integer 

yes 
The number of concurrent threads used for writing the result to Neo4j. 

String 

no 
The node property in the Neo4j database to which the community ID is written. 

String 

yes 
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. 

String 

yes 
Used to set the initial community for a node. The property value needs to be a nonnegative number. 

maxLevels 
Integer 

yes 
The maximum number of levels in which the graph is clustered and then condensed. 
Integer 

yes 
The maximum number of iterations that the modularity optimization will run for each level. 

Float 

yes 
Minimum change in modularity between iterations. If the modularity changes less than the tolerance value, the result is considered stable and the algorithm returns. 

includeIntermediateCommunities 
Boolean 

yes 
Indicates whether to write intermediate communities. If set to false, only the final community is persisted. 
consecutiveIds 
Boolean 

yes 
Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). Cannot be used in combination with the 
minCommunitySize 
Integer 

yes 
Only community ids of communities with a size greater than or equal to the given value are written to Neo4j. 
Name  Type  Description 

preProcessingMillis 
Integer 
Milliseconds for preprocessing the data. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
writeMillis 
Integer 
Milliseconds for writing result data back. 
postProcessingMillis 
Integer 
Milliseconds for computing percentiles and community count. 
nodePropertiesWritten 
Integer 
The number of node properties written. 
communityCount 
Integer 
The number of communities found. 
ranLevels 
Integer 
The number of supersteps the algorithm actually ran. 
modularity 
Float 
The final modularity score. 
modularities 
List of Float 
The modularity scores for each level. 
communityDistribution 
Map 
Map containing min, max, mean as well as p1, p5, p10, p25, p50, p75, p90, p95, p99 and p999 percentile values of community size for the last level. 
configuration 
Map 
The configuration used for running the algorithm. 
Examples
All the examples below should be run in an empty database. The examples use native projections as the norm, although Cypher projections can be used as well. 
In this section we will show examples of running the Louvain community detection algorithm on a concrete graph. The intention is to illustrate what the results look like and to provide a guide in how to make use of the algorithm in a real setting. We will do this on a small social network graph of a handful nodes connected in a particular pattern. The example graph looks like this:
CREATE
(nAlice:User {name: 'Alice', seed: 42}),
(nBridget:User {name: 'Bridget', seed: 42}),
(nCharles:User {name: 'Charles', seed: 42}),
(nDoug:User {name: 'Doug'}),
(nMark:User {name: 'Mark'}),
(nMichael:User {name: 'Michael'}),
(nAlice)[:LINK {weight: 1}]>(nBridget),
(nAlice)[:LINK {weight: 1}]>(nCharles),
(nCharles)[:LINK {weight: 1}]>(nBridget),
(nAlice)[:LINK {weight: 5}]>(nDoug),
(nMark)[:LINK {weight: 1}]>(nDoug),
(nMark)[:LINK {weight: 1}]>(nMichael),
(nMichael)[:LINK {weight: 1}]>(nMark);
This graph has two clusters of Users, that are closely connected.
Between those clusters there is one single edge.
The relationships that connect the nodes in each component have a property weight
which determines the strength of the relationship.
We can now project the graph and store it in the graph catalog.
We load the LINK
relationships with orientation set to UNDIRECTED
as this works best with the Louvain algorithm.
CALL gds.graph.project(
'myGraph',
'User',
{
LINK: {
orientation: 'UNDIRECTED'
}
},
{
nodeProperties: 'seed',
relationshipProperties: 'weight'
}
)
In the following examples we will demonstrate using the Louvain algorithm on this graph.
Memory Estimation
First off, we will estimate the cost of running the algorithm using the estimate
procedure.
This can be done with any execution mode.
We will use the write
mode in this example.
Estimating the algorithm is useful to understand the memory impact that running the algorithm on your graph will have.
When you later actually run the algorithm in one of the execution modes the system will perform an estimation.
If the estimation shows that there is a very high probability of the execution going over its memory limitations, the execution is prohibited.
To read more about this, see Automatic estimation and execution blocking.
For more details on estimate
in general, see Memory Estimation.
CALL gds.louvain.write.estimate('myGraph', { writeProperty: 'community' })
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
nodeCount  relationshipCount  bytesMin  bytesMax  requiredMemory 

6 
14 
5329 
563224 
"[5329 Bytes ... 550 KiB]" 
Stream
In the stream
execution mode, the algorithm returns the community ID for each node.
This allows us to inspect the results directly or postprocess them in Cypher without any side effects.
For more details on the stream
mode in general, see Stream.
CALL gds.louvain.stream('myGraph')
YIELD nodeId, communityId, intermediateCommunityIds
RETURN gds.util.asNode(nodeId).name AS name, communityId
ORDER BY name ASC
name  communityId 

"Alice" 
2 
"Bridget" 
2 
"Charles" 
2 
"Doug" 
5 
"Mark" 
5 
"Michael" 
5 
We use default values for the procedure configuration parameter.
Levels and innerIterations
are set to 10 and the tolerance value is 0.0001.
Stats
In the stats
execution mode, the algorithm returns a single row containing a summary of the algorithm result.
This execution mode does not have any side effects.
It can be useful for evaluating algorithm performance by inspecting the computeMillis
return item.
In the examples below we will omit returning the timings.
The full signature of the procedure can be found in the syntax section.
For more details on the stats
mode in general, see Stats.
CALL gds.louvain.stats('myGraph')
YIELD communityCount
communityCount 

2 
Mutate
The mutate
execution mode extends the stats
mode with an important side effect: updating the named graph with a new node property containing the community ID for that node.
The name of the new property is specified using the mandatory configuration parameter mutateProperty
.
The result is a single summary row, similar to stats
, but with some additional metrics.
The mutate
mode is especially useful when multiple algorithms are used in conjunction.
For more details on the mutate
mode in general, see Mutate.
myGraph
:CALL gds.louvain.mutate('myGraph', { mutateProperty: 'communityId' })
YIELD communityCount, modularity, modularities
communityCount  modularity  modularities 

2 
0.3571428571428571 
[0.3571428571428571] 
In mutate
mode, only a single row is returned by the procedure.
The result contains meta information, like the number of identified communities and the modularity values.
In contrast to the write
mode the result is written to the GDS inmemory graph instead of the Neo4j database.
Write
The write
execution mode extends the stats
mode with an important side effect: writing the community ID for each node as a property to the Neo4j database.
The name of the new property is specified using the mandatory configuration parameter writeProperty
.
The result is a single summary row, similar to stats
, but with some additional metrics.
The write
mode enables directly persisting the results to the database.
For more details on the write
mode in general, see Write.
CALL gds.louvain.write('myGraph', { writeProperty: 'community' })
YIELD communityCount, modularity, modularities
communityCount  modularity  modularities 

2 
0.3571428571428571 
[0.3571428571428571] 
When writing back the results, only a single row is returned by the procedure. The result contains meta information, like the number of identified communities and the modularity values.
Weighted
The Louvain algorithm can also run on weighted graphs, taking the given relationship weights into concern when calculating the modularity.
CALL gds.louvain.stream('myGraph', { relationshipWeightProperty: 'weight' })
YIELD nodeId, communityId, intermediateCommunityIds
RETURN gds.util.asNode(nodeId).name AS name, communityId
ORDER BY name ASC
name  communityId 

"Alice" 
3 
"Bridget" 
2 
"Charles" 
2 
"Doug" 
3 
"Mark" 
5 
"Michael" 
5 
Using the weighted relationships, we see that Alice
and Doug
have formed their own community, as their link is much stronger than all the others.
Seeded
The Louvain algorithm can be run incrementally, by providing a seed property. With the seed property an initial community mapping can be supplied for a subset of the loaded nodes. The algorithm will try to keep the seeded community IDs.
CALL gds.louvain.stream('myGraph', { seedProperty: 'seed' })
YIELD nodeId, communityId, intermediateCommunityIds
RETURN gds.util.asNode(nodeId).name AS name, communityId
ORDER BY name ASC
name  communityId 

"Alice" 
42 
"Bridget" 
42 
"Charles" 
42 
"Doug" 
47 
"Mark" 
47 
"Michael" 
47 
Using the seeded graph, we see that the community around Alice
keeps its initial community ID of 42
.
The other community is assigned a new community ID, which is guaranteed to be larger than the largest seeded community ID.
Note that the consecutiveIds
configuration option cannot be used in combination with seeding in order to retain the seeding values.
Using intermediate communities
As described before, Louvain is a hierarchical clustering algorithm. That means that after every clustering step all nodes that belong to the same cluster are reduced to a single node. Relationships between nodes of the same cluster become selfrelationships, relationships to nodes of other clusters connect to the clusters representative. This condensed graph is then used to run the next level of clustering. The process is repeated until the clusters are stable.
In order to demonstrate this iterative behavior, we need to construct a more complex graph.
CREATE (a:Node {name: 'a'})
CREATE (b:Node {name: 'b'})
CREATE (c:Node {name: 'c'})
CREATE (d:Node {name: 'd'})
CREATE (e:Node {name: 'e'})
CREATE (f:Node {name: 'f'})
CREATE (g:Node {name: 'g'})
CREATE (h:Node {name: 'h'})
CREATE (i:Node {name: 'i'})
CREATE (j:Node {name: 'j'})
CREATE (k:Node {name: 'k'})
CREATE (l:Node {name: 'l'})
CREATE (m:Node {name: 'm'})
CREATE (n:Node {name: 'n'})
CREATE (x:Node {name: 'x'})
CREATE (a)[:TYPE]>(b)
CREATE (a)[:TYPE]>(d)
CREATE (a)[:TYPE]>(f)
CREATE (b)[:TYPE]>(d)
CREATE (b)[:TYPE]>(x)
CREATE (b)[:TYPE]>(g)
CREATE (b)[:TYPE]>(e)
CREATE (c)[:TYPE]>(x)
CREATE (c)[:TYPE]>(f)
CREATE (d)[:TYPE]>(k)
CREATE (e)[:TYPE]>(x)
CREATE (e)[:TYPE]>(f)
CREATE (e)[:TYPE]>(h)
CREATE (f)[:TYPE]>(g)
CREATE (g)[:TYPE]>(h)
CREATE (h)[:TYPE]>(i)
CREATE (h)[:TYPE]>(j)
CREATE (i)[:TYPE]>(k)
CREATE (j)[:TYPE]>(k)
CREATE (j)[:TYPE]>(m)
CREATE (j)[:TYPE]>(n)
CREATE (k)[:TYPE]>(m)
CREATE (k)[:TYPE]>(l)
CREATE (l)[:TYPE]>(n)
CREATE (m)[:TYPE]>(n);
CALL gds.graph.project(
'myGraph2',
'Node',
{
TYPE: {
orientation: 'undirected',
aggregation: 'NONE'
}
}
)
Stream intermediate communities
CALL gds.louvain.stream('myGraph2', { includeIntermediateCommunities: true })
YIELD nodeId, communityId, intermediateCommunityIds
RETURN gds.util.asNode(nodeId).name AS name, communityId, intermediateCommunityIds
ORDER BY name ASC
name  communityId  intermediateCommunityIds 

"a" 
14 
[3, 14] 
"b" 
14 
[3, 14] 
"c" 
14 
[14, 14] 
"d" 
14 
[3, 14] 
"e" 
14 
[14, 14] 
"f" 
14 
[14, 14] 
"g" 
7 
[7, 7] 
"h" 
7 
[7, 7] 
"i" 
7 
[7, 7] 
"j" 
12 
[12, 12] 
"k" 
12 
[12, 12] 
"l" 
12 
[12, 12] 
"m" 
12 
[12, 12] 
"n" 
12 
[12, 12] 
"x" 
14 
[14, 14] 
In this example graph, after the first iteration we see 4 clusters, which in the second iteration are reduced to three.
Mutate intermediate communities
CALL gds.louvain.mutate('myGraph2', {
mutateProperty: 'intermediateCommunities',
includeIntermediateCommunities: true
})
YIELD communityCount, modularity, modularities
communityCount  modularity  modularities 

3 
0.3816 
[0.37599999999999995, 0.3816] 
CALL gds.graph.nodeProperty.stream('myGraph2', 'intermediateCommunities')
YIELD nodeId, propertyValue
RETURN
gds.util.asNode(nodeId).name AS name,
toIntegerList(propertyValue) AS intermediateCommunities
ORDER BY name ASC
name  intermediateCommunities 

"a" 
[3, 14] 
"b" 
[3, 14] 
"c" 
[14, 14] 
"d" 
[3, 14] 
"e" 
[14, 14] 
"f" 
[14, 14] 
"g" 
[7, 7] 
"h" 
[7, 7] 
"i" 
[7, 7] 
"j" 
[12, 12] 
"k" 
[12, 12] 
"l" 
[12, 12] 
"m" 
[12, 12] 
"n" 
[12, 12] 
"x" 
[14, 14] 
Write intermediate communities
CALL gds.louvain.write('myGraph2', {
writeProperty: 'intermediateCommunities',
includeIntermediateCommunities: true
})
YIELD communityCount, modularity, modularities
communityCount  modularity  modularities 

3 
0.3816 
[0.37599999999999995, 0.3816] 
MATCH (n:Node) RETURN n.name AS name, toIntegerList(n.intermediateCommunities) AS intermediateCommunities
ORDER BY name ASC
name  intermediateCommunities 

"a" 
[3, 14] 
"b" 
[3, 14] 
"c" 
[14, 14] 
"d" 
[3, 14] 
"e" 
[14, 14] 
"f" 
[14, 14] 
"g" 
[7, 7] 
"h" 
[7, 7] 
"i" 
[7, 7] 
"j" 
[12, 12] 
"k" 
[12, 12] 
"l" 
[12, 12] 
"m" 
[12, 12] 
"n" 
[12, 12] 
"x" 
[14, 14] 