title: Recursive Query with Cypher on a Neo4j Graph DB
body: <p>Is there a way to write recursive queries in Cypher?
  I have to traverse all the paths from a set of nodes(with a label <code>L</code>) to another set of nodes<code>(m, n, o, ...)</code>. The Graph is directional and there are multiple paths like </p>
  
  <pre><code>&gt; (m, n, o, p, ...) -[*]-&gt; ({:L})
  </code></pre>
  
  <p>The recursive query has to outline this pseudo code</p>
  
  <pre><code>ForEach x where x HasLabel l:
      rec(x)
  Def rec(node x):
      If x in (m, n, o, p):
           return
      ForEach y where y--&gt;x:
           rec(y)
           x.prop = f(y.prop, x.prop)   
           //f(a,b) is a trivial arithmetic function with subtraction and division.
  </code></pre>
  
  <p>The solution to this need not be a recursive one. Any other solution will be appreciated.</p>
  
  <hr>
  
  <p>EDIT:
  Before the query begins, <code>m.prop, n.prop, ..</code> are set to some values as required. And all other nodes(say <code>x</code>) in the graph have their <code>x.prop = 0</code>.</p>
  
  <p>There are multiple directed paths as <code>(m,n,o,..)-[*]-&gt;({L})</code>.
  For every node <code>x</code> in these paths where <code>(m,n,o,..)-[*]-&gt;y--&gt;x-[*]-&gt;({:L})</code> I need,
      <code>x.prop = x.prop + y.prop.</code></p>
  
  <p>This is a way to propagate a property from <code>(m,n,o)</code> to <code>({:L})</code>.</p>
  
  <hr>
  
  <p>Example: </p>
  
  <pre><code>a--&gt;b--&gt;c--&gt;d:L
      ^
      |
  e--&gt;f--&gt;g:L
  </code></pre>
  
  <p>In the beginning, <code>a.prop = 1, e.prop = 2</code> and all other nodes have <code>prop</code> as <code>0</code> i.e. <code>b.prop = f.prop = c.prop = d.prop = g.prop = 0</code></p>
  
  <pre><code>Step 1: f.prop = 2
  Step 2: b.prop = 1+2 = 3
  Step 3: c.prop = 3, g.prop = 2
  Step 4: d.prop = 3
  </code></pre>
  
  <p>This routine is easy when defined recursively at each node <code>x</code> as
  <code>x.prop += (y.prop + z.prop + ...)</code> where <code>y--&gt;x, z--&gt;x, ...</code></p>
  
  <p>I hope this clears up the confusion.</p>
  
  <hr>
  
  <p>EDIT:
  I wrote a query using paths before posting on stackoverflow. But, there could be a logical error or problem with foreach loop. This might help in coming up with the solution.</p>
  
  <pre><code>MATCH p=((u)-[*]-&gt;(v:L{}))
  MATCH (x)-[]-&gt;(u)
  WHERE x.id in ['m','n','o']
  FOREACH(n in nodes(p) | set n.marked=x.marked+n.marked)
  </code></pre>
  
  <hr>
  
  <p>EDIT: Caught the problem with my query. The above query sets the property correctly for the nodes in the end labelled as 'L'. But, does not set the property correctly for the nodes in the path in scenarios like this</p>
  
  <pre><code>    c--&gt;d:L
      ^   
      |
  a--&gt;b--&gt;e:L
  </code></pre>
  
  <p>If my query starts with <code>a.marked=1</code>, after finishing it sets <code>b.marked=2</code>. Because <code>b</code> is in two paths leading to nodes with label '<code>L</code>'. But, <code>b.marked=1</code> is the expected value as it is connected to node, <code>a</code> via one path only.</p>