且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

密码如何获得每两个节点与起始节点之间的距离的关系?

更新时间:2023-02-01 22:46:31

虽然到端节点的距离很容易,但是要获得最后的关系以及所需的结果,您的要求会很困难(也许不可能?)之所以只使用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