liveBook · Manning

Clustering coefficient

As explained in Graph Algorithms for Data Science

The clustering coefficient of common neighbors is a link-prediction metric that measures the proportion of possible connections among the common neighbors of a node pair that actually exist.

The clustering coefficient of common neighbors scores a candidate link by measuring how tightly its two endpoints’ shared neighbors connect. Values range from 0 to 1: 0 means no links among the common neighbors, while 1 means they form a complete graph. section 10.3.5

Calculate the score by following these steps:

  • Identify the common neighbors of the two candidate nodes. section 10.3.5
  • Count the existing links between those common neighbors. section 10.3.5
  • Divide existing links by the number of possible links; with fewer than two common neighbors, assign 0. section 10.3.5
  • Treat relationships as undirected when appropriate, reducing the possible-link count by half compared with a directed network. section 10.3.5

Scoring candidate links

A Cypher query matches candidate pairs, collects distinct shared feature neighbors, counts relationships among those neighbors, computes the ratio, and stores it as a relationship property named clusteringCoefficient. section 10.3.5

Listing 10.7 Calculating the clustering coefficient of common neighbors between pairs of nodes in the train and test sets
run_query("""
MATCH (s1:Stream)-[r:TEST_TRAIN|NEGATIVE_TEST_TRAIN]->(s2:Stream)
OPTIONAL MATCH (s1)-[:FEATURE_REL]-(neighbor)-[:FEATURE_REL]-(s2)
WITH r, collect(distinct neighbor) AS commonNeighbors,
        count(distinct neighbor) AS commonNeighborCount             #1
OPTIONAL MATCH (x)-[cr:FEATURE_REL]->(y)                            #2
WHERE x IN commonNeighbors AND y IN commonNeighbors
WITH r, commonNeighborCount, count(cr) AS commonNeighborRels
WITH r, CASE WHEN commonNeighborCount < 2 THEN 0 ELSE               #3
  toFloat(commonNeighborRels) / (commonNeighborCount *
                 (commonNeighborCount - 1) / 2) END as clusteringCoefficient
SET r.clusteringCoefficient = clusteringCoefficient                 #4
“””)

Inspect the complete Cypher calculation for candidate pairs in training and test sets. section 10.3.5

How Graph Algorithms for Data Science covers it

How deep it goes
Explains the ratio and implements it in Cypher, while treating relationships as undirected rather than developing directed-network details.
Examples
Uses candidate Stream pairs, shared feature neighbors, and a Cypher query that stores relationship scores.
What it assumes
Assumes graph relationships and candidate pairs, while defining common neighbors and the coefficient’s range directly.
What you can do after
Apply it via Cypher to compute and store clustering scores for candidate links.
How it connects
Connects link prediction with common neighbors, local clustering coefficient, feature relationships, and undirected graphs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Algorithms for Data Science. The section links point to the parts of the book it draws on.