计算路径问题的权重

时间:2018-01-22 14:20:28

标签: graph-databases gremlin tinkerpop3 janusgraph

我们在DataSource下方,我们有以下脚本。

robin    = graph.addVertex(label,'person','name','robin');
jeff     = graph.addVertex(label,'person','name','jeff');
andrew   = graph.addVertex(label,'person','name','andrew');

robin.addEdge('Communication',jeff,'date','2017-12-11T00:00:00Z','weight',1);
robin.addEdge('Communication',jeff,'date','2017-12-10T00:00:00Z','weight',1);
robin.addEdge('Communication',jeff,'date','2017-12-09T00:00:00Z','weight',1);
robin.addEdge('Communication',andrew,'date','2017-12-11T00:00:00Z','weight',1);
robin.addEdge('Communication',andrew,'date','2017-12-10T00:00:00Z','weight',1);
robin.addEdge('Communication',andrew,'date','2017-12-09T00:00:00Z','weight',1);

jeff.addEdge('Communication',robin,'date','2017-12-11T00:00:00Z','weight',1);
jeff.addEdge('Communication',robin,'date','2017-12-10T00:00:00Z','weight',1);
jeff.addEdge('Communication',robin,'date','2017-12-09T00:00:00Z','weight',1);
jeff.addEdge('Communication',andrew,'date','2017-12-11T00:00:00Z','weight',1);
jeff.addEdge('Communication',andrew,'date','2017-12-10T00:00:00Z','weight',1);
jeff.addEdge('Communication',andrew,'date','2017-12-09T00:00:00Z','weight',1);
andrew.addEdge('Communication',robin,'date','2017-12-11T00:00:00Z','weight',1);
andrew.addEdge('Communication',robin,'date','2017-12-10T00:00:00Z','weight',1);
andrew.addEdge('Communication',robin,'date','2017-12-09T00:00:00Z','weight',1);
andrew.addEdge('Communication',jeff,'date','2017-12-11T00:00:00Z','weight',1);
andrew.addEdge('Communication',jeff,'date','2017-12-10T00:00:00Z','weight',1);
andrew.addEdge('Communication',jeff,'date','2017-12-09T00:00:00Z','weight',1); 

我们运行下面的脚本。

g.V().has('name','robin').as('v').
  repeat(outE().as('e').otherV().as('v').simplePath()).
    until(has('name','jeff')).
  store('a').
    by('name').
  store('a').
    by(select(all, 'v').unfold().values('name').fold()).
  store('a').
    by(select(all, 'e').unfold().
       store('x').
         by(union(values('weight'),
                  select('x').count(local)).fold()).
       cap('x').
       store('a').
         by(unfold().limit(local, 1).fold()).unfold().
       sack(assign).
         by(constant(1d)).
       sack(div).
         by(union(constant(1d),
                  tail(local, 1)).sum()).
       sack(mult).
         by(limit(local, 1)).
       sack().sum()).
  cap('a') 

得到了结果: ==> [杰夫,杰夫,杰夫,杰夫,杰夫,杰夫,杰夫,杰夫,杰夫,杰夫,杰夫,杰夫,[循环赛,杰夫],[循环赛,杰夫],[循环赛,杰夫],[1] 1.0,[1,1],1.5%,[1,1,1],1.8333333333333333,[循环赛,安德鲁,杰夫],[循环赛,安德鲁,杰夫],[循环赛,安德鲁,杰夫],[循环赛,安德鲁,杰夫],[循环赛,安德鲁,杰夫],[循环赛,安德鲁,杰夫],[循环赛,安德鲁,杰夫],[循环赛,安德鲁,杰夫],[循环赛,安德鲁,杰夫],[1,1,1, 1,1],2.283333333333333,[1,1,1,1,1,1,1],2.5928571428571425,[1,1,1,1,1,1,1,1,1],2.8289682539682537,[1, 1,1,1,1,1,1,1,1,1,1] 3.0198773448773446,[1,1,1,1,1,1,1,1,1,1,1,1,1] ,3.180133755133755,[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] 3.3182289932289937,[1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1] 3.439552522640758,[1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1],3.547739657143682,[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1],3.6453587047627294]

但我们希望按相同路径分组,并将相同路径的分数相加。 有什么建议吗? 现在的脚本我们有相同的路径,如[robin,andrew,jeff],但它们是分开的。

结果我们想得到如下结果: [[循环赛,杰夫] = 4.3333333333333333,[循环赛,安德鲁,杰夫] = 27.85604971]

0 个答案:

没有答案