Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
784 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 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...