Exclude paths with duplicated nodes

651 views Asked by At

In my flight modelization, I would like to search for path with 1 stop which is equivalent in the graph to have a 4 hops relationships from the source to the destination. When searching the path with :

     match (s:Airport{airportName:'CAN'}),
     (d:Airport{airportName:'ICN'})
     with s,d 
     match p = (s)<-[*4]->(d)
     return  nodes(p), relationships(p) 

But this also give me path with airport node that are visited twice, like this : airport node

So my question is, how to exclude paths which contains duplicated nodes ? How to detect whether there is a duplicated node within a path ?

Thank you !

1

There are 1 answers

0
InverseFalcon On

If you have access to APOC Procedures, you can try using apoc.algo.allSimplePaths(), which will not contain any loops back to a previously visited node in the path.

 match (s:Airport{airportName:'CAN'}),
  (d:Airport{airportName:'ICN'})
 call apoc.algo.allSimplePaths(s, d, '', 4) yield path
 return  nodes(path), relationships(path)