title: How to avoid cycle or loop in path Neo4j Cypher
body: <p>I have a problem when i use the path variables. </p>
  
  <p>For example my hierarchy looks like </p>
  
  <pre><code>A-[Knows]-&gt; B   
  B-[Knows]-&gt; C  
  C-[Knows]-&gt; D  
  D-[Knows]-&gt; E  
  C-[Knows]-&gt; F  
  F-[Knows]-&gt; G  
  G-[Knows]-&gt; B 
  </code></pre>
  
  <p>My query is <code>MATCH p = (x)&lt;-[Knows*]-&gt;(y) Return p</code></p>
  
  <p>The relation direction have to be bidirectional. Toy problem is like A,B but in real application, i don't know direction of relations.</p>
  
  <p>So there is a cycle and loop. The path start to find from A to C. But after finding C, next steps, cycle is happened. </p>
  
  <p>How to avoid the loop or how to ignore it, how to remove from actual path or how to stop when the path end in B. I don't need to find second B to C.</p>
  
  <p>Expected result : </p>
  
  <pre><code>A-&gt;B-&gt;C-&gt;D-&gt;E
  A-&gt;B-&gt;C-&gt;F-&gt;G-&gt;B  // that is enough. The query must be stopped.
  </code></pre>
  
  <p>Actual result : </p>
  
  <pre><code>A-&gt;B-&gt;C-&gt;D-&gt;E
  A-&gt;B-&gt;C-&gt;F-&gt;G-&gt;B-&gt;C-&gt;F-&gt;G-&gt;B..... loop trouble.
  </code></pre>