Modularity Optimization
This section describes the Modularity Optimization algorithm in the Neo4j Graph Data Science library.
This algorithm is in the beta tier. For more information on algorithm tiers, see Algorithms.
1. Introduction
The Modularity Optimization algorithm tries to detect communities in the graph based on their modularity. Modularity is a measure of the structure of a graph, measuring the density of connections within a module or community. Graphs with a high modularity score will have many connections within a community but only few pointing outwards to other communities. The algorithm will explore for every node if its modularity score might increase if it changes its community to one of its neighboring nodes.
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. 
2. Syntax
CALL gds.beta.modularityOptimization.stream(graphName: String, configuration: Map)
YIELD
nodeId: Integer,
communityId: Integer
Name  Type  Default  Optional  Description 

graphName 
String or Map 

no 
Either the name of a graph stored in the catalog or a Map configuring the graph creation and algorithm execution. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. Must be empty if graphNameOrConfig is a Map. 
Name  Type  Default  Optional  Description 

Integer 

yes 
The number of concurrent threads used for running the algorithm. Also provides the default value for 'readConcurrency' and 'writeConcurrency'. 

readConcurrency 
Integer 

yes 
The number of concurrent threads used for reading the graph. 
Integer 

yes 
The number of concurrent threads used for writing the result (applicable in WRITE mode). 

nodeProjection 
Map or List 

yes 
The node projection used for implicit graph loading or filtering nodes of an explicitly loaded graph. 
relationshipProjection 
Map or List 

yes 
The relationship projection used for implicit graph loading or filtering relationship of an explicitly loaded graph. 
nodeQuery 
String 

yes 
The Cypher query used to select the nodes for implicit graph loading via a Cypher projection. 
relationshipQuery 
String 

yes 
The Cypher query used to select the relationships for implicit graph loading via a Cypher projection. 
nodeProperties 
Map or List 

yes 
The node properties to load during implicit graph loading. 
relationshipProperties 
Map or List 

yes 
The relationship properties to load during implicit graph loading. 
Name  Type  Default  Optional  Description 

Integer 
10 
yes 
The maximum number of iterations to run. 

Float 
0.0001 
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. 

seedProperty 
String 
n/a 
yes 
Used to define initial set of labels (must be a number). 
consecutiveIds 
Boolean 
false 
yes 
Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). 
String 
null 
yes 
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. 
Name  Type  Description 

nodeId 
Integer 
Node ID 
communityId 
Integer 
Community ID 
CALL gds.beta.modularityOptimization.mutate(graphName: StringMap, configuration: Map})
YIELD
createMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
mutateMillis: Integer,
communityCount: Integer,
communityDistribution: Map,
modularity: Float,
ranIterations: Integer,
didConverge: Boolean,
nodes: Integer,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String or Map 

no 
Either the name of a graph stored in the catalog or a Map configuring the graph creation and algorithm execution. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. Must be empty if graphNameOrConfig is a Map. 
The configuration for the mutate
mode is similar to the write
mode.
Instead of specifying a writeProperty
, we need to specify a mutateProperty
.
Also, specifying writeConcurrency
is not possible in mutate
mode.
Name  Type  Description 

createMillis 
Integer 
Milliseconds for loading data. 
computeMillis 
Integer 
Milliseconds for running the algorithm. 
mutateMillis 
Integer 
Milliseconds for adding properties to the inmemory graph. 
postProcessingMillis 
Integer 
Milliseconds for computing percentiles and community count. 
nodes 
Integer 
The number of nodes considered. 
didConverge 
Boolean 
True if the algorithm did converge to a stable modularity score within the provided number of maximum iterations. 
ranIterations 
Integer 
The number of iterations run. 
modularity 
Float 
The final modularity score. 
communityCount 
Integer 
The number of communities found. 
communityDistribution 
Map 
The containing min, max, mean as well as 50, 75, 90, 95, 99 and 999 percentile of community size. 
configuration 
Map 
The configuration used for running the algorithm. 
CALL gds.beta.modularityOptimization.write(graphName: StringMap, configuration: Map})
YIELD
createMillis: Integer,
computeMillis: Integer,
postProcessingMillis: Integer,
writeMillis: Integer,
communityCount: Integer,
communityDistribution: Map,
modularity: Float,
ranIterations: Integer,
didConverge: Boolean,
nodes: Integer,
configuration: Map
Name  Type  Default  Optional  Description 

graphName 
String or Map 

no 
Either the name of a graph stored in the catalog or a Map configuring the graph creation and algorithm execution. 
configuration 
Map 

yes 
Configuration for algorithmspecifics and/or graph filtering. Must be empty if graphNameOrConfig is a Map. 
Name  Type  Default  Optional  Description 

Integer 

yes 
The number of concurrent threads used for running the algorithm. Also provides the default value for 'readConcurrency' and 'writeConcurrency'. 

readConcurrency 
Integer 

yes 
The number of concurrent threads used for reading the graph. 
Integer 

yes 
The number of concurrent threads used for writing the result (applicable in WRITE mode). 

nodeProjection 
Map or List 

yes 
The node projection used for implicit graph loading or filtering nodes of an explicitly loaded graph. 
relationshipProjection 
Map or List 

yes 
The relationship projection used for implicit graph loading or filtering relationship of an explicitly loaded graph. 
nodeQuery 
String 

yes 
The Cypher query used to select the nodes for implicit graph loading via a Cypher projection. 
relationshipQuery 
String 

yes 
The Cypher query used to select the relationships for implicit graph loading via a Cypher projection. 
nodeProperties 
Map or List 

yes 
The node properties to load during implicit graph loading. 
relationshipProperties 
Map or List 

yes 
The relationship properties to load during implicit graph loading. 
Name  Type  Default  Optional  Description 

String 

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

String 

yes 
The property name written back the ID of the partition particular node belongs to. 

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. 

consecutiveIds 
Boolean 

yes 
Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). 
String 

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

createMillis 
Integer 
Milliseconds for loading 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. 
nodes 
Integer 
The number of nodes considered. 
didConverge 
Boolean 
True if the algorithm did converge to a stable modularity score within the provided number of maximum iterations. 
ranIterations 
Integer 
The number of iterations run. 
modularity 
Float 
The final modularity score. 
communityCount 
Integer 
The number of communities found. 
communityDistribution 
Map 
The containing min, max, mean as well as 50, 75, 90, 95, 99 and 999 percentile of community size. 
configuration 
Map 
The configuration used for running the algorithm. 
3. Examples
Consider the graph created by the following Cypher statement:
CREATE
(a:Person {name:'Alice'})
, (b:Person {name:'Bridget'})
, (c:Person {name:'Charles'})
, (d:Person {name:'Doug'})
, (e:Person {name:'Elton'})
, (f:Person {name:'Frank'})
, (a)[:KNOWS {weight: 0.01}]>(b)
, (a)[:KNOWS {weight: 5.0}]>(e)
, (a)[:KNOWS {weight: 5.0}]>(f)
, (b)[:KNOWS {weight: 5.0}]>(c)
, (b)[:KNOWS {weight: 5.0}]>(d)
, (c)[:KNOWS {weight: 0.01}]>(e)
, (f)[:KNOWS {weight: 0.01}]>(d)
This graph consists of two center nodes "Alice" and "Bridget" each of which have two more neighbors. Additionally, each neighbor of "Alice" is connected to one of the neighbors of "Bridget". Looking at the weights of the relationships, it can be seen that the connections from the two center nodes to their neighbors are very strong, while connections between those groups are weak. Therefore the Modularity Optimization algorithm should detect two communities: "Alice" and "Bob" together with their neighbors respectively.
In the examples below we will use named graphs and native projections as the norm. However, anonymous graphs and/or Cypher projections can also be used. 
CALL gds.graph.create(
'myGraph',
'Person',
{
KNOWS: {
type: 'KNOWS',
orientation: 'UNDIRECTED',
properties: ['weight']
}
})
The following example demonstrates using the Modularity Algorithm on this weighted graph.
CALL gds.beta.modularityOptimization.stream('myGraph', { relationshipWeightProperty: 'weight' })
YIELD nodeId, communityId
RETURN gds.util.asNode(nodeId).name AS name, communityId
ORDER BY name
name  communityId 

"Alice" 
4 
"Bridget" 
1 
"Charles" 
1 
"Doug" 
1 
"Elton" 
4 
"Frank" 
4 
It is also possible to write the assigned community ids back to the database using the write
mode.
CALL gds.beta.modularityOptimization.write('myGraph', { relationshipWeightProperty: 'weight', writeProperty: 'community' })
YIELD nodes, communityCount, ranIterations, didConverge
nodes  communityCount  ranIterations  didConverge 

6 
2 
3 
true 
When using write
mode the procedure will return information about the algorithm execution.
In this example we return the number of processed nodes, the number of communities assigned to the nodes in the graph, the number of iterations and information whether the algorithm converged.
Running the algorithm without specifying the relationshipWeightProperty
will default all relationship weights to 1.0.
To instead mutate the inmemory graph with the assigned community ids, the mutate
mode is used.
CALL gds.beta.modularityOptimization.mutate('myGraph', { relationshipWeightProperty: 'weight', mutateProperty: 'community' })
YIELD nodes, communityCount, ranIterations, didConverge
nodes  communityCount  ranIterations  didConverge 

6 
2 
3 
true 
When using mutate
mode the procedure will return information about the algorithm execution as in write
mode.
Was this page helpful?