2

How to write an query runs on my graph, that outputs 'false' if there is NO path traversing through each edge only once and return to the starting point.

Filipe Teixeira
  • 3,565
  • 1
  • 25
  • 45
  • This boils down to looping over all vertexes and returning true if the total number of both().count() % 2 == 0 is 0 or 2. I'm still noodling the traversal to express that. – Paul Jackson Nov 18 '16 at 13:49
  • Ye. The traversal is the trickiest part. Still trying to get it. –  Nov 19 '16 at 03:40

1 Answers1

6

I was using the following sample graph:

g = TinkerGraph.open().traversal()
g.addV().property(id, 'blue').as('b').
  addV().property(id, 'orange').as('o').
  addV().property(id, 'red').as('r').
  addV().property(id, 'white').as('w').
  addE('bridge').from('w').to('b').
  addE('bridge').from('w').to('o').
  addE('bridge').from('w').to('r').
  addE('bridge').from('w').to('r').
  addE('bridge').from('o').to('b').
  addE('bridge').from('o').to('b').
  addE('bridge').from('o').to('r').
  addE('bridge').from('o').to('r').iterate()

The following query returns the first possible path:

gremlin> g.V().sideEffect(outE("bridge").aggregate("bridges")).barrier().
......1>   repeat(bothE().or(__.not(select('e')),
......2>                     __.not(filter(__.as('x').select(all, 'e').unfold().where(eq('x'))))).as('e').otherV()).
......3>     until(select(all, 'e').count(local).as("c").select("bridges").count(local).where(eq("c"))).limit(1).
......4>   path().by(id).by(constant(" -> ")).map {String.join("", it.get().objects())}
==>orange -> blue -> white -> orange -> red -> white -> red -> orange -> blue

If all you need is a boolean value, then just append .hasNext():

g.V().sideEffect(outE("bridge").aggregate("bridges")).barrier().
  repeat(bothE().or(__.not(select('e')),
                    __.not(filter(__.as('x').select(all, 'e').unfold().where(eq('x'))))).as('e').otherV()).
    until(select(all, 'e').count(local).as("c").select("bridges").count(local).where(eq("c"))).hasNext()
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34