Gremlin:计数连接时忽略了在相反方向上具有平行边的边的连接 [英] Gremlin: Count connections ignoring edges with a parallel edge in the opposing direction

查看:55
本文介绍了Gremlin:计数连接时忽略了在相反方向上具有平行边的边的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理一个图形,该图形指示顶点之间的连接.顶点可以双向连接.我想知道有多少个顶点相互连接,而不管连接的方向还是两个方向上都存在连接.

I'm currently working with a graph which indicates connections between vertices. The vertices can be connected in both directions. I am interested in knowing how many vertices are connected to each other regardless both the direction of the connection or if connections exist in both directions.

因此,例如,在下面绘制的图形中,连接的顶点总数为3(而简单的边沿计数将告诉我们有4

So for example, in the graph sketched below the total number of connected vertices would be 3 (whilst a simple edge count would tell us there are 4

由于边缘的方向性,这不是Tinkerpop食谱提供的重复边缘检测所解决的问题.是否有Gremlin查询可以帮助解决这一问题?

Due to the directionality of the edges this isn’t the same problem solved by the duplicate edge detection provided by the Tinkerpop recipes Is there a Gremlin query which could help with this count?

我在下面提供了一些示例数据:

I’ve included some example data below:

vertex1 = graph.addVertex("example","vertex1")
vertex2 = graph.addVertex("example","vertex2")
vertex3 = graph.addVertex("example","vertex3")
vertex4 = graph.addVertex("example","vertex4")

vertex1.addEdge("Connected_to",vertex2)
vertex2.addEdge("Connected_to",vertex1)
vertex2.addEdge("Connected_to",vertex3)
vertex3.addEdge("Connected_to",vertex4)

我是Gremlin语言的新手,我无法创建一个查询来计算顶点之间的连接数.当您掌握Graph查询的复杂性时,能从你们那里获得一些帮助将是非常棒的!

I’m new to the Gremlin language and I’m having trouble creating a query which counts the number of connections between vertices. It would be great to get some help from you guys as I get to grips with the complexities of Graph queries!

推荐答案

您可以通过两个顶点ID dedup().只需确保两个顶点的顺序一致(例如,按其ID排序),这样边方向就不会受到影响.

You can dedup() by the two vertices ids. Just make sure to have a consistent order of the two vertices (e.g. order by their id), so that the edge direction has no impact.

gremlin> g.E()
==>e[8][0-Connected_to->2]
==>e[9][2-Connected_to->0]
==>e[10][2-Connected_to->4]
==>e[11][4-Connected_to->6]
gremlin> g.E().dedup().by(bothV().order().by(id).fold())
==>e[8][0-Connected_to->2]
==>e[10][2-Connected_to->4]
==>e[11][4-Connected_to->6]
gremlin> g.E().dedup().by(bothV().order().by(id).fold()).count()
==>3

这篇关于Gremlin:计数连接时忽略了在相反方向上具有平行边的边的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆