title: Gremlin > recursively find nodes connected by an edge type
body: <p>Just working with the <a href="https://github.com/tinkerpop/blueprints/wiki/Property-Graph-Model" rel="nofollow">TinkerGraph</a>, and attempting to recursively find nodes connected by a specific edge label (in this case <code>created</code>).</p>
  
  <ol>
  <li>Is there a way I can recursively(/loop) traverse nodes? In the example below, I want to loop until there are no more matching edges (instead of the hardcoded <code>3</code> value).</li>
  <li>Is there anyway to find and group connected Vertices, given a graph?</li>
  </ol>
  
  <p>Extra kudos for deduplicating nodes, and handling node loops.</p>
  
  <h3>Dependencies</h3>
  
  <pre class="lang-groovy prettyprint-override"><code>compile("com.thinkaurelius.titan:titan-berkeleyje:0.5.4")
  compile('com.tinkerpop:gremlin-groovy:2.6.0')
  </code></pre>
  
  <h3>Code (manually recurse 3 times :( )</h3>
  
  <pre class="lang-groovy prettyprint-override"><code>Gremlin.load()
  def g = TinkerGraphFactory.createTinkerGraph()
  println g.v(5).as('x')
      .both('created')
      .dedup
      .loop(2){it.loops &lt;= 3}
      .path
      .toList().flatten() as Set // groovy code to flatten &amp; dedup
  </code></pre>
  
  <h3>Gives me: (correct)</h3>
  
  <pre class="lang-groovy prettyprint-override"><code>[v[5], v[4], v[3], v[1], v[6]]
  </code></pre>
  
  <p>Thanks!</p>