Clique Counting
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
A clique is a subset of nodes where all nodes are connected to each other, sometimes called a complete subgraph. The clique counting algorithm counts the number of cliques of various sizes in the graph. In GDS this is done efficiently, without realizing every clique, through a Succinct Clique Tree-structure, see The Power of Pivoting for Exact Clique Counting.
Counting the number of cliques in the graph, can give an understanding for the topology and clustering of the graph in its entirety as well as for individual nodes. The algorithm counts only cliques of size 3 or larger, since cliques of sizes 1 and 2 are trivial (nodes and relationships, respectively). If you are only interested in triangles (cliques of size 3), the Triangle Count is a better choice.
For more about cliques, including applications, see https://en.wikipedia.org/wiki/Clique_(graph_theory).
Syntax
This section covers the syntax used to execute the Clique Counting 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.
CALL gds.cliqueCounting.stream(
graphName: String,
configuration: Map
)
YIELD
nodeId: Integer,
counts: List
Name | Type | Default | Optional | Description |
---|---|---|---|---|
graphName |
String |
|
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 |
---|---|---|---|---|
List of String |
|
yes |
Filter the named graph using the given node labels. Nodes with any of the given labels will be included. |
|
List of String |
|
yes |
Filter the named graph using the given relationship types. Relationships with any of the given types will be included. |
|
Integer |
|
yes |
The number of concurrent threads used for running the algorithm. |
|
String |
|
yes |
An ID that can be provided to more easily track the algorithm’s progress. |
|
Boolean |
|
yes |
If disabled the progress percentage will not be logged. |
|
1. In a GDS Session, the default is the number of available processors. |
Name | Type | Description |
---|---|---|
nodeId |
Integer |
Node ID. |
counts |
List of Integer |
Number of cliques of size ≥ 3 that this node is part of. |
CALL gds.cliqueCounting.stats(
graphName: String,
configuration: Map
)
YIELD
globalCount: List,
preProcessingMillis: Integer,
computeMillis: Integer,
configuration: Map
Name | Type | Default | Optional | Description |
---|---|---|---|---|
graphName |
String |
|
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 |
---|---|---|---|---|
List of String |
|
yes |
Filter the named graph using the given node labels. Nodes with any of the given labels will be included. |
|
List of String |
|
yes |
Filter the named graph using the given relationship types. Relationships with any of the given types will be included. |
|
Integer |
|
yes |
The number of concurrent threads used for running the algorithm. |
|
String |
|
yes |
An ID that can be provided to more easily track the algorithm’s progress. |
|
Boolean |
|
yes |
If disabled the progress percentage will not be logged. |
|
2. In a GDS Session, the default is the number of available processors. |
Name | Type | Description |
---|---|---|
globalCount |
List of Integer |
Number of cliques of sizes ≥ 3 in the graph. |
preProcessingMillis |
Integer |
Milliseconds for preprocessing the graph. |
computeMillis |
Integer |
Milliseconds for running the algorithm. |
configuration |
Map |
The configuration used for running the algorithm. |
CALL gds.cliqueCounting.mutate(
graphName: String,
configuration: Map
)
YIELD
globalCount: List,
nodePropertiesWritten: Integer,
preProcessingMillis: Integer,
computeMillis: Integer,
mutateMillis: Integer,
configuration: Map
Name | Type | Default | Optional | Description |
---|---|---|---|---|
graphName |
String |
|
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 |
|
no |
The node property in the GDS graph to which the clique counts is written. |
List of String |
|
yes |
Filter the named graph using the given node labels. |
|
List of String |
|
yes |
Filter the named graph using the given relationship types. |
|
Integer |
|
yes |
The number of concurrent threads used for running the algorithm. |
|
String |
|
yes |
An ID that can be provided to more easily track the algorithm’s progress. |
Name | Type | Description |
---|---|---|
globalCount |
List of Integer |
Number of cliques of sizes ≥ 3 in the graph. |
nodePropertiesWritten |
Integer |
Number of properties added to the projected graph. |
preProcessingMillis |
Integer |
Milliseconds for preprocessing the graph. |
computeMillis |
Integer |
Milliseconds for running the algorithm. |
mutateMillis |
Integer |
Milliseconds for adding properties to the projected graph. |
configuration |
Map |
The configuration used for running the algorithm. |
CALL gds.cliqueCounting.write(
graphName: String,
configuration: Map
)
YIELD
globalCount: List,
nodePropertiesWritten: Integer,
preProcessingMillis: Integer,
computeMillis: Integer,
writeMillis: Integer,
configuration: Map
Name | Type | Default | Optional | Description |
---|---|---|---|---|
graphName |
String |
|
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 |
|
no |
The node property in the GDS graph to which the clique counts is written. |
List of String |
|
yes |
Filter the named graph using the given node labels. |
|
List of String |
|
yes |
Filter the named graph using the given relationship types. |
|
Integer |
|
yes |
The number of concurrent threads used for running the algorithm. |
|
String |
|
yes |
An ID that can be provided to more easily track the algorithm’s progress. |
Name | Type | Description |
---|---|---|
globalCount |
List of Integer |
Number of cliques of sizes ≥ 3 in the graph. |
nodePropertiesWritten |
Integer |
Number of properties written to Neo4j. |
preProcessingMillis |
Integer |
Milliseconds for preprocessing the graph. |
computeMillis |
Integer |
Milliseconds for running the algorithm. |
writeMillis |
Integer |
Milliseconds for writing results back to Neo4j. |
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. |
In this section we will show examples of running the Clique Counting 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
(alice:Person {name: 'Alice'}),
(michael:Person {name: 'Michael'}),
(karin:Person {name: 'Karin'}),
(chris:Person {name: 'Chris'}),
(will:Person {name: 'Will'}),
(mark:Person {name: 'Mark'}),
(alice)-[:KNOWS]->(michael),
(alice)-[:KNOWS]->(karin),
(alice)-[:KNOWS]->(chris),
(michael)-[:KNOWS]->(karin),
(michael)-[:KNOWS]->(chris),
(karin)-[:KNOWS]->(chris),
(karin)-[:KNOWS]->(will),
(chris)-[:KNOWS]->(will),
(will)-[:KNOWS]->(mark)
With the graph in Neo4j we can now project it into the graph catalog to prepare it for algorithm execution.
We do this using a Cypher projection targeting the Person
nodes and the KNOWS
relationships.
For the relationships we must use the UNDIRECTED
orientation.
This is because the Clique Counting algorithm is defined only for undirected graphs.
MATCH (source:Person)-[r:KNOWS]->(target:Person)
RETURN gds.graph.project(
'myGraph',
source,
target,
{},
{ undirectedRelationshipTypes: ['*'] }
)
The Clique Counting algorithm requires the graph to use the UNDIRECTED orientation for relationships.
You can either create the graph with undirected relationships or update it by converting the directed relationships into new undirected ones.
|
In the following examples we will demonstrate using the Clique Counting algorithm on this graph.
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.
CALL gds.cliqueCounting.write.estimate('myGraph', { writeProperty: 'cliqueCount' })
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory
nodeCount | relationshipCount | bytesMin | bytesMax | requiredMemory |
---|---|---|---|---|
6 |
18 |
64 |
952 |
"[64 Bytes ... 952 Bytes]" |
Stream
In the stream
execution mode, the algorithm returns the clique counts 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.
stream
mode:CALL gds.cliqueCounting.stream('myGraph')
YIELD nodeId, counts
RETURN gds.util.asNode(nodeId).name AS name, counts
ORDER BY name ASC
name | counts |
---|---|
"Alice" |
[3, 1] |
"Chris" |
[4, 1] |
"Karin" |
[4, 1] |
"Mark" |
[] |
"Michael" |
[3, 1] |
"Will" |
[1] |
Here we find that the 'Chris' node is part of 4 triangles and a four-clique, while the 'Will' node is part of only a triangle. This can be verified in the example graph. Since the 'Mark' node is linked only to the 'Will' node, it is not part of any clique of size three or larger and we therefore get an empty list.
Note that with a clique of size k, you are guaranteed to have a clique also of size k-1 since any subset of a clique is also a clique.
Stats
In the stats
execution mode, the algorithm returns a single row containing a summary of the algorithm result.
The summary result contains the global clique count, which is the total number of cliques of each size in the entire graph.
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.
stats
mode:CALL gds.cliqueCounting.stats('myGraph')
YIELD globalCount
globalCount |
---|
[5, 1] |
Here we can see that the graph has six nodes with a total number of five triangles, one four-clique and no larger cliques. Comparing this to the stream example we can see that the 'Chris' node is part of almost every clique. The exception is the 'Michael-Karin-Alice'-triangle. In other words, that node is part of most cliques in the graph and thus has a very central position in the graph.
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 clique counts 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.
mutate
mode:CALL gds.cliqueCounting.mutate('myGraph', {
mutateProperty: 'cliqueCount'
})
YIELD globalCount
globalCount |
---|
[5, 1] |
The returned result is the same as in the stats
example.
Additionally, the graph 'myGraph' now has a node property cliqueCount
which stores the clique counts for each node.
To find out how to inspect the new schema of the in-memory graph, see Listing graphs.
Write
The write
execution mode extends the stats
mode with an important side effect: writing the clique counts 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.
write
mode:CALL gds.cliqueCounting.write('myGraph', {
writeProperty: 'cliqueCount'
})
YIELD globalCount
globalCount |
---|
[5, 1] |
The returned result is the same as in the stats
example.
Additionally, each of the six nodes now has a new property cliqueCount
in the Neo4j database, containing the clique counts for that node.