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()