Tinkerpop 3:使用Gremlin遍历计算连接组件

3

我认为标签已经很好地解释了我的问题 :)

我一直在尝试编写Gremlin遍历来计算帖子末尾描述的简单图的连通组件。

我尝试了:

g.V().repeat(both('e')).until(cyclicPath()).dedup().tree().by('name').next()

获取
==>a={b={a={}, c={b={}}, d={c={d={}}}}, c={d={c={}}}}
==>e={f={e={}, g={f={}}}, h={f={h={}}}}
==>g={f={g={}}}

这很糟糕,因为cyclicPath过滤器在到达g之前就终止了从e开始的遍历。显然,如果我删除until子句,我将得到一个无限循环。 此外,如果我使用simplePath,遍历将在一步后结束。 有没有办法告诉它按深度优先顺序探索节点呢?

干杯!

a = graph.addVertex(T.id, 1, "name", "a")
b = graph.addVertex(T.id, 2, "name", "b")
c = graph.addVertex(T.id, 3, "name", "c")
d = graph.addVertex(T.id, 4, "name", "d")
e = graph.addVertex(T.id, 5, "name", "e")
f = graph.addVertex(T.id, 6, "name", "f")
g = graph.addVertex(T.id, 7, "name", "g")
h = graph.addVertex(T.id, 8, "name", "h")

a.addEdge("e", b)
a.addEdge("e", c)
b.addEdge("e", c)
b.addEdge("e", d)
c.addEdge("e", d)

e.addEdge("e", f)
e.addEdge("e", h)
f.addEdge("e", h)
f.addEdge("e", g)
2个回答

1
这个查询也在Gremlin-users组中讨论过。这是我想出来的解决方案。@Daniel Kuppitz也有一个有趣的解决方案,你可以在提到的主题中找到。
我认为,如果在无向图中,遍历连通分量的“最后”节点总是导致先前访问过的节点(cyclicPath())或度数<=1,则此查询应该有效。 g.V().repeat(both('e')).until(cyclicPath().or().both('e').count().is(lte(1))).dedup().tree().by('name').next() 在我的示例中,它给出了以下输出
gremlin>  g.V().repeat(both('e')).until(cyclicPath().or().both('e').count().is(lte(1))).dedup().tree().by('name').next()
==>a={b={a={}, c={b={}}, d={c={d={}}}}, c={d={c={}}}}
==>e={f={e={}, g={}, h={f={}}}, h={f={h={}}}}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接