只需使用TinkerGraph,并试图递归地查找由特定边缘标签连接的节点(在本例中为created)。
3值)。对于去重复节点和处理节点循环的额外荣誉。
依赖关系
compile("com.thinkaurelius.titan:titan-berkeleyje:0.5.4")
compile('com.tinkerpop:gremlin-groovy:2.6.0')代码(手动递归3次:( )
Gremlin.load()
def g = TinkerGraphFactory.createTinkerGraph()
println g.v(5).as('x')
.both('created')
.dedup
.loop(2){it.loops <= 3}
.path
.toList().flatten() as Set // groovy code to flatten & dedup给我:(正确)
[v[5], v[4], v[3], v[1], v[6]]谢谢!
发布于 2015-04-02 00:28:54
您不需要任何Groovy代码,只需要使用Gremlin:
gremlin> g.v(5).as('x').both('created').dedup()
gremlin> .loop('x') {true} {true}.dedup()
==>v[4]
==>v[3]
==>v[5]
==>v[6]
==>v[1]发布于 2015-04-02 00:27:27
这是我目前的解决方案。这是一项正在进行的工作,所以我非常乐意得到改进和建议。(当然可以使用Gremlin语法对其进行优化?)
假设:,给我们一个开始节点
Gremlin.load()
def g = TinkerGraphFactory.createTinkerGraph()
def startV = g.v(5)
def seen = [startV] // a list of 'seen' vertices
startV.as('x')
.both('created')
.filter { // only traverse 'unseen' vertices
def unseen = !seen.contains(it)
if (unseen){
seen << it
}
unseen
}
.loop('x'){
// continue looping while there are still more 'created' edges...
it.object.both('created').hasNext() // ##
}
.toList() // otherwise won't process above pipeline
println seen##我不知道为什么这个条件有效/没有找到以前遍历过的边。有人能解释吗?
给我:
[v[4], v[5], v[3], v[1], v[6]]https://stackoverflow.com/questions/29384300
复制相似问题