Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-19-2020 02:45 PM
Hello guys,
i got this cypher to get all the shortest paths between two nodes, and i think it is working pretty well. Can you tell me how can i write a cypher to get all paths up to a maximum number of hops from a specific node and no specific destination ? With this i want to see the possible ending nodes starting at my given node.
Thank you !
José Salvador
MATCH (n:Perception_Group)
WHERE ID(n)=252 OR ID(n)=801
WITH collect(n) as nodes
UNWIND nodes as n
UNWIND nodes as m
WITH * WHERE id(n) < id(m)
MATCH path = allShortestPaths( (n)-[*..5]-(m) )
RETURN path
Solved! Go to Solution.
05-19-2020 09:40 PM
Try this:
MATCH (a)
WHERE id(a) = 801
CALL apoc.path.spanningTree(a, {}) YIELD path
return path
Returns all available levels, nodes and relationships.
If you want to restrict level to say 3 then
CALL apoc.path.spanningTree(a, {maxLevel:3}) YIELD path
Check the syntax as you can include/exclude nodes and relationships.
05-19-2020 03:41 PM
Came up with this one. Do you guys see any better way to do it ?
MATCH (pg1:Perception_Group)-[s:SEQUENCE*0..5]-(pg2:Perception_Group)
WHERE ID(pg1)=801
RETURN pg1,s,pg2
05-19-2020 04:14 PM
It felt like it was taking too long for longer paths so i think this next option is better (avoid cycles):
MATCH p=allShortestPaths((pg1:Perception_Group)-[s:SEQUENCE*0..5]-(pg2:Perception_Group))
WHERE ID(pg1)=801
RETURN p
Any comment ?
05-19-2020 09:40 PM
Try this:
MATCH (a)
WHERE id(a) = 801
CALL apoc.path.spanningTree(a, {}) YIELD path
return path
Returns all available levels, nodes and relationships.
If you want to restrict level to say 3 then
CALL apoc.path.spanningTree(a, {maxLevel:3}) YIELD path
Check the syntax as you can include/exclude nodes and relationships.
05-20-2020 02:33 AM
Thank you ameyasoft.
How do you compare the performance of your suggestion with this one ?
MATCH p=allShortestPaths((pg1:Perception_Group)-[s:SEQUENCE*0..5]-(pg2:Perception_Group))
WHERE ID(pg1)=801
RETURN p
05-20-2020 02:43 AM
Ameyasoft. Your query returns all node types, how can i specify which nodes type i want and through which relationship type ?
05-20-2020 08:11 PM
You add all your filters between the curly baffles:{ }.
Check this for syntax: https://neo4j.com/docs/labs/apoc/current/graph-querying/expand-spanning-tree/
05-21-2020 02:52 AM
Thank you ameysoft !
05-20-2020 02:38 AM
Hello @a10554,
This should help you to compare queries: https://neo4j.com/docs/cypher-manual/current/query-tuning/how-do-i-profile-a-query/
05-20-2020 02:41 AM
Thank you Maxime ! I was wondering exactly how to do that.
05-20-2020 02:48 AM
@a10554 I think this documentation should answer your questions: https://neo4j.com/docs/labs/apoc/current/graph-querying/expand-spanning-tree/
05-20-2020 02:54 AM
Cool Maxime, found the parameter
relationshipFilter: "KNOWS",
05-20-2020 02:55 AM
Do you need anything else?
05-20-2020 02:58 AM
Yes, i will need more stuff with other queries i haven't started yet. Will post as soon as i dive into them 🙂 Thank you Maxime !
05-20-2020 02:59 AM
No problem, you can accept the answer of @ameyasoft as answer to close this topic and you will reopen a new one when you will be in need:)
All the sessions of the conference are now available online