0
votes

I have a somewhat unique scenario. I am going to get a node A. From node A, I want to find path to some other nodes (say set C of nodes). Once I get these set of paths I want to rerun those paths from the other node B (which corresponds/similar to node A) to discover all nodes (which corresponds to set C of nodes) which follow same paths (as from node A to nodes in set C).

For example if for (A) I have got (E),(G) following these paths:

   ---[:Rel2]--->(F)---[:Rel1]--->(G)
  /
(A)---[:Rel1]--->(E)

Then for (B), I should get (J),(H) following the same paths, but should not get (K) as it does not follow any of the path patterns returned for (A):

   ---[:Rel2]--->(I)---[:Rel1]--->(J)    
  /
(B)---[:Rel1]--->(H)
  \
   ---[:Rel2]--->(K)

As of now, I am doing it as follows:

  • Given A, run the initial cypher to get the desired paths.
  • The paths are returned as instances of org.neo4j.driver.v1.types.Path. Each Path instance is essentially a list of org.neo4j.driver.v1.types.Path.Segment instances. So iterate over Segment list to form cypher string for each corresponding path.
  • Use the cypher strings to do MATCH from B.

This is working, however, forming cypher string from the Java lists of Segment classes gives me a weird feeling. I dont know if this approach is correct or not. Is there any other better looking approach? Or is this approach just ok? Is there any recommended approach? Have anyone came across similar scenario?

Edit: You may view first graph as a part of ("part" because it does not have schema definition corresponding to instance subpath (B)---[:Rel2]--->(K)) a schema and second graph as an instance of that schema. We first find the path in schema and then run it on instance.

1

1 Answers

0
votes

I haven't come across this kind of requirement before, but I think it falls in the category of graph isomorphism. Neither Neo4j nor APOC have anything for this right now, or for taking elements of a path and reapplying them to a different node.

For now, this sounds like a job for a custom procedure, which is what you're probably doing.

Otherwise, there are ways in Cypher to filter paths to conform to relationships defined by a collection of relationship types, but I don't believe that will be evaluated during path expansion, so it may not be efficient on busier graphs.