简体   繁体   English

Neo4j Cypher从特定节点查找循环

[英]Neo4j Cypher find loop from specific node

I want to find all loops that originate and terminate with a specific node in a Neo4j database. 我想在Neo4j数据库中找到所有以特定节点为起点和终点的循环。 I tried: 我试过了:

START n=node:Event(time=",timestamp,")
MATCH p=(n)-[:LINKED_TO*1..5]->(n)
WHERE NONE (n IN nodes(p) WHERE size(filter(x IN nodes(p) WHERE n = x))> 2)
RETURN p, length(p)

This is the best I can mashup from what is on the web. 这是我可以从网络上实现的最佳混搭。 There are two things I don't like about this: 1. it crashes 2. the count threshold must be ">2" to allow for the start+termination node. 我对此有两点不满意:1.崩溃2.计数阈值必须为“> 2”以允许start + termination节点。 That means that loops that visit the same intermediate node twice will be included, which I wish was not the case. 这意味着将包含两次访问同一中间节点的循环,我希望不是这种情况。

I'm not interested in the shortest path. 我对最短的路径不感兴趣。 I want to know all loops that return to my starting node. 我想知道所有返回到起始节点的循环。

Thank you in advance! 先感谢您!

This query should return all loops that start and end at the specified node and have no other repeated nodes: 此查询应返回在指定节点处开始和结束且没有其他重复节点的所有循环:

START n=node:Event(time=",timestamp,")
MATCH p=(n)-[:LINKED_TO*1..5]->(n)
UNWIND TAIL(NODES(p)) AS m
WITH p, COUNT(DISTINCT m) AS cm
WHERE LENGTH(p)-1 = cm
RETURN p, LENGTH(p);

Thank you, cybersam! 谢谢Cyber​​sam! That was helpful. 那很有帮助。 As typed, it gave a few errors and warned me that "START" is deprecated. 键入时,它给出了一些错误,并警告我“ START”已弃用。 I found the following modifications worked: 我发现以下修改有效:

MATCH (n:Event{time:1458238060505007})
MATCH p=(n)-[:LINKED_TO*1..5]->(n)
UNWIND TAIL(NODES(p)) AS m WITH p RETURN p

The only problem with this is that it appears to give all paths that go through the desired start node, n. 唯一的问题是,它似乎给出通过所需起始节点n的所有路径。 Is that true? 真的吗? If so, is there a way to correct this? 如果是这样,有没有办法解决这个问题?

This what finally worked for me. 这终于对我有用。 It is very close to what cybersam suggested. 这与Cyber​​sam的建议非常接近。 Apologies for doing this "the wrong way". 抱歉这样做“错误的方式”。 I'm sure cybersam will yell at me, again, but adding code via Comment is not very easy to read. 我敢肯定,cybersam会再次对我大吼大叫,但是通过Comment添加代码并不是很容易阅读。

MATCH p=(n:Event{time:",timestamp,"})-[:LINKED_TO*1..5]->(n) 
UNWIND TAIL (NODES(p)) AS m
WITH p,COUNT(DISTINCT m) AS cm
WHERE LENGTH(p) = cm
RETURN p

As I noted earlier, one sticking point was the use of "START", which is deprecated and causes errors (for example, when using RNeo4j in R, which I'm using). 正如我前面提到的,一个症结是使用“ START”,它已被弃用并导致错误(例如,当我在R中使用RNeo4j时)。 The new way appears to be to use MATCH and specify your starting node in the path pattern. 新方法似乎是使用MATCH并在路径模式中指定起始节点。 The other confusing thing for me was the use of "LENGTH(p)-1" instead of "LENGTH(p)". 对我来说,另一个令人困惑的事情是使用了“ LENGTH(p)-1”而不是“ LENGTH(p)”。 For one node connecting to another, the path has a length of 2, not 3 and there are only 2 distinct nodes. 对于一个连接到另一个节点的节点,路径的长度为2,而不是3,并且只有2个不同的节点。 For my application, "LENGTH(p)=cm" worked. 对于我的应用程序,“ LENGTH(p)= cm”有效。

Finally, if you want the nodes in the paths, do NOT try to use "WITH m,..." because this messes up the "COUNT(DISTINCT(m))" computation for some reason that I do not understand. 最后,如果您想要路径中的节点,请不要尝试使用“ WITH m,...”,因为由于某些我不了解的原因,这会弄乱“ COUNT(DISTINCT(m))”计算。

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

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