我正在使用图表,最近了解neo4j。
neo4j能帮我找到通过图中给定节点的所有简单循环吗?
我已经可以通过a modification of Johnson's algorithm在java
/ python
代码中执行此操作。
这只是我创建的图表的一个例子,是可以在neo4j数据库上执行的Cypher代码:
CREATE (John:Person { name : '@john',facebook: 'facebook.com/john'})
CREATE (Josh:Person { name : '@josh',facebook: 'facebook.com/josh'})
CREATE (Dan:Person { name : '@dan',facebook: 'facebook.com/dan'})
CREATE (Kenny:Person { name : '@kenny',facebook: 'facebook.com/kenny'})
CREATE (Bart:Person { name : '@bart',facebook: 'facebook.com/bart'})
CREATE (Mike:Person { name : '@mike',facebook: 'facebook.com/mike'})
CREATE (Jenny:Person { name : '@jenny',facebook: 'facebook.com/jenny'})
CREATE (Frank:Person { name : '@frank',facebook: 'facebook.com/frank'})
CREATE (Erick:Person { name : '@erick',facebook: 'facebook.com/erick'})
CREATE (Lynda:Person { name : '@lynda',facebook: 'facebook.com/lynda'})
CREATE (Lynda)-[:KNOWS]-> (Josh)
CREATE (Lynda)-[:KNOWS]-> (Frank)
CREATE (Lynda)-[:KNOWS]-> (Bart)
CREATE (Josh)-[:KNOWS]-> (Erick)
CREATE (Josh)-[:KNOWS]-> (Jenny)
CREATE (Josh)-[:KNOWS]-> (Dan)
CREATE (Dan)-[:KNOWS]-> (Lynda)
CREATE (Dan)-[:KNOWS]-> (Josh)
CREATE (Dan)-[:KNOWS]-> (Mike)
CREATE (Dan)-[:KNOWS]-> (Kenny)
CREATE (Mike)-[:KNOWS]-> (Kenny)
CREATE (Kenny)-[:KNOWS]-> (Bart)
CREATE (Bart)-[:KNOWS]-> (Josh)
CREATE (Frank)-[:KNOWS]-> (Erick)
CREATE (Erick)-[:KNOWS]-> (Frank)
...这些是图表中的所有周期:
Josh->Dan->Lynda->Josh
Josh->Dan->Lynda->Bart->Josh
Josh->Dan->Josh
Josh->Dan->Mike->Kenny->Bart->Josh
Josh->Dan->Kenny->Bart->Josh
这里列出了一些简单的测试用例:
1- input: Josh
output (all the cycles):
Josh->Dan->Lynda->Josh
Josh->Dan->Lynda->Bart->Josh
Josh->Dan->Josh
Josh->Dan->Mike->Kenny->Bart->Josh
Josh->Dan->Kenny->Bart->Josh
2- input: Lynda
output:
Josh->Dan->Lynda->Josh
Josh->Dan->Lynda->Bart->Josh
答案 0 :(得分:7)
您可以使用以下查询在Cypher中执行此操作:
update tablea a
join (
select email, max(purchase_date) purchase_date
from tableb
group by email
) b on a.email = b.email
set a.purchase_date = b.purchase_date
查询的文字表示是:
找到开始节点和结束节点相同且完整路径具有传出方向的路径
请注意,对于中/大图,这是一个昂贵的查询,您可以限制路径的深度,例如:
MATCH p=(n)-[*]->(n) RETURN nodes(p)
也许您还希望最小深度为2,因为与自身有关系的节点将返回深度为1; - )