简体   繁体   English

在ArangoDB中找到具有最大价值的边沿之后的路径

[英]Find path following edges with greatest value in ArangoDB

Lets say, that in my graph I've got edges that have field called value. 可以说,在我的图中,我的边具有称为值的字段。 After selecting start vertex I would like to find path by always selecting the edge that has the highest value. 选择起始顶点后,我想通过始终选择具有最高值的边来查找路径。 Unfortunatly I can't figure out how to write proper query, is it possible in ArangoDB? 不幸的是,我不知道如何编写正确的查询,在ArangoDB中可以吗?

Hi i am unsure what you would like to achieve, there are two possible scenarios that i can imagine from your description: 嗨,我不确定您想要实现什么,从您的描述中可以想象出两种可能的情况:

First: Shortest Path 第一:最短路径

The use-case here is you know the starting vertex and the target vertex, and you want to find the shortest (or cheapest) path between those two. 这里的用例是您知道起始顶点和目标顶点,并且想要找到这两个顶点之间的最短(或最便宜)路径。 The built in SHORTEST_PATH ( https://docs.arangodb.com/3.1/AQL/Graphs/ShortestPath.html#shortest-path-in-aql ) feature can serve it by defining the distance attribute in the options like this: 内置的SHORTEST_PATHhttps://docs.arangodb.com/3.1/AQL/Graphs/ShortestPath.html#shortest-path-in-aql )功能可以通过在以下选项中定义distance属性来实现:

FOR v IN OUTBOUND @start TO @end @@edgeCollections OPTIONS {weightAttribute: "value", defaultWeight: 1}
  RETURN v

This will give you all vertices on the path from start to end which has the lowest some of value attributes. 这将为您提供路径中从起点到终点的所有顶点,这些顶点的某些值属性最低。 If you need the "highest value" you could copy the value and save it again with 1/value in a different field, to find the path with the fewest edges having in total the highest sum of values 如果你需要的“最高值”你可以复制的价值,并再次保存1/value在不同的领域,找到与总的最高金额为边最少的路径values

Second: Sorting of edges 第二:边缘排序

The use case is you only have the starting vertex and want to get the connected vertices, ordered by the value on the edges. 用例是您只有起始顶点并且想要获得连接的顶点,该顶点按边上的值排序。 There you can simply combine the traversal statement with a simple sort. 在那里,您可以简单地将遍历语句与简单的排序组合在一起。 ( https://docs.arangodb.com/3.1/AQL/Graphs/Traversals.html#graph-traversals-in-aql ): https://docs.arangodb.com/3.1/AQL/Graphs/Traversals.html#graph-traversals-in-aql ):

FOR v, e IN OUTBOUND @start @@edgeCollection
  SORT e.value DESC
  LIMIT 1 /* Only pick the highest one */
  REUTRN {v: v, e: e}

Third use-case: Iterating several depth only using the highest value 第三种用例:仅使用最大值迭代多个深度

The AQL in Use-case 2 can be chained up to an arbitrary depth which has to be known a-priori. 用例2中的AQL可以链接到任意深度,这必须是先验的。 So say you would like to iterate 3 steps only using the edge with highest value: 假设您只想使用具有最高值的边来迭代3个步骤:

FOR v1, e1 IN OUTBOUND @start @@edgeCollection
  SORT e1.value DESC
  LIMIT 1 /* Only pick the highest one */
  /* Depth 1 done. now depth 2*/
  FOR v2, e2 IN OUTBOUND v1 @@edgeCollection
    SORT e2.value DESC
    LIMIT 1 /* Only pick the highest one */
    FOR v3, e3 IN OUTBOUND v2 @@edgeCollection
      SORT e3.value DESC
      LIMIT 1 /* Only pick the highest one */
      RETURN [v1,v2,v3]

Forth use-case: 第四个用例:

The depth is not known a-priori, in this case pure AQL in the currently release version (3.1) cannot formulate this. 深度是先验的,在这种情况下,当前发行版(3.1)中的纯AQL无法公式化。 It will be easier to use a Foxx service ( https://docs.arangodb.com/3.1/Manual/Foxx/#foxx ) using the traversal module ( https://docs.arangodb.com/3.1/Manual/Graphs/Traversals/UsingTraversalObjects.html#getting-started ) in JavaScript which is a bit more flexible, but can only be implemented in Javascript. 通过遍历模块( https://docs.arangodb.com/3.1/Manual/Graphs/ )使用Foxx服务( https://docs.arangodb.com/3.1/Manual/Foxx/#foxx )将更加容易。 JavaScript中的Traversals / UsingTraversalObjects.html#getting-started ),虽然灵活性更高,但只能在Javascript中实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM