问题
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.
回答1:
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()
来源:https://stackoverflow.com/questions/40672466/query-to-check-if-there-is-a-cycle-in-a-graph-with-edges-visited-only-once