【问题标题】:Gremlin All shortest paths from multiple vertices to a single vertexGremlin 从多个顶点到单个顶点的所有最短路径
【发布时间】:2019-02-14 10:22:19
【问题描述】:

以下堆栈溢出问题

How to increase performance of shortest path using Gremlin?

展示了如何找到从id为687的单个起始顶点到id为1343的结束顶点的最短路径,并通过使用storewithout确保没有重复路径来有效地做到这一点,和aggregate

g.V(687).store('x').repeat(out().where(without('x')).aggregate('x')).until(hasId(1343)).limit(1).path()

我想以相同的效率执行相同的查询,但是我需要从具有相同标签的多个起始顶点到相同结束顶点的所有最短路径,例如它看起来像这样(尽管这不起作用)

g.V().hasLabel('label').store('x').repeat(out().where(without('x')).aggregate('x')).until(hasId(1343)).limit(1).path()

我尝试了多个在语句中重复两次的构造,但无法为每个起始顶点获得独立的store('x')。我也在使用AWS Neptune 平台,因此它限制了在不允许循环/脚本的情况下使用 Gremlin。所有 gremlin 查询必须以 g. 开头,并且由与 . 链接在一起的命令组成

https://docs.aws.amazon.com/neptune/latest/userguide/access-graph-gremlin-differences.html

【问题讨论】:

    标签: gremlin tinkerpop tinkerpop3 amazon-neptune


    【解决方案1】:

    此技术不能应用于多个起始顶点。但是,您可以从另一侧开始,因为这是一个已知顶点:

    g.V(1343).store('x').
      repeat(__.in().where(without('x')).aggregate('x')).
        until(hasLabel('label')).
      path()
    

    如果一个起始顶点可以是另一个起始顶点路径的一部分,那么您可能不会在潜在起始顶点处中断,而是这样做:

    g.V(1343).store('x').
      repeat(__.in().where(without('x')).aggregate('x')).
        emit(hasLabel('label')).
      path()
    

    【讨论】:

    • 这是否不适用于多个起始顶点,因为 BFS 在每次遍历时并行发生?
    • 有点。如果您在一条路径上击中任何顶点 X,则会将其添加到排除列表中 ('x');然后,如果您稍后在另一条路径上再次点击该顶点,则遍历器将不再遵循该路径,因此他们可能找不到到达目标顶点的最短(或任何)路径。
    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2013-10-25
    相关资源
    最近更新 更多