7.6.5. The Same Community algorithm

This section describes the Same Community algorithm in the Neo4j Graph Algorithms library.

Same Community is a way of determining whether two nodes belong to the same community. These communities could be computed by using one of the Chapter 6, Community detection algorithms.

The Same Community algorithm is experimental and not officially supported.

This section includes:

7.6.5.1. History and explanation

If two nodes belong to the same community, there is a greater likelihood that there will be a relationship between them in future, if there isn’t already.

A value of 0 indicates that two nodes are not in the same community. A value of 1 indicates that two nodes are in the same community.

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

7.6.5.2. Same Community algorithm sample

The following will create a sample graph: 

MERGE (zhen:Person {name: "Zhen"}) SET zhen.community = 1
MERGE (praveena:Person {name: "Praveena"}) SET praveena.community=2
MERGE (michael:Person {name: "Michael"}) SET michael.community = 1

MERGE (arya:Person {name: "Arya"}) SET arya.partition = 5
MERGE (karin:Person {name: "Karin"}) SET karin.partition = 5

MERGE (jennifer:Person {name: "Jennifer"})

The following will indicate that Michael and Zhen belong to the same community: 

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

Table 7.139. Results
score

1.0

The following will indicate that Michael and Praveena do not belong to the same community: 

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

Table 7.140. Results
score

0.0

If one of the nodes doesn’t have a community, this means it doesn’t belong to the same community as any other node.

The following will indicate that Michael and Jennifer do not belong to the same community: 

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

Table 7.141. Results
score

0.0

By default, the community is read from the community property, but it is possible to explicitly state which property to read from.

The following will indicate that Arya and Karin belong to the same community, based on the partition property: 

MATCH (p1:Person {name: 'Arya'})
MATCH (p2:Person {name: 'Karin'})
RETURN algo.linkprediction.sameCommunity(p1, p2, 'partition') AS score

Table 7.142. Results
score

1.0

7.6.5.3. Syntax

The following will run the algorithm and return the result: 

RETURN algo.linkprediction.sameCommunity(node1:Node, node2:Node, communityProperty:String)

Table 7.143. Parameters
Name Type Default Optional Description

node1

Node

null

no

A node

node2

Node

null

no

Another node

communityProperty

String

'community'

yes

The property that contains the community to which nodes belong