I'm using Neo4j to find the degree of connection between users. I have data in the follower shape:
(user)-[:INTERACTS_WITH]->(user)
So if user_1 interact with user_2 and user_2 interacts with user_3, then user_1 and user_3 share a second-degree connection.
Ideally, I would like to get the following dataset like this in return:
degree count
NULL 123
1 1050
2 3032
3 2110
... ...
Is there a better way to do this than to simply run shortestPath() function for every pair of users? If not, then what is the best way to loop over users in Neo4j?
Also, I imagine the direction plays a role here, so would you recommend making this relationship bidirectional, so that for every (user1)-[:INTERACTS_WITH]->(user2)
I would also create the reverse relationship (user2)-[:INTERACTS_WITH]->(user1)
?
If you have any tips on how to create the above dataset, please let me know.
Many thanks!
I believe that run
shortestPath()
for every pair of users is a good choice, but keep in mind that it should be very expensive.No, you do not need another relationship. Remember that relationship direction can be ignored at query time in Neo4j. When modeling relationships that are naturally bidirectional we should use only one relationship to represent it. So when querying the graph we can transverse from
a
tob
andb
toa
. You only need an extra relationship when some data in the bidirectional relationship can be different betweena
tob
andb
toa
. Suppose that the interaction between the users in your model has a weight and this weight can be different froma
tob
andb
toa
. In this case you can store this weight as a property in the relationship. Example:Take a look in this link about modelling bidirectional relationships.