7.6.6. The Total Neighbors algorithm

This section describes the Total Neighbors algorithm in the Neo4j Labs Graph Algorithms library.

Total Neighbors computes the closeness of nodes, based on the number of unique neighbors that they have. It is based on the idea that the more connected a node is, the more likely it is to receive new links.

The Total Neighbors algorithm was developed by the Neo4j Labs team and is not officially supported.

This section includes:

7.6.6.1. History and explanation

Total Neighbors is computed using the following formula:

total neighbors

where N(x) is the set of nodes adjacent to x, and N(y) is the set of nodes adjacent to y.

A value of 0 indicates that two nodes are not close, while higher values indicate nodes are closer.

The library contains a function to calculate the closeness between two nodes.

7.6.6.2. Total Neighbors algorithm sample

The following will create a sample graph: 

MERGE (zhen:Person {name: "Zhen"})
MERGE (praveena:Person {name: "Praveena"})
MERGE (michael:Person {name: "Michael"})
MERGE (arya:Person {name: "Arya"})
MERGE (karin:Person {name: "Karin"})

MERGE (zhen)-[:FRIENDS]-(arya)
MERGE (zhen)-[:FRIENDS]-(praveena)
MERGE (praveena)-[:WORKS_WITH]-(karin)
MERGE (praveena)-[:FRIENDS]-(michael)
MERGE (michael)-[:WORKS_WITH]-(karin)
MERGE (arya)-[:FRIENDS]-(karin)

The following will return the Total Neighbors score for Michael and Karin: 

MATCH (p1:Person {name: 'Michael'})
MATCH (p2:Person {name: 'Karin'})
RETURN algo.linkprediction.totalNeighbors(p1, p2) AS score

Table 7.173. Results
score

4.0

We can also compute the score of a pair of nodes, based on a specific relationship type.

The following will return the Total Neighbors score for Michael and Karin based only on the FRIENDS relationship: 

MATCH (p1:Person {name: 'Michael'})
MATCH (p2:Person {name: 'Karin'})
RETURN algo.linkprediction.totalNeighbors(p1, p2, {relationshipQuery: "FRIENDS"}) AS score

Table 7.174. Results
score

2.0

7.6.6.3. Syntax

The following will run the algorithm and return the result: 

RETURN algo.linkprediction.totalNeighbors(node1:Node, node2:Node, {
    relationshipQuery: null,
    direction: "BOTH"
})

Table 7.175. Parameters
Name Type Default Optional Description

node1

Node

null

no

A node

node2

Node

null

no

Another node

relationshipQuery

String

null

yes

The relationship type used to compute similarity between node1 and node2

direction

String

BOTH

yes

The direction of relationship type used to compute similarity between node1 and node2