6.3.5. Local Clustering Coefficient

This section describes the Local Clustering Coefficient algorithm in the Neo4j Graph Data Science library.

This topic includes:

6.3.5.1. Introduction

The Local Clustering Coefficient algorithm computes the local clustering coefficient for each node in the graph. The local clustering coefficient Cn of a node n describes the likelihood that the neighbours of n are also connected. To compute Cn we use the number of triangles a node is a part of Tn, and the degree of the node dn. The formula to compute the local clustering coefficient is as follows:

lcc formula

As we can see the triangle count is required to compute the local clustering coefficient. To do this the Triangle Count algorithm is utilised.

Additionally, the algorithm can compute the average clustering coefficient for the whole graph. This is the normalised sum over all the local clustering coefficients.

For more information, see Clustering Coefficient.

6.3.5.2. Syntax

This section covers the syntax used to execute the Local Clustering Coefficient 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 Section 6.1, “Syntax overview”.

Example 6.7. Local Clustering Coefficient syntax per mode

Run Local Clustering Coefficient in stream mode on a named graph: 

CALL gds.localClusteringCoefficient.stream(
  graphName: String,
  configuration: Map
)
YIELD
  nodeId: Integer,
  localClusteringCoefficient: Double

Table 6.192. 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 6.193. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

nodeLabels

String[]

['*']

yes

Filter the named graph using the given node labels.

relationshipTypes

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.

Table 6.194. Algorithm specific configuration
Name Type Default Optional Description

triangleCountProperty

String

n/a

Yes

Node property that contains pre-computed triangle count.

Table 6.195. Results
Name Type Description

nodeId

Integer

Node ID.

localClusteringCoefficient

Double

Local clustering coefficient.

Run Local Clustering Coefficient in stats mode on a named graph: 

CALL gds.localClusteringCoefficient.stats(
  graphName: String,
  configuration: Map
)
YIELD
  averageClusteringCoefficient: Double,
  nodeCount: Integer,
  createMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  configuration: Map

Table 6.196. 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 6.197. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

nodeLabels

String[]

['*']

yes

Filter the named graph using the given node labels.

relationshipTypes

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.

Table 6.198. Algorithm specific configuration
Name Type Default Optional Description

triangleCountProperty

String

n/a

Yes

Node property that contains pre-computed triangle count.

Table 6.199. Results
Name Type Description

averageClusteringCoefficient

Double

The average clustering coefficient.

nodeCount

Integer

Number of nodes in the graph.

createMillis

Integer

Milliseconds for creating the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the global metrics.

configuration

Map

The configuration used for running the algorithm.

Run Local Clustering Coefficient in mutate mode on a named graph: 

CALL gds.localClusteringCoefficient.mutate(
  graphName: String,
  configuration: Map
)
YIELD
  averageClusteringCoefficient: Double,
  nodeCount: Integer,
  nodePropertiesWritten: Integer,
  createMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  mutateMillis: Integer,
  configuration: Map

Table 6.200. 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 6.201. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

mutateProperty

String

n/a

no

The node property in the GDS graph to which the local clustering coefficient is written.

nodeLabels

String[]

['*']

yes

Filter the named graph using the given node labels.

relationshipTypes

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.

Table 6.202. Algorithm specific configuration
Name Type Default Optional Description

triangleCountProperty

String

n/a

Yes

Node property that contains pre-computed triangle count.

Table 6.203. Results
Name Type Description

averageClusteringCoefficient

Double

The average clustering coefficient.

nodeCount

Integer

Number of nodes in the graph.

nodePropertiesWritten

Integer

Number of properties added to the in-memory graph.

createMillis

Integer

Milliseconds for creating the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the global metrics.

mutateMillis

Integer

Milliseconds for adding properties to the in-memory graph.

configuration

Map

The configuration used for running the algorithm.

Run Local Clustering Coefficient in write mode on a named graph: 

CALL gds.localClusteringCoefficient.write(
  graphName: String,
  configuration: Map
)
YIELD
  averageClusteringCoefficient: Double,
  nodeCount: Integer,
  nodePropertiesWritten: Integer,
  createMillis: Integer,
  computeMillis: Integer,
  postProcessingMillis: Integer,
  writeMillis: Integer,
  configuration: Map

Table 6.204. 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 6.205. General configuration for algorithm execution on a named graph.
Name Type Default Optional Description

writeProperty

String

n/a

no

The node property in the Neo4j database to which the local clustering coefficient is written.

nodeLabels

String[]

['*']

yes

Filter the named graph using the given node labels.

relationshipTypes

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. Also provides the default value for 'writeConcurrency'.

writeConcurrency

Integer

value of 'concurrency'

yes

The number of concurrent threads used for writing the result to Neo4j.

Table 6.206. Algorithm specific configuration
Name Type Default Optional Description

triangleCountProperty

String

n/a

Yes

Node property that contains pre-computed triangle count.

Table 6.207. Results
Name Type Description

averageClusteringCoefficient

Double

The average clustering coefficient.

nodeCount

Integer

Number of nodes in the graph.

nodePropertiesWritten

Integer

Number of properties written to Neo4j.

createMillis

Integer

Milliseconds for creating the graph.

computeMillis

Integer

Milliseconds for running the algorithm.

postProcessingMillis

Integer

Milliseconds for computing the global metrics.

writeMillis

Integer

Milliseconds for writing results back to Neo4j.

configuration

Map

The configuration used for running the algorithm.

Anonymous graphs

It is also possible to execute the algorithm on a graph that is projected in conjunction with the algorithm execution. In this case, the graph does not have a name, and we call it anonymous. When executing over an anonymous graph the configuration map contains a graph projection configuration as well as an algorithm configuration. All execution modes support execution on anonymous graphs, although we only show syntax and mode-specific configuration for the write mode for brevity.

For more information on syntax variants, see Section 6.1, “Syntax overview”.

Run Local Clustering Coefficient in write mode on an anonymous graph: 

CALL gds.localClusteringCoefficient.write(
  configuration: Map
)
YIELD
  averageClusteringCoefficient: Double,
  nodeCount: Integer,
  nodePropertiesWritten: Integer,
  createMillis: Integer,
  computeMillis: Integer,
  writeMillis: Integer,
  configuration: Map

Table 6.208. General configuration for algorithm execution on an anonymous graph.
Name Type Default Optional Description

nodeProjection

String, String[] or Map

null

yes

The node projection used for anonymous graph creation via a Native projection.

relationshipProjection

String, String[] or Map

null

yes

The relationship projection used for anonymous graph creation a Native projection.

nodeQuery

String

null

yes

The Cypher query used to select the nodes for anonymous graph creation via a Cypher projection.

relationshipQuery

String

null

yes

The Cypher query used to select the relationships for anonymous graph creation via a Cypher projection.

nodeProperties

String, String[] or Map

null

yes

The node properties to project during anonymous graph creation.

relationshipProperties

String, String[] or Map

null

yes

The relationship properties to project during anonymous graph creation.

concurrency

Integer

4

yes

The number of concurrent threads used for running the algorithm. Also provides the default value for 'readConcurrency' and 'writeConcurrency'.

readConcurrency

Integer

value of 'concurrency'

yes

The number of concurrent threads used for creating the graph.

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 local clustering coefficient is written.

Table 6.209. Algorithm specific configuration
Name Type Default Optional Description

triangleCountProperty

String

n/a

Yes

Node property that contains pre-computed triangle count.

The results are the same as for running write mode with a named graph, see the write mode syntax above.

6.3.5.3. Examples

In this section we will show examples of running the Local Clustering Coefficient 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:

triangle count

The following Cypher statement will create the example graph in the Neo4j database: 

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'}),

  (michael)-[:KNOWS]->(karin),
  (michael)-[:KNOWS]->(chris),
  (will)-[:KNOWS]->(michael),
  (mark)-[:KNOWS]->(michael),
  (mark)-[:KNOWS]->(will),
  (alice)-[:KNOWS]->(michael),
  (will)-[:KNOWS]->(chris),
  (chris)-[:KNOWS]->(karin)

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 native projection targeting the Person nodes and the KNOWS relationships. For the relationships we must use the UNDIRECTED orientation. This is because the Local Clustering Coefficient algorithm is defined only for undirected graphs.

In the examples below we will use named graphs and native projections as the norm. However, anonymous graphs and/or Cypher projections can also be used.

The following statement will create a graph using a native projection and store it in the graph catalog under the name 'myGraph'. 

CALL gds.graph.create(
  'myGraph',
  'Person',
  {
    KNOWS: {
      orientation: 'UNDIRECTED'
    }
  }
)

The Local Clustering Coefficient algorithm requires the graph to be created using the UNDIRECTED orientation for relationships.

In the following examples we will demonstrate using the Local Clustering Coefficient algorithm on 'myGraph'.

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 Section 3.1.3, “Automatic estimation and execution blocking”.

For more details on estimate in general, see Section 3.1, “Memory Estimation”.

The following will estimate the memory requirements for running the algorithm: 

CALL gds.localClusteringCoefficient.write.estimate('myGraph', {
  writeProperty: 'localClusteringCoefficient'
})
YIELD nodeCount, relationshipCount, bytesMin, bytesMax, requiredMemory

Table 6.210. Results
nodeCount relationshipCount bytesMin bytesMax requiredMemory

6

16

288

288

"288 Bytes"

Note that the relationship count is 16 although we only created 8 relationships in the original Cypher statement. This is because we used the UNDIRECTED orientation, which will project each relationship in each direction, effectively doubling the number of relationships.

Stream

In the stream execution mode, the algorithm returns the local clustering coefficient for each node. This allows us to inspect the results directly or post-process them in Cypher without any side effects. For example, we can order the results to find the nodes with the highest local clustering coefficient.

For more details on the stream mode in general, see Section 3.3.1, “Stream”.

The following will run the algorithm in stream mode: 

CALL gds.localClusteringCoefficient.stream('myGraph')
YIELD nodeId, localClusteringCoefficient
RETURN gds.util.asNode(nodeId).name AS name, localClusteringCoefficient
ORDER BY localClusteringCoefficient DESC

Table 6.211. Results
name localClusteringCoefficient

"Karin"

1.0

"Mark"

1.0

"Chris"

0.6666666666666666

"Will"

0.6666666666666666

"Michael"

0.3

"Alice"

0.0

From the results we can see that the nodes 'Karin' and 'Mark' have the highest local clustering coefficients. This shows that they are the best at introducing their friends - all the people who know them, know each other! This can be verified in the example graph.

Stats

In the stats execution mode, the algorithm returns a single row containing a summary of the algorithm result. The summary result contains the avearage clustering coefficient of the graph, which is the normalised sum over all local clustering coefficients. 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 Section 3.3.2, “Stats”.

The following will run the algorithm in stats mode: 

CALL gds.localClusteringCoefficient.stats('myGraph')
YIELD averageClusteringCoefficient, nodeCount

Table 6.212. Results
averageClusteringCoefficient nodeCount

0.6055555555555555

6

The result shows that on average each node of our example graph has approximately 60% of its neighbours connected.

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 local clustering coefficient 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 example, using the triangle count to compute the local clustering coefficient.

For more details on the mutate mode in general, see Section 3.3.3, “Mutate”.

The following will run the algorithm in mutate mode: 

CALL gds.localClusteringCoefficient.mutate('myGraph', {
  mutateProperty: 'localClusteringCoefficient'
})
YIELD averageClusteringCoefficient, nodeCount

Table 6.213. Results
averageClusteringCoefficient nodeCount

0.6055555555555555

6

The returned result is the same as in the stats example. Additionally, the graph 'myGraph' now has a node property localClusteringCoefficient which stores the local clustering coefficient for each node. To find out how to inspect the new schema of the in-memory graph, see Section 4.1.2, “Listing graphs in the catalog”.

Write

The write execution mode extends the stats mode with an important side effect: writing the local clustering coefficient 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 Section 3.3.4, “Write”.

The following will run the algorithm in write mode: 

CALL gds.localClusteringCoefficient.write('myGraph', {
  writeProperty: 'localClusteringCoefficient'
})
YIELD averageClusteringCoefficient, nodeCount

Table 6.214. Results
averageClusteringCoefficient nodeCount

0.6055555555555555

6

The returned result is the same as in the stats example. Additionally, each of the six nodes now has a new property localClusteringCoefficient in the Neo4j database, containing the local clustering coefficient for that node.

Pre-computed Counts

By default, the Local Clustering Coefficient algorithm executes Triangle Count as part of its computation. It is also possible to avoid the triangle count computation by configuring the Local Clustering Coefficient algorithm to read the triangle count from a node property. In order to do that we specify the triangleCountProperty configuration parameter. Please note that the Local Clustering Coefficient algorithm depends on the property holding actual triangle counts and not another number for the results to be actual local clustering coefficients.

To illustrate this we make use of the Triangle Count algorithm in mutate mode. The Triangle Count algorithm is going to store its result back into 'myGraph'. It is also possible to obtain the property value from the Neo4j database using a graph projection with a node property when creating the in-memory graph.

The following computes the triangle counts and stores the result into the in-memory graph: 

CALL gds.triangleCount.mutate('myGraph', {
  mutateProperty: 'triangles'
})

The following will run the algorithm in stream mode using pre-computed triangle counts: 

CALL gds.localClusteringCoefficient.stream('myGraph', {
  triangleCountProperty: 'triangles'
})
YIELD nodeId, localClusteringCoefficient
RETURN gds.util.asNode(nodeId).name AS name, localClusteringCoefficient
ORDER BY localClusteringCoefficient DESC

Table 6.215. Results
name localClusteringCoefficient

"Karin"

1.0

"Mark"

1.0

"Chris"

0.6666666666666666

"Will"

0.6666666666666666

"Michael"

0.3

"Alice"

0.0

As we can see the results are the same as in the stream example where we did not specify a triangleCountProperty.