Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

cypher - How to process each of the allShortestPaths in Neo4j?

I have a Neo4j graph with several relationships, each of them having property y (number). I want to find all shortest paths between node with name 'x' and all other nodes and then, for each of these paths, calculate sum of the path edges properties and return the minimum of these sums.

MATCH paths = allShortestPaths((m)-[r*]-(n)) 
WHERE m.name = 'x' AND n.name <> 'x'
RETURN n.name, //min(sum(edge.y for y in path) for path in paths) -- something like this

So, is there some way to process each of the found paths independently and apply some function to their edges?

question from:https://stackoverflow.com/questions/65859220/how-to-process-each-of-the-allshortestpaths-in-neo4j

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is possible of course. The allShortestPaths query returns a List of paths that you can unwind, then for each of them you can reduce the properties of the relationships :

MATCH paths = allShortestPaths((m)-[r*]-(n)) 
WHERE m.name = 'x' AND n.name <> 'x'
UNWIND paths AS path
WITH n.name AS name, 
reduce(total = 0, rel IN relationships(path) | total + rel.y) AS total
RETURN name, min(total)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...