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
