简体   繁体   中英

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)). For this I use sack to collect edge weights along the way.

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:

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

(my code runs on Neptune with '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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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