【问题标题】:Gremlin Python shortest path between two nodesGremlin Python 两个节点之间的最短路径
【发布时间】:2023-01-15 08:03:52
【问题描述】:

我们如何使用gremlin-python找到两个节点之间的最短路径?

为 gremlin here 给出的示例显示了以下 gremlin 查询

g.V(1).repeat(out().simplePath()).until(hasId(5)).path().
       group().by(count(local)).next()

假设源节点标签和目标节点标签已知,我如何将其转换为等效的 gremlin-python 查询?

【问题讨论】:

    标签: gremlin tinkerpop janusgraph gremlinpython


    【解决方案1】:

    Gremlin 查询变为

    g.V().hasLabel(<label1>).
          repeat(out().simplePath()).
          until(hasLabel(<label2>)).
          path().
          group().
            by(count(local)).
          next()
    

    group...by 并不是完全需要的,因为返回的路径将按最短路径顺序(按深度)即 BFS,除非使用其他一些权重来查找和排序它们。

    在“完全合格”的 Gremlin Python 中(如果您导入静态等,则不需要)查询变为:

    result = (g.V().hasLabel('label1').
                repeat(__.out().simplePath()).
                until(__.hasLabel('label2')).
                path().
                group().
                  by(__.count(Scope.local)).
                next())
    

    【讨论】:

    • 谢谢!第一个查询返回一个 GraphTraversal 对象,有没有一种简单的方法可以找到其中的节点和边数?似乎 .to_list() 在无法调用的对象上抛出错误。
    • 答案中的第一个查询还是问题中的第一个查询?使用 Python 时,“对象不可调用”错误几乎总是意味着您与 Python 保留字发生冲突。
    • 当在末尾附加 to_list() 时,答案中的第一个查询给出 TypeError: 'GraphTraversal' object is not callable
    • 它是toList()而不是to_list(),它将取代next()
    • 在较大的图形上使用此查询时,会出现以下错误:GremlinServerError: 598: A timeout occurred during traversal evaluation of [RequestMessage{, requestId=cc2be6b9-03aa-438c-b960-4491fc969202, op='bytecode', processor='traversal', args={gremlin=[[withStrategies(OptionsStrategy)], [V(), hasLabel(label1), repeat([[], [out(), simplePath()]]), until([[], [hasLabel(label2)]]), path(), group(), by([[], [count(local)]])]], aliases={g=g}, scriptEvaluationTimeout=20}}] - consider increasing the limit given to evaluationTimeout
    猜你喜欢
    • 2016-03-04
    • 1970-01-01
    • 2021-12-18
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 2019-04-20
    相关资源
    最近更新 更多