0

Trying to learn Neo4j, graph DB and using a test setup where i'm representing users who want to trade fruits.

Im trying to find a situation where there exists a "3 person trade" or a direct cycle between 3 or more persons in the system.

This is the scenario i'm trying to store

userA has apples , wants cherries
userB has bananas, wants apples
userC has cherries , wants bananas

So a trade is possible in the above scenario,if the 3 parties are involved in the trade. I need a query that will return the names of the traders/persons.

Need help representing this and writing the code to be able to solve this query. For the scenario, this is the cypher i'm using:

(userA)-[r:has]->(apples) (userA)-[r:wants]->(cherries)

(userB)-[r:has]->(bananas) (userB)-[r:wants]->(apples)

(userA)-[r:has]->(cherries) (userA)-[r:wants]->(bananas)

Also tried using this : find the group in Neo4j graph db , but that query didnt work ..

thanks for any info, that can help!

Dgibbs
  • 1
  • 2

1 Answers1

0

The initial approach would be something like this:

MATCH (userA:User)
WHERE (userA)-[:WANTS]->() AND (userA)-[:HAS]->()
MATCH (userA)-[:WANTS]->()<-[:HAS]-(userB)-[:WANTS]->()<-[:HAS]-(userC)-[:WANTS]->()<-[:HAS]-(userA)
RETURN DISTINCT userA, userB, userC

That said, you may need to adjust this based on how big your graph is, and how fast the query runs on your graph.

InverseFalcon
  • 29,576
  • 4
  • 38
  • 51