密码如何获得每两个节点与起始节点之间的距离的关系? [英] cypher how get relation between every two node and the distance from start node?

查看:71
本文介绍了密码如何获得每两个节点与起始节点之间的距离的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些节点和关系,例如A -> B ->C -> D;并且B->D所以B C D是一个循环,现在我要获得所有与节点A的关系和每个关系的距离; 我希望得到这样的结果:

I have some nodes and relation like A -> B ->C -> D; andB->DSo the B C D is a loop, now I want get all relations and each relation distance from node A; I expect result like:

{startNode: A, endNode: B, rel:FRIEND, distanceFromAtoEndnode: 1},
{startNode: B, endNode: C, rel:FRIEND, distanceFromAtoEndnode: 2},
{startNode: C, endNode: D, rel:FRIEND, distanceFromAtoEndnode: 3},
{startNode: B, endNode: D, rel:FRIEND, distanceFromAtoEndnode: 2}

和我的密码:

match p=(n:Person {name:"A"})-[r*1..9]-(m:Person) return last(r) as rel,length(p) as distanceFromAtoEndnode 

但这总是得到我不需要的另外一项:

but this always get one more item I donot need:

{startNode: D, endNode: C, rel:FRIEND, distanceFromAtoEndnode: 3},

如果存在像"8"这样的双循环,则结果会更糟

and if there are double loop like "8" then the result is worse

我怎么写密码?

推荐答案

虽然到端节点的距离很容易,但是要获得最后的关系以及所需的结果,您的要求会很困难(也许不可能?)之所以只使用Cypher,是因为关系可能会根据图形的连通性进行多次遍历,并且由于相同的关系将作为长度不同的路径的最后一个关系出现.

While it's easy enough to get distance to end nodes, your requirement to get the last relationship, with the results you want, will be tough (maybe impossible?) to get with just Cypher, since since relationships may be traversed multiple times depending on the connectedness of the graph, and since the same relationships will occur as the last relationship of paths of differing lengths.

如果您绝对需要此功能,则可以使用 APOC路径扩展程序以确保每个关系仅被遍历一次,因此结果将不会导致相同的关系,而是路径不同.

If you absolutely need this, then you can use APOC path expander procedures to ensure that each relationship is only ever traversed once, so you won't get the same relationship as a result but for a different path.

应该为您提供所需结果的用法示例:

An example of usage that should give you the results you want:

MATCH (n:Person {name:"A"})
CALL apoc.path.expandConfig(n, {uniqueness:'RELATIONSHIP_GLOBAL', minLevel:1, maxLevel:9, labelFilter:'>Person'}) YIELD path
WITH last(relationships(path)) as rel, length(path) as distanceFromAtoEndnode
RETURN rel {startNode:startNode(rel).name, endNode:endNode(rel).name, rel:type(rel)} as rel, distanceFromAtoEndnode

对于纯Cypher解决方案,由于Cypher的扩展试图查找所有可能的路径,因此您可能会挂在高度连接的图上.但是,你去了.

As for pure Cypher solutions, you may get hangs on highly connected graphs, since Cypher's expansion tries to find all possible paths. But here you go.

要查找所有连接的节点的最短距离,请执行以下操作:

To find shortest distance for all connected nodes:

MATCH path=(:Person {name:"A"})-[*1..9]-(other:Person)
RETURN other, min(length(path)) as shortestDistance

并找到连接所有连接的节点的所有关系:

And to find all relationships connecting all connected nodes:

MATCH path=(:Person {name:"A"})-[*1..9]-(:Person)
WITH distinct last(relationships(path)) as rel
RETURN rel {startNode:startNode(rel).name, endNode:endNode(rel).name, rel:type(rel)} as rel

这篇关于密码如何获得每两个节点与起始节点之间的距离的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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