Triangle Count
Introduction
The Triangle Count algorithm counts the number of triangles for each node in the graph. A triangle is a set of three nodes where each node has a relationship to the other two. In graph theory terminology, this is sometimes referred to as a 3-clique. The Triangle Count algorithm in the Neo4j Graph Analytics app only finds triangles in undirected graphs.
Triangle counting has gained popularity in social network analysis, where it is used to detect communities and measure the cohesiveness of those communities. It can also be used to determine the stability of a graph, and is often used as part of the computation of network indices, such as clustering coefficients.
For more information on this algorithm, see:
-
Triangle count and clustering coefficient have been shown to be useful as features for classifying a given website as spam, or non-spam, content. This is described in "Efficient Semi-streaming Algorithms for Local Triangle Counting in Massive Graphs".
Syntax
CALL Neo4j_Graph_Analytics.graph.triangle_count(
'CPU_X64_XS', (1)
{
'project': {...}, (2)
'compute': {...}, (3)
'write': {...} (4)
}
);
1 | Compute pool selector. |
2 | Project config. |
3 | Compute config. |
4 | Write config. |
Name | Type | Default | Optional | Description |
---|---|---|---|---|
computePoolSelector |
String |
|
no |
The selector for the compute pool on which to run the Triangle Count 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. |
Name | Type |
---|---|
nodeTables |
List of node tables. |
relationshipTables |
Map of relationship types to relationship tables. |
Name | Type | Default | Optional | Description |
---|---|---|---|---|
mutateProperty |
String |
|
yes |
The node property that will be written back to the Snowflake database. |
relationshipWeightProperty |
String |
|
yes |
Name of the relationship property to use as weights. If unspecified, the algorithm runs unweighted. |
seedProperty |
String |
|
yes |
Used to set the initial component for a node. The property value needs to be a number. |
threshold |
Float |
|
yes |
The value of the weight above which the relationship is considered in the computation. |
maxDegree |
Integer |
|
yes |
Maximum degree of a node to be considered in triangle counting. Nodes with degree higher than this value will be excluded and get assigned a triangle count of |
For more details on below Write configuration, refer to the Write documentation. |
Name | Type | Default | Optional | Description |
---|---|---|---|---|
nodeLabel |
String |
|
no |
Node label in the in-memory graph from which to write a node property. |
nodeProperty |
String |
|
yes |
The node property that will be written back to the Snowflake database. |
outputTable |
String |
|
no |
Table in Snowflake database to which node properties are written. |
Examples
In this section we will show examples of running the Triangle Count 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 OR REPLACE TABLE EXAMPLE_DB.DATA_SCHEMA.PERSON (NODEID STRING);
INSERT INTO EXAMPLE_DB.DATA_SCHEMA.PERSON VALUES
('Alice'),
('Michael'),
('Karin'),
('Chris'),
('Will'),
('Mark');
CREATE OR REPLACE TABLE EXAMPLE_DB.DATA_SCHEMA.KNOWS (SOURCENODEID STRING, TARGETNODEID STRING);
INSERT INTO EXAMPLE_DB.DATA_SCHEMA.KNOWS VALUES
('Michael', 'Karin'),
('Michael', 'Chris'),
('Will', 'Michael'),
('Mark', 'Michael'),
('Mark', 'Will'),
('Alice', 'Michael'),
('Will', 'Chris'),
('Chris', 'Karin');
This graph has several triangles formed by the relationships between nodes. For example, there is a triangle between Michael, Chris, and Karin, as they are all connected to each other. The algorithm will count these triangles for each node.
In the following example, we will demonstrate using the Triangle Count algorithm on this graph.
Run job
Running a triangle count 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.
CALL Neo4j_Graph_Analytics.graph.triangle_count('CPU_X64_XS', {
'project': {
'defaultTablePrefix': 'EXAMPLE_DB.DATA_SCHEMA',
'nodeTables': ['PERSON'],
'relationshipTables': {
'KNOWS': {
'sourceTable': 'PERSON',
'targetTable': 'PERSON',
'orientation': 'UNDIRECTED'
}
}
},
'compute': {},
'write': [
{
'nodeLabel': 'PERSON',
'outputTable': 'EXAMPLE_DB.DATA_SCHEMA.TRIANGLE_COUNT_BY_PERSON'
}
]
});
JOB_ID | JOB_START | JOB_END | JOB_RESULT |
---|---|---|---|
job_492026bbeaa6422eb4502a18def04cd6 |
2025-04-30 13:53:53.702000 |
2025-04-30 13:54:00.716000 |
{ "project_1": { "graphName": "snowgraph", "nodeCount": 6, "nodeMillis": 218, "relationshipCount": 16, "relationshipMillis": 541, "totalMillis": 759 }, "triangle_count_1": { "computeMillis": 15, "configuration": { "concurrency": 2, "jobId": "4a766a5a-57ee-4438-bcca-9294a8bd6743", "logProgress": true, "maxDegree": 9223372036854775807, "mutateProperty": "triangles", "nodeLabels": [ "*" ], "relationshipTypes": [ "*" ], "sudo": false }, "globalTriangleCount": 3, "mutateMillis": 3, "nodeCount": 6, "nodePropertiesWritten": 6, "postProcessingMillis": 0, "preProcessingMillis": 8 }, "write_node_property_1": { "exportMillis": 2268, "nodeLabel": "PERSON", "nodeProperty": "triangles", "outputTable": "EXAMPLE_DB.DATA_SCHEMA.TRIANGLE_COUNT_BY_PERSON", "propertiesExported": 6 } } |
The returned result contains information about the job execution and result distribution. Additionally, the triangle count for each of the nodes has been written back to the Snowflake database. The job result indicates a global triangle count of 3, representing the total number of triangles in the graph. We can query the individual node triangle counts like so:
SELECT * FROM EXAMPLE_DB.DATA_SCHEMA.TRIANGLE_COUNT_BY_PERSON;
NODEID | TRIANGLES |
---|---|
Alice |
0 |
Michael |
3 |
Karin |
1 |
Chris |
2 |
Will |
2 |
Mark |
1 |
The result shows that the algorithm identifies triangles in the graph. This can be verified in the example graph.