Modularity metric

Glossary

Directed

Directed trait. The algorithm is well-defined 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 well-defined 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

Modularity is a metric that allows you to evaluate the quality of a community detection. Relationships of nodes in a community C connect to nodes either within C or outside C. Graphs with high modularity have dense connections between the nodes within communities but sparse connections between nodes in different communities.

Syntax

This section covers the syntax used to execute the Modularity Metric 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.

Example 1. Modularity syntax per mode
Run Modularity in stream mode on a named graph.
CALL gds.modularity.stream(
  graphName: String,
  configuration: Map
) YIELD
  communityId: Integer,
  modularity: Float
Table 1. Parameters
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.

Table 2. Configuration
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

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.

Table 3. Results
Name Type Description

communityId

Integer

Community ID.

modularity

Float

Modularity of the community.

Run Modularity in stats mode on a named graph.
CALL gds.modularity.stats(
  graphName: String,
  configuration: Map
) YIELD
  nodeCount: Integer,
  relationshipCount: Integer,
  communityCount: Integer,
  modularity: Float,
  postProcessingMillis: Integer,
  preProcessingMillis: Integer,
  computeMillis: Integer,
  configuration: Map
Table 4. Parameters
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.

Table 5. Configuration
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

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.

Table 6. Results
Name Type Description

nodeCount

Integer

The number of nodes in the graph.

relationshipCount

Integer

The number of relationships in the graph.

communityCount

Integer

The number of communities.

modularity

Float

The total modularity score.

preProcessingMillis

Integer

Milliseconds for preprocessing the data.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing percentiles and community count.

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 Modularity 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:

Visualization of the example graph
The following Cypher statement will create the example graph in the Neo4j database:
CREATE
  (nAlice:User {name: 'Alice', community: 3}),
  (nBridget:User {name: 'Bridget', community: 2}),
  (nCharles:User {name: 'Charles', community: 2}),
  (nDoug:User {name: 'Doug', community: 3}),
  (nMark:User {name: 'Mark', community: 5}),
  (nMichael:User {name: 'Michael', community: 5}),

  (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 three pre-computed communities of Users, that are closely connected. For more details on the available community detection algorithms, please refer to Community algorithms section of the documentation. The communities are indicated by the community node property on each node. 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.

The following statement will project the graph and store it in the graph catalog.
CALL gds.graph.project(
    'myGraph',
    'User',
    {
        LINK: {
            orientation: 'UNDIRECTED'
        }
    },
    {
        nodeProperties: 'community',
        relationshipProperties: 'weight'
    }
)

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 stats 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 in stats mode:
CALL gds.modularity.stats.estimate('myGraph', {
     communityProperty: 'community',
     relationshipWeightProperty: 'weight'
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
Table 7. Results
nodeCount relationshipCount bytesMin bytesMax requiredMemory

6

14

872

872

"872 Bytes"

Stream

Since we have community information on each node, we can evaluate how good it is under the modularity metric. Note that we in this case we use the feature of relationships being weighted by a relationship property.

The Modularity stream procedure returns the modularity for each community. 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.

The following will run the Modularity algorithm in stream mode:
CALL gds.modularity.stream('myGraph', {
     communityProperty: 'community',
     relationshipWeightProperty: 'weight'
})
YIELD communityId, modularity
RETURN communityId, modularity
ORDER BY communityId ASC
Table 8. Results
communityId modularity

2

0.057851239669421

3

0.105371900826446

5

0.130165289256198

We can see that the community of the weighted graph with the highest modularity is community 5. This means that 5 is the community that is most "well-knit" in the sense that most of its relationship weights are internal to the community.

Stats

For more details on the stream mode in general, see Stats.

The following will run the Modularity algorithm in stats mode:
CALL gds.modularity.stats('myGraph', {
     communityProperty: 'community',
     relationshipWeightProperty: 'weight'
})
YIELD nodeCount, relationshipCount, communityCount, modularity
Table 9. Results
nodeCount relationshipCount communityCount modularity

6

14

3

0.293388429752066