简体   繁体   中英

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. 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?

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.

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. 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).

So far, I'm able to define a path starting at 100 and going 2 steps further to m:

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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