【问题标题】:Tinkerpop 3: compute connected components with Gremlin traversalTinkerpop 3:使用 Gremlin 遍历计算连接组件
【发布时间】:2015-11-24 13:58:52
【问题描述】:

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

我一直在尝试编写 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)

【问题讨论】:

    标签: gremlin connected-components tinkerpop3


    【解决方案1】:

    此查询was also discussed in the Gremlin-users group。 这是我提出的解决方案。 @Daniel Kuppitz 还有一个有趣的解决方案,您可以在上述线程中找到。

    我认为,如果在无向图中始终正确,则连接组件遍历的“最后一个”节点要么导致先前访问的节点 (cyclicPath()),要么度数

    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={}}}}
    

    【讨论】:

      【解决方案2】:

      只是为了增强运行良好的@Alberto 版本,您可以使用simplePath() 遍历步骤(http://tinkerpop.apache.org/docs/current/reference/#simplepath-step)来确保遍历器不会在图形中重复其路径

      g.V().repeat(both().simplePath())
        .until(bothE().count().is(lte(1)))
        .dedup().tree().by('name').next()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-15
        • 1970-01-01
        • 2019-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-21
        相关资源
        最近更新 更多