Neo4j: find degree of connection

2019-08-09 05:44发布

问题:

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!

回答1:

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?

I believe that run shortestPath() for every pair of users is a good choice, but keep in mind that it should be very expensive.

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)?

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 to b and b to a. You only need an extra relationship when some data in the bidirectional relationship can be different between a to b and b to a. Suppose that the interaction between the users in your model has a weight and this weight can be different from a to b and b to a. In this case you can store this weight as a property in the relationship. Example:

(a)-[:INTERACTS_WITH {weight:10}]->(b)
(b)-[:INTERACTS_WITH {weight:6}]->(a)

Take a look in this link about modelling bidirectional relationships.