简体   繁体   English

添加不同路径的Sack值

[英]add Sack value of different paths

I have a graph with one start-node and two goal-vetices.我有一个包含一个起始节点和两个目标节点的图表。 Two paths lead to the first goal, another path to the second.两条路通向第一个目标,另一条路通向第二个目标。 I want to find all paths to all goals and collect their weight (sack(sum)).我想找到所有目标的所有路径并收集它们的重量(sack(sum))。 For this I use sack to collect edge weights along the way.为此,我使用 sack 沿途收集边缘权重。

data:数据:

g.addV('start').property(id, 'v0').
  addV('road').property(id, 'v1').
  addV('road').property(id, 'v2').
  addV('goal').property(id, 'v3').
  addV('road').property(id, 'v4').
  addV('road').property(id, 'v5').
  addV('road').property(id, 'v6').
  addV('goal').property(id, 'v7').
  addE('link').property('weight', 0.4).from(V('v0')).to(V('v1')).
  addE('link').property('weight', 0.4).from(V('v1')).to(V('v2')).
  addE('link').property('weight', 0.4).from(V('v2')).to(V('v3')).
  addE('link').property('weight', 0.5).from(V('v0')).to(V('v5')).
  addE('link').property('weight', 0.5).from(V('v5')).to(V('v4')).
  addE('link').property('weight', 0.5).from(V('v4')).to(V('v3')).
  addE('link').property('weight', 0.7).from(V('v0')).to(V('v6')).
  addE('link').property('weight', 0.4).from(V('v6')).to(V('v7'))

query:询问:

g.withSack(1.0f).V('v0')
    .repeat(
        outE().sack(mult).by('weight')
        .inV()
    ).until(hasLabel('goal'))
    .order().by(sack(), desc)
    .limit(20)
    .project('sack', 'id', 'edge-weight')
        .by(sack())
        .by(id)
        .by(path())

result:结果:

{'sack': 0.28, 'id': 'v7', 'edge-weight': path[v[v0], e[e6][v0-link->v6], v[v6], e[e7][v6-link->v7], v[v7]]}
{'sack': 0.125, 'id': 'v3', 'edge-weight': path[v[v0], e[e3][v0-link->v5], v[v5], e[e4][v5-link->v4], v[v4], e[e5][v4-link->v3], v[v3]]}
{'sack': 0.064, 'id': 'v3', 'edge-weight': path[v[v0], e[e0][v0-link->v1], v[v1], e[e1][v1-link->v2], v[v2], e[e2][v2-link->v3], v[v3]]}

so far so good!到目前为止,一切都很好!

now I want to add all values that end at the same goal-vertex and sort the goal-vertices by this sum.现在我想添加所有以相同目标顶点结尾的值,并按此总和对目标顶点进行排序。

There is no need to keep the path, its just for demonstration puroses.无需保留路径,仅用于演示目的。 Also, of course, each vertex should only appear once.当然,每个顶点也应该只出现一次。

how do I accomplish that?我该怎么做?

ideal output:理想 output:

{'sum': 0.28, 'id': 'v7'}
{'sum': 0.189, 'id': 'v3'}

(my code runs on Neptune with 'gremlin': {'version': 'tinkerpop-3.4.11'}) (我的代码在 Neptune 上运行 'gremlin': {'version': 'tinkerpop-3.4.11'})

Your query is pretty close to the answer.您的查询非常接近答案。 If you don't need path, you can just group them by vertices and sum the sack value.如果您不需要路径,您可以按顶点对它们进行分组并对 sack 值求和。

gremlin>  g.withSack(1.0f).
......1>   V('v0').
......2>   repeat(outE().sack(mult).by('weight').inV()).until(hasLabel('goal')).
......3>   group().by(id()).by(sack().sum()).
......4>   unfold().
......5>   order().by(values, desc)
==>v7=0.280
==>v3=0.1890

If you want answer to be exact in the format you need you can add project at the end as well.如果您希望答案以您需要的格式准确无误,您也可以在最后添加项目。

gremlin>  g.withSack(1.0f).
......1>   V('v0').
......2>   repeat(outE().sack(mult).by('weight').inV()).until(hasLabel('goal')).
......3>   group().by(id()).by(sack().sum()).
......4>   unfold().
......5>   order().by(values, desc).
......6>   project('sum', 'id').by(select(values)).by(select(keys))
==>[sum:0.280,id:v7]
==>[sum:0.1890,id:v3]

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

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