Modularity Optimization - Neo4j Graph Data Science
Maybe your like
-
- The Neo4j Graph Data Science Library Manual v2.27
- Introduction
- Installation
- Supported Neo4j versions
- Neo4j Desktop
- Neo4j Server
- Neo4j on Docker
- GDS Enterprise Edition
- Configure Apache Arrow server
- System Requirements
- Getting started
- Basic workflow
- End-to-end workflow
- Machine learning pipeline
- Common usage
- Memory Estimation
- Projecting graphs
- Running algorithms
- Logging
- Monitoring system
- System Information
- Graph management
- Creating graphs
- Cypher projection
- Native projection
- Apache Arrow projection
- Filtering
- Sampling
- Random walk with restarts sampling
- Common Neighbour Aware Random Walk sampling
- Random generation
- Cypher projection (deprecated)
- Catalog operations
- Listing graphs
- Check if a graph exists
- Dropping graphs
- Reading from graphs
- Streaming nodes
- Streaming relationships
- Updating graphs
- Adding node labels
- Converting directed relationships to undirected
- Collapse Path
- Scaling node properties
- Dropping parts of the graph
- Writing back to Neo4j
- Writing node properties and labels
- Writing relationships
- Exporting graphs
- Export to a new Neo4j database
- Export to CSV
- Export using Apache Arrow
- Utility functions
- Administration
- Access control
- Backup and restore
- Creating graphs
- Graph algorithms
- Syntax overview
- Centrality
- Article Rank
- Articulation Points
- Betweenness Centrality
- Bridges
- CELF
- Closeness Centrality
- Degree Centrality
- Eigenvector Centrality
- PageRank
- Harmonic Centrality
- HITS
- Community detection
- Clique Counting
- Conductance metric
- HDBSCAN
- K-Core Decomposition
- K-1 Coloring
- K-Means Clustering
- Label Propagation
- Leiden
- Local Clustering Coefficient
- Louvain
- Modularity metric
- Modularity Optimization
- Strongly Connected Components
- Triangle Count
- Weakly Connected Components
- Approximate Maximum k-cut
- Speaker-Listener Label Propagation
- Similarity
- Node Similarity
- Filtered Node Similarity
- K-Nearest Neighbors
- Filtered K-Nearest Neighbors
- Similarity functions
- Path finding
- Delta-Stepping Single-Source Shortest Path
- Dijkstra Source-Target Shortest Path
- Dijkstra Single-Source Shortest Path
- A* Shortest Path
- Yen’s Shortest Path algorithm
- Minimum Weight Spanning Tree
- Minimum Weight k-Spanning Tree
- Minimum Directed Steiner Tree
- Prize-Collecting Steiner Tree
- All Pairs Shortest Path
- Random Walk
- Breadth First Search
- Depth First Search
- Bellman-Ford Single-Source Shortest Path
- Maximum flow
- Minimum cost maximum flow
- Longest Path for DAG
- DAG algorithms
- Topological Sort
- Longest Path for DAG
- Node embeddings
- Fast Random Projection
- GraphSAGE
- Node2Vec
- HashGNN
- Topological link prediction
- Adamic Adar
- Common Neighbors
- Preferential Attachment
- Resource Allocation
- Same Community
- Total Neighbors
- Pregel API
- Machine learning
- Pre-processing
- Scale Properties
- One Hot Encoding
- Split Relationships
- Node embeddings
- Fast Random Projection
- GraphSAGE
- Node2Vec
- HashGNN
- Node property prediction
- Node classification pipelines
- Configuring the pipeline
- Training the pipeline
- Applying a trained model for prediction
- Node regression pipelines
- Configuring the pipeline
- Training the pipeline
- Applying a trained model for prediction
- Node classification pipelines
- Link prediction pipelines
- Configuring the pipeline
- Training the pipeline
- Applying a trained model for prediction
- Theoretical considerations
- Pipeline catalog
- Listing pipelines
- Checking if a pipeline exists
- Removing pipelines
- Model catalog
- Listing models
- Checking if a model exists
- Dropping models
- Storing models on disk
- Publishing models
- Training methods
- Logistic regression
- Random forest
- Multilayer Perceptron
- Linear regression
- Auto-tuning
- Pre-processing
- Production deployment
- Defaults and Limits
- Transaction Handling
- Using GDS and composite databases (formerly known as Fabric)
- GDS with Neo4j cluster
- GDS Configuration Settings
- GDS Feature Toggles
- Python client
- Aura Graph Analytics
- Quickstart
- Cypher API
- Bloom visualization
- Appendix
- Operations reference
- Graph Catalog
- Graph Algorithms
- Machine Learning
- Additional Operations
- Configuration Settings
- Migration from Graph Data Science library Version 1.x
- Common changes
- Graph projection
- Graph listing
- Graph drop
- Memory estimation
- Algorithms
- Machine Learning
- Migration from Legacy to new Cypher projection
- Migration from Alpha Cypher Aggregation to new Cypher projection
- Operations reference
Is this page helpful?
- Neo4j Graph Data Science
- Graph algorithms
- Community detection
- Modularity Optimization
Directed
Undirected
Heterogeneous nodes
Heterogeneous relationships
Weighted relationships
Node properties
Glossary
DirectedDirected trait. The algorithm is well-defined on a directed graph.
DirectedDirected trait. The algorithm ignores the direction of the graph.
DirectedDirected trait. The algorithm does not run on a directed graph.
UndirectedUndirected trait. The algorithm is well-defined on an undirected graph.
UndirectedUndirected trait. The algorithm ignores the undirectedness of the graph.
Heterogeneous nodesHeterogeneous nodes fully supported. The algorithm has the ability to distinguish between nodes of different types.
Heterogeneous nodesHeterogeneous nodes allowed. The algorithm treats all selected nodes similarly regardless of their label.
Heterogeneous relationshipsHeterogeneous relationships fully supported. The algorithm has the ability to distinguish between relationships of different types.
Heterogeneous relationshipsHeterogeneous relationships allowed. The algorithm treats all selected relationships similarly regardless of their type.
Weighted relationshipsWeighted trait. The algorithm supports a relationship property to be used as weight, specified via the relationshipWeightProperty configuration parameter.
Weighted relationshipsWeighted trait. The algorithm treats each relationship as equally important, discarding the value of any relationship weight.
Node propertiesNode properties trait. The algorithm makes use of node properties.
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:
-
MEJ Newman, M Girvan "Finding and evaluating community structure in networks"
-
https://en.wikipedia.org/wiki/Modularity_(networks)
| Running this algorithm requires sufficient memory availability. Before running this algorithm, we recommend that you read Memory Estimation. |
Syntax
Modularity Optimization syntax per mode Run Modularity Optimization in stream mode on a named graph. CALL gds.modularityOptimization.stream(graphName: String, configuration: Map) YIELD nodeId: Integer, communityId: Integer| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
graphName | String | n/a | no | The name of a graph stored in the catalog. |
configuration | Map | {} | yes | Configuration for algorithm-specifics and/or graph filtering. |
| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
nodeLabels | List of String | ['*'] | yes | Filter the named graph using the given node labels. Nodes with any of the given labels will be included. |
relationshipTypes | List of String | ['*'] | yes | Filter the named graph using the given relationship types. Relationships with any of the given types will be included. |
concurrency | Integer | 4 [1] | yes | The number of concurrent threads used for running the algorithm. |
jobId | String | Generated internally | yes | An ID that can be provided to more easily track the algorithm’s progress. |
logProgress | Boolean | true | yes | If disabled the progress percentage will not be logged. |
maxIterations | Integer | 10 | yes | The maximum number of iterations to run. |
tolerance | 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 non-negative number). |
consecutiveIds | Boolean | false | yes | Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). |
relationshipWeightProperty | String | null | yes | Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
minCommunitySize | Integer | 0 | yes | Only community ids of communities with a size greater than or equal to the given value are written to Neo4j. |
1. In a GDS Session, the default is the number of available processors. | ||||
| Name | Type | Description |
|---|---|---|
nodeId | Integer | Node ID |
communityId | Integer | Community ID |
| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
graphName | String | n/a | no | The name of a graph stored in the catalog. |
configuration | Map | {} | yes | Configuration for algorithm-specifics and/or graph filtering. |
| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
concurrency | Integer | 4 [2] | yes | The number of concurrent threads used for running the algorithm. |
jobId | String | Generated internally | yes | An ID that can be provided to more easily track the algorithm’s progress. |
logProgress | Boolean | true | yes | If disabled the progress percentage will not be logged. |
relationshipWeightProperty | String | null | yes | Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
communityProperty | String | n/a | no | The node property that holds the community ID as an integer for each node. Note that only non-negative community IDs are considered valid and will have their modularity score computed. |
2. In a GDS Session, the default is the number of available processors. | ||||
| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
nodeLabels | List of String | ['*'] | yes | Filter the named graph using the given node labels. Nodes with any of the given labels will be included. |
relationshipTypes | List of String | ['*'] | yes | Filter the named graph using the given relationship types. Relationships with any of the given types will be included. |
concurrency | Integer | 4 [3] | yes | The number of concurrent threads used for running the algorithm. |
jobId | String | Generated internally | yes | An ID that can be provided to more easily track the algorithm’s progress. |
logProgress | Boolean | true | yes | If disabled the progress percentage will not be logged. |
maxIterations | Integer | 10 | yes | The maximum number of iterations to run. |
tolerance | 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 non-negative number). |
consecutiveIds | Boolean | false | yes | Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). |
relationshipWeightProperty | String | null | yes | Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
3. In a GDS Session, the default is the number of available processors. | ||||
| 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. |
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. |
| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
graphName | String | n/a | no | The name of a graph stored in the catalog. |
configuration | Map | {} | yes | Configuration for algorithm-specifics and/or graph filtering. |
| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
mutateProperty | String | n/a | no | The node property in the GDS graph to which the community is written. |
nodeLabels | List of String | ['*'] | yes | Filter the named graph using the given node labels. |
relationshipTypes | List of String | ['*'] | yes | Filter the named graph using the given relationship types. |
concurrency | Integer | 4 | yes | The number of concurrent threads used for running the algorithm. |
logProgress | Boolean | true | yes | If disabled the progress percentage will not be logged. |
jobId | String | Generated internally | yes | An ID that can be provided to more easily track the algorithm’s progress. |
seedProperty | String | n/a | yes | Used to set the initial community for a node. The property value needs to be a number. |
maxIterations | Integer | 10 | yes | The maximum number of iterations that the modularity optimization will run for each level. |
tolerance | 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. |
consecutiveIds | Boolean | false | yes | Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). |
relationshipWeightProperty | String | null | yes | Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
| 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. |
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. |
| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
graphName | String | n/a | no | The name of a graph stored in the catalog. |
configuration | Map | {} | yes | Configuration for algorithm-specifics and/or graph filtering. |
| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
nodeLabels | List of String | ['*'] | yes | Filter the named graph using the given node labels. Nodes with any of the given labels will be included. |
relationshipTypes | List of String | ['*'] | yes | Filter the named graph using the given relationship types. Relationships with any of the given types will be included. |
concurrency | Integer | 4 [4] | yes | The number of concurrent threads used for running the algorithm. |
jobId | String | Generated internally | yes | An ID that can be provided to more easily track the algorithm’s progress. |
logProgress | Boolean | true | yes | If disabled the progress percentage will not be logged. |
writeConcurrency | Integer | value of 'concurrency' | yes | The number of concurrent threads used for writing the result to Neo4j. |
writeProperty | String | n/a | no | The node property in the Neo4j database to which the community is written. |
4. In a GDS Session, the default is the number of available processors. | ||||
| Name | Type | Default | Optional | Description |
|---|---|---|---|---|
nodeLabels | List of String | ['*'] | yes | Filter the named graph using the given node labels. Nodes with any of the given labels will be included. |
relationshipTypes | List of String | ['*'] | yes | Filter the named graph using the given relationship types. Relationships with any of the given types will be included. |
concurrency | Integer | 4 [5] | yes | The number of concurrent threads used for running the algorithm. |
jobId | String | Generated internally | yes | An ID that can be provided to more easily track the algorithm’s progress. |
logProgress | Boolean | true | yes | If disabled the progress percentage will not be logged. |
writeConcurrency | Integer | value of 'concurrency' | yes | The number of concurrent threads used for writing the result to Neo4j. |
writeProperty | String | n/a | no | The node property in the Neo4j database to which the community is written. |
seedProperty | String | n/a | yes | Used to set the initial community for a node. The property value needs to be a number. |
maxIterations | Integer | 10 | yes | The maximum number of iterations that the modularity optimization will run for each level. |
tolerance | 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. |
consecutiveIds | Boolean | false | yes | Flag to decide whether component identifiers are mapped into a consecutive id space (requires additional memory). |
relationshipWeightProperty | String | null | yes | Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
minCommunitySize | Integer | 0 | yes | Only community ids of communities with a size greater than or equal to the given value are written to Neo4j. |
5. In a GDS Session, the default is the number of available processors. | ||||
| 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. |
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. |
Examples
| All the examples below should be run in an empty database. The examples use Cypher projections as the norm. Native projections will be deprecated in a future release. |
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.
The following statement will project the graph and store it in the graph catalog. MATCH (source:Person)-[r:KNOWS]->(target:Person) RETURN gds.graph.project( 'myGraph', source, target, { relationshipProperties: r { .weight } }, { undirectedRelationshipTypes: ['*'] } )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.
The following will estimate the memory requirements for running the algorithm: CALL gds.modularityOptimization.write.estimate('myGraph', { relationshipWeightProperty: 'weight', writeProperty: 'community' }) YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory| nodeCount | relationshipCount | bytesMin | bytesMax | requiredMemory |
|---|---|---|---|---|
6 | 14 | 5160 | 5248 | "[5160 Bytes ... 5248 Bytes]" |
Stream
In the stream execution mode, the algorithm returns the community for each node. This allows us to inspect the results directly or post-process them in Cypher without any side effects.
For more details on the stream mode in general, see Stream.
Running the Modularity Optimization algorithm in stream mode: CALL gds.modularityOptimization.stream('myGraph', { relationshipWeightProperty: 'weight' }) YIELD nodeId, communityId RETURN gds.util.asNode(nodeId).name AS name, communityId ORDER BY name| name | communityId |
|---|---|
"Alice" | 3 |
"Bridget" | 1 |
"Charles" | 1 |
"Doug" | 1 |
"Elton" | 3 |
"Frank" | 3 |
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.
Running the Modularity Optimization algorithm in stats mode: CALL gds.modularityOptimization.stats('myGraph', { relationshipWeightProperty: 'weight' }) YIELD nodes, communityCount, ranIterations, didConverge| nodes | communityCount | ranIterations | didConverge |
|---|---|---|---|
6 | 2 | 2 | true |
Write
The write execution mode extends the stats mode with an important side effect: writing the community 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.
Running the Modularity Optimization algorithm in write mode: CALL gds.modularityOptimization.write('myGraph', { relationshipWeightProperty: 'weight', writeProperty: 'community' }) YIELD nodes, communityCount, ranIterations, didConverge| nodes | communityCount | ranIterations | didConverge |
|---|---|---|---|
6 | 2 | 2 | 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.
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 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.
Running the Modularity Optimization algorithm in mutate mode: CALL gds.modularityOptimization.mutate('myGraph', { relationshipWeightProperty: 'weight', mutateProperty: 'community' }) YIELD nodes, communityCount, ranIterations, didConverge| nodes | communityCount | ranIterations | didConverge |
|---|---|---|---|
6 | 2 | 2 | true |
When using mutate mode the procedure will return information about the algorithm execution as in write mode.
Modularity metric Strongly Connected ComponentsGraph Data Science courses on GraphAcademy
Follow our Graph Data Analytics learning path to learn how to apply graph thinking to your machine learning pipelines.
Enroll nowTag » What Is A High Modularity Score
-
Modularity (networks) - Wikipedia
-
[PDF] Modularity
-
Community Detection - Tim Stuart
-
What Does Modularity Score Mean? - High Tech Guide
-
Modularity And Community Structure In Networks - PNAS
-
Modularity
-
Modularity — SparklingGraph 0.0.6 Documentation
-
Gephi - Clustering Layout By Modularity - Data Story
-
Community Detection Algorithm Based On Centrality And Node ...
-
Difference Between Modularity And Clustering In Graphs
-
Does A Broadly Accepted Threshold Exist That Permits One To Say That ...
-
[PDF] Social And Technological Network Analysis Lecture 4: Modularity ...
-
How Can Modularity Help In Network Analysis [closed] - Stack Overflow
-
Examining The Distribution, Modularity, And Community Structure In ...