最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

database - How to find co-connected nodes with 'k' relationships? - Neo4j - Stack Overflow

matteradmin4PV0评论

I'm having a large Neo4j database, below is the small subset of nodes and relationships.

I have 3 different kind of nodes:

  • (Pink) Customers
  • (Blue) Transactions
  • (Yellow) Terminals

It's easy to find the pattern below using Cypher, by just indicating the pattern using MATCH.

c1 -[:MADE]-> transaction1 -[:AT]-> terminal <-[:AT]- transaction2 <-[:MADE]- c2

We consider that this means that c2 node, is a 2nd connection degree neighbour to c1.

But this could be done simply for connection degree of 2. How can I find kth connection degree neighbours to a given customer node, for k>2 ? I'm looking for only Customer neigbour nodes.

I'm using Python to execute my cypher queries in neo4j. I know I can maybe append the pattern k times in my query string to achieve this goal, but I want to know if is it possible to just indicate this in one specific cypher query.

If there's any solutions that would let me choose k explicitly in my cypher query, that would be great. I couldn't find anything like that.

Update: I would appreciate a query which can show me the path with the relationships as well, as I would like to check manually and make sure the path is a correct one.

I'm having a large Neo4j database, below is the small subset of nodes and relationships.

I have 3 different kind of nodes:

  • (Pink) Customers
  • (Blue) Transactions
  • (Yellow) Terminals

It's easy to find the pattern below using Cypher, by just indicating the pattern using MATCH.

c1 -[:MADE]-> transaction1 -[:AT]-> terminal <-[:AT]- transaction2 <-[:MADE]- c2

We consider that this means that c2 node, is a 2nd connection degree neighbour to c1.

But this could be done simply for connection degree of 2. How can I find kth connection degree neighbours to a given customer node, for k>2 ? I'm looking for only Customer neigbour nodes.

I'm using Python to execute my cypher queries in neo4j. I know I can maybe append the pattern k times in my query string to achieve this goal, but I want to know if is it possible to just indicate this in one specific cypher query.

If there's any solutions that would let me choose k explicitly in my cypher query, that would be great. I couldn't find anything like that.

Update: I would appreciate a query which can show me the path with the relationships as well, as I would like to check manually and make sure the path is a correct one.

Share Improve this question edited Nov 19, 2024 at 10:27 Holyamirali asked Nov 18, 2024 at 18:59 HolyamiraliHolyamirali 366 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

To get the paths to Customer nodes that are, for example, four degrees away from a Customer node with id = 'c1', you can use the following:

MATCH p =
      (c1:Customer {id: 'c1'})
      (()-[:MADE]->(:Transaction)-[:AT]->(:Terminal)
         <-[:AT]-(:Transaction)<-[:MADE]-(:Customer)){4} (c2)
WHERE c1 <> c2
RETURN p

Note here what you refer to as 2nd degree connection would here be 1st.

That solution ignores the fact that there may be multiple paths of varying length that connect a given pair of nodes. To only return the minimum degree for each Customer node, find the shortest path between each pair:

MATCH p = SHORTEST 1
      (c1:Customer {id: 'c1'})
      (()-[:MADE]->(:Transaction)-[:AT]->(:Terminal)
         <-[:AT]-(:Transaction)<-[:MADE]-(:Customer)){4} (c2)
WHERE c1 <> c2
RETURN p

These solutions use quantified path patterns and shortest paths.

Post a comment

comment list (0)

  1. No comments so far