计算从给定顶点到多图中每个邻居的传入和传出边缘

问题描述 投票:0回答:1

我试图找到一个查询,这将允许我得到一个多元图中顶点的传入和传出顶点的组计数。

enter image description here

以上图为V [0],我们应该得到:

[V [0],V [1],传入:2,传出:0]

[V [0],V [2],传入:1,传出:0]

[V [0],V [3],传入:0,传出:1]

graph graph-databases gremlin tinkerpop3
1个回答
1
投票

在询问有关Gremlin的问题时,图片和图形描述很不错,但创建一些示例数据的Gremlin脚本甚至更好:

g = TinkerGraph.open().traversal()
g.addV('node').property(T.id,0).as('0').
  addV('node').property(T.id,1).as('1').
  addV('node').property(T.id,2).as('2').
  addV('node').property(T.id,3).as('3').
  addE('link').from('1').to('0').
  addE('link').from('1').to('0').
  addE('link').from('0').to('3').
  addE('link').from('2').to('0').iterate()

这是一种方法:

gremlin> g.V(0).bothE().
......1>   group().
......2>     by(union(inV(),outV()).fold()).
......3>     by(fold().
......4>        project('incoming','outgoing').
......5>          by(unfold().inV().hasId(0).count()).
......6>          by(unfold().outV().hasId(0).count()))
==>[[v[0],v[1]]:[incoming:2,outgoing:0],[v[0],v[2]]:[incoming:1,outgoing:0],[v[3],v[0]]:[incoming:0,outgoing:1]]

基本上,我们通过其相关的输入/输出顶点(第2行 - 即由group()形成的输入/输出的List)对每个边缘进行union().fold(),然后减少每个顶点对收集的边(从第3行开始)。 reduce操作只是创建一个fold()列表然后使用project()List转换为带有“传入”和“传出”键的Map - 这些键的值在以下各自的by()调制器中定义(即展开边缘列表,适当地过滤顶点“0”和count())。

© www.soinside.com 2019 - 2024. All rights reserved.