密码中的重复关系模式 [英] Repetitive pattern of relationships in cypher

查看:40
本文介绍了密码中的重复关系模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有可以通过一组路径模式连接的节点,例如:

(x:Class {code:'xxx'})<-[:a]-()-[:b]->()<-[:a]-()-[:b]->()<-[:a]-()-[:b]->()<-[:a]-()-[:b]->(y:Class{代码:'yyy'})

xy<之间可以存在可变数量的 <-[:a]-()-[:b]->/代码>.如果 xy 之间存在最短路径,我如何获得最短路径?

解决方案

正如评论中提到的,APOC Procedures 中的路径扩展程序提供了一种处理重复序列的方法.

但是,看起来您不仅要寻找特定的结束节点,还要寻找出现在序列中该点的节点(而不是,例如,作为这些关系对中间的节点).

在这种情况下,我们也需要包含 labelFilter,以特定我们感兴趣的序列中的哪个节点.或者我们可以只使用 sequence 配置参数而不是标签和关系过滤器.

MATCH (start:Class {code:'xxx'}), (end:Class{code:'yyy'})CALL apoc.path.expandConfig(start, {endNodes:[end], sequence:'>Class, <a, *, b>', limit:1}) YIELD 路径返回路径

我们提供我们感兴趣的终端节点(因为我们已经知道这些,我们已经匹配了它们),然后我们提供交替节点标签和关系类型的序列(使用 * 作为任何标签节点的替代,并使用前缀 > 作为结束节点过滤器,这意味着我们希望路径在此序列的此位置的 :Class 节点处结束).

路径扩展过程使用广度优先扩展,limit:1 表示我们遇到的第一条路径既是端节点又满足序列模式(a :重复序列中那个点的类节点)将是我们的最短路径.

Suppose I have nodes that can be joined by a set of pattern of paths, for example:

(x:Class {code:'xxx'})<-[:a]-()-[:b]->()<-[:a]-()-[:b]->()<-[:a]-()-[:b]->()<-[:a]-()-[:b]->(y:Class{code:'yyy'})

There can be a variable number of <-[:a]-()-[:b]-> between x and y. How can I get the shortest path if it exist between x and y ?

解决方案

As mentioned in the comments, the path expander procs from APOC Procedures provide a way to handle repeating sequences.

However it looks like you're not just looking for a specific end node, but for one that occurs at that point in the sequence (and not, say, as the node in the middle of those relationship pairs).

In that case we need to include the labelFilter too, to specific which node in the sequence we're interested in. Or we can just use the sequence config parameter instead of both the label and relationship filters.

MATCH (start:Class {code:'xxx'}), (end:Class{code:'yyy'})
CALL apoc.path.expandConfig(start, {endNodes:[end], sequence:'>Class, <a, *, b>', limit:1}) YIELD path
RETURN path

We supply the end nodes we're interested in (since we already know those, we matched to them already), then we supply the sequence of alternating node labels and relationship types (using * as a standin for a node of any label, and using a prefix of > as an end node filter, meaning we want paths that end at this :Class node in this position of the sequence).

The path expander procs use breadth-first expansion, and the limit:1 means the first path we encounter to a node that is both an end node, and that satisfies the sequence pattern (a :Class node at that point in the repeating sequence) will be our shortest path.

这篇关于密码中的重复关系模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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