简体   繁体   English

Cypher (Neo4j) 匹配具有特定长度和值的所有路径

[英]Cypher (Neo4j) Match all paths with specific length and value

I'm new to Cypher and Neo4j, but I find it really interesting and are trying to use it to solve a math problem that I have.我是 Cypher 和 Neo4j 的新手,但我发现它真的很有趣,并试图用它来解决我遇到的数学问题。 In order to make the problem easy to illustrate, I've scaled it down and hoping you can help me find the right logic.为了使问题易于说明,我将其按比例缩小,希望您能帮助我找到正确的逻辑。

The Math problem: Given a set of tiles, how many ways can you select 3 tiles, with the sum less than x?数学问题:给定一组瓷砖,你可以有多少种方式 select 3 个瓷砖,总和小于 x?

In my example, let's just say that I have 5 tiles (100, 100, 80, 80, 50), and that I have to include at least one 100-tile, and that x is 270.在我的示例中,假设我有 5 个图块(100、100、80、80、50),并且我必须至少包含一个 100 块,并且 x 为 270。

Since the order doesn't matter, the way I think about the problem is that I start at the highest nr, and then from there can choose to go to either the same nr again, or the next lower number, or the second lower number.由于顺序无关紧要,所以我考虑问题的方式是从最高的 nr 开始,然后从那里可以选择 go 到相同的 nr,或者下一个较低的数字,或者第二个较低的数字. This would mean, that starting at 100, I could choose to select either another 100, or 80 (the next lower one), or 50 (the second lower one).这意味着,从 100 开始,我可以选择 select 或者另外 100,或者 80(下一个较低的),或者 50(第二个较低的)。

So far, I'm able to define a path starting at 100 and going 2 steps further to m:到目前为止,我能够定义从 100 开始并进一步到 m 2 步的路径:

MATCH path = (n:Node {value:100})-[:CONNECTED*2]-(m)

QUESTION: How do I find all paths with a specific sum of the nodes.value?问题:如何找到具有特定节点值总和的所有路径? Since the order doesn't matter, I'm only interested in the unique one-way paths.由于顺序无关紧要,我只对独特的单向路径感兴趣。 (Meaning, for example that if I get one path as 100-80-50, then Im not interested in the path 50-80-100 since that contains the exact same tiles, just different order). (意思是,例如,如果我得到一个路径为 100-80-50,那么我对路径 50-80-100 不感兴趣,因为它包含完全相同的图块,只是顺序不同)。

Thanks!谢谢!

neo4j 图像

you means this?你的意思是这个?

MATCH path = (n:Node {value:100})-[:CONNECTED*2]-(m)
WITH REDUCE(x=0,n in nodes(path)|x+n.value) as expected, [n in nodes(path)|n.value] as listNode
WHERE expected >100
RETURN listNode

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

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