I need to build a short route with three different link conditions. For example, the first condition is the connection between nodes: (:n) - [:r] - (:n) is direct. The second condition is that the connection between nodes goes through an intermediate node: :t (:n) - [:rt] - (:t) - [:rt] - (:n). the Third condition is the connection between nodes through two intermediate nodes: t1 and: t2 (:n) - [:rt1] - (:t1) - [:rt1] - (:t2) - [:rt2] - (:n). In all three cases, the types of links are also different. Connections can be made in any sequence several times. How do I write the correct cypher query?
Thank you.
MATCH p=(start:NodeLabel{prop:"propValue"})-[r*1..3]->(end:NodeLabel{prop:propValue})
WHERE ALL(rel In r WHERE type(r) IN ["rt","rt1","rt2"])
WITH start,end, [rel In r| rel.weight] as weightList,nodes(p) as path
WITH start,end ,path, apoc.coll.sum(weightList) as distance
RETURN start.name as startNode, end.name as endNode,[x in path|x.name] as shortestRoute , distance ORDER BY distance LIMIT 1
this query searches for all the routes from start Node to end node and then finds the sum of weights on relations in the path and finally ordering by distance gives the shortest weighted path..if there is no weight , you can just use
SIZE(r) as distance in the place of apoc.coll.sum(weightList) as distance