简体   繁体   English

找到ArangoDB中节点数的交叉节点?

[英]Find the cross node for number of nodes in ArangoDB?

I have a number of nodes connected through intermediate node of other type. 我有许多节点通过其他类型的中间节点连接。 Like on picture There are can be multiple middle nodes. 像在图片上可以有多个中间节点。 I need to find all the middle nodes for a given number of nodes and sort it by number of links between my initial nodes. 我需要找到给定数量节点的所有中间节点,并按照我的初始节点之间的链接数对其进行排序。 In my example given A, B, C, D it should return node E (4 links) folowing node F (3 links). 在给出A,B,C,D的示例中,它应该返回节点E(4个链接)以下节点F(3个链接)。 Is this possible? 这可能吗? If not may be it can be done using multiple requests? 如果不是,可以使用多个请求完成? I was thinking about using SHORTEST_PATH function but seems it can only find path between nodes from the same collection? 我正在考虑使用SHORTEST_PATH函数,但似乎它只能找到来自同一集合的节点之间的路径? 在此输入图像描述

Very nice question, it challenged the AQL part of my brain ;) Good news: it is totally possible with only one query utilizing GRAPH_COMMON_NEIGHBORS and a portion of math. 非常好的问题,它挑战了我脑中的AQL部分;)好消息:完全有可能只有一个查询利用GRAPH_COMMON_NEIGHBORS和一部分数学。 Common neighbors will count for how many of your selected vertices a cross is the connecting component (taking into account ordering AEB is different from BEA) using combinatorics we end up having a*(a-1)=c many combinations, where c is comupted. 常见的邻居将计算你选择的十字架中有多少是十字架是连接组件(考虑到订购AEB与BEA不同)使用组合学我们最终得到a*(a-1)=c许多组合,其中c是com 。 We use p/q formula to identify a (the number of connected vertices given in your set). 我们使用p / q公式来识别a(在集合中给出的连接顶点的数量)。

If the type of vertex is encoded in an attribute of the vertex object the resulting AQL looks like this: 如果顶点type在顶点对象的属性中编码,则生成的AQL如下所示:

FOR x in ( ( let nodes = ["nodes/A","nodes/B","nodes/C","nodes/D"] for n in GRAPH_COMMON_NEIGHBORS("myGraph",nodes , nodes) for f in VALUES(n) for s in VALUES(f) for candidate in s filter candidate.type == "cross" collect crosses = candidate._key into counter return {crosses: crosses, connections: 0.5 + SQRT(0.25 + LENGTH(counter))} ) ) sort x.connections DESC return x

If you put the crosses in a different collection and filter by collection name the query will even get more efficient, we do not need to open any vertices that are not of type cross at all. 如果将十字架放在不同的集合中并按集合名称过滤,查询甚至会变得更有效率,我们不需要打开任何非十字类型的顶点。 FOR x in ( ( let nodes = ["nodes/A","nodes/B","nodes/C","nodes/D"] for n in GRAPH_COMMON_NEIGHBORS("myGraph",nodes, nodes, {"vertexCollectionRestriction": "crosses"}, {"vertexCollectionRestriction": "crosses"}) for f in VALUES(n) for s in VALUES(f) for candidate in s collect crosses = candidate._key into counter return {crosses: crosses, connections: 0.5 + SQRT(0.25 + LENGTH(counter))} ) ) sort x.connections DESC return x

Both queries will yield the result on your dataset: 两个查询都将在您的数据集上产生结果:

[ { "crosses": "E", "connections": 4 }, { "crosses": "F", "connections": 3 } ]

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

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