Louvain

Neo4j Graph Analytics for Snowflake is in Public Preview and is not intended for production use.

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:

Syntax

Run Louvain.
CALL Neo4j_Graph_Analytics.graph.louvain(
  'X64_CPU_L',        (1)
  {
    'project': {...}, (2)
    'compute': {...}, (3)
    'write':   {...}  (4)
  }
);
1 Compute pool selector.
2 Project config.
3 Compute config.
4 Write config.
Table 1. Parameters
Name Type Default Optional Description

computePoolSelector

String

n/a

no

The selector for the compute pool on which to run the Louvain job.

configuration

Map

{}

no

Configuration for graph project, algorithm compute and result write back.

The configuration map consists of the following three entries.

For more details on below Project configuration, refer to the Project documentation.
Table 2. Project configuration
Name Type

nodeTables

List of node tables.

relationshipTables

Map of relationship types to relationship tables.

Table 3. Compute configuration
Name Type Default Optional Description

mutateProperty

String

'community'

yes

The node property that will be written back to the Snowflake database.

relationshipWeightProperty

String

null

yes

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

seedProperty

String

n/a

yes

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

maxLevels

Integer

10

yes

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

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.

includeIntermediateCommunities

Boolean

false

yes

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

consecutiveIds

Boolean

false

yes

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

minCommunitySize

Integer

0

yes

Only nodes inside communities larger or equal the given value are returned.

For more details on below Write configuration, refer to the Write documentation.
Table 4. Write configuration
Name Type Default Optional Description

nodeProperty

String

'community'

yes

The node property that will be written back to the Snowflake database.

Examples

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:

Visualization of the example graph
The following SQL statement will create the example graph tables in the Snowflake database:
CREATE OR REPLACE TABLE EXAMPLE_DB.DATA_SCHEMA.USERS (NODEID STRING);
INSERT INTO EXAMPLE_DB.DATA_SCHEMA.USERS VALUES
  ('Alice'), -- (nAlice:User {name: 'Alice', seed: 42}),
  ('Bridget'), -- (nBridget:User {name: 'Bridget', seed: 42}),
  ('Charles'), -- (nCharles:User {name: 'Charles', seed: 42}),
  ('Doug'), -- (nDoug:User {name: 'Doug'}),
  ('Mark'), -- (nMark:User {name: 'Mark'}),
  ('Michael'); -- (nMichael:User {name: 'Michael'}),

CREATE OR REPLACE TABLE EXAMPLE_DB.DATA_SCHEMA.LINKS (SOURCENODEID STRING, TARGETNODEID STRING, WEIGHT FLOAT);
INSERT INTO EXAMPLE_DB.DATA_SCHEMA.LINKS VALUES
  ('Alice',   'Bridget', 1), -- (nAlice)-[:LINK {weight: 1}]->(nBridget),
  ('Alice',   'Charles', 1), -- (nAlice)-[:LINK {weight: 1}]->(nCharles),
  ('Charles', 'Bridget', 1), -- (nCharles)-[:LINK {weight: 1}]->(nBridget),

  ('Alice',   'Doug',    5), -- (nAlice)-[:LINK {weight: 5}]->(nDoug),

  ('Mark',    'Doug',    1), -- (nMark)-[:LINK {weight: 1}]->(nDoug),
  ('Mark',    'Michael', 1), -- (nMark)-[:LINK {weight: 1}]->(nMichael),
  ('Michael', 'Mark',    1); -- (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 load the LINK relationships with orientation set to UNDIRECTED as this works best with the Louvain algorithm.

With the node and relationship tables in Snowflake we can now project it as part of an algorithm job. In the following examples we will demonstrate using the Louvain algorithm on this graph.

Run job

Running a Louvain job involves the three steps: Project, Compute and Write.

To run the query, there is a required setup of grants for the application, your consumer role and your environment. Please see the Getting started page for more on this.

We also assume that the application name is the default Neo4j_Graph_Analytics. If you chose a different app name during installation, please replace it with that.

The following will run the algorithm and stream results:
CALL Neo4j_Graph_Analytics.graph.louvain('CPU_X64_XS', {
    'project': {
        'defaultTablePrefix': 'EXAMPLE_DB.DATA_SCHEMA',
        'nodeTables': [ 'USERS' ],
        'relationshipTables': {
            'LINKS': {
                'sourceTable': 'USERS',
                'targetTable': 'USERS',
                'orientation': 'UNDIRECTED'
            }
        }
    },
    'compute': {
        'mutateProperty': 'community_id'
    },
    'write': [{
        'nodeLabel': 'USERS',
        'outputTable': 'EXAMPLE_DB.DATA_SCHEMA.USERS_COMMUNITY',
        'nodeProperty': 'community_id'
    }]
});

The returned result contains information about the job execution and result distribution. Additionally, the community ID for each of the nodes has been written back to the Snowflake database. We can query it like so:

SELECT * FROM EXAMPLE_DB.DATA_SCHEMA.USERS_COMMUNITY;
Table 5. Results
NODEID COMMUNITY_ID

Alice

1

Bridget

1

Charles

1

Doug

3

Mark

3

Michael

3

We use default values for the procedure configuration parameter. Levels and innerIterations are set to 10 and the tolerance value is 0.0001.