Possible paths from a node without a destination node

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

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

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 ?

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. 

2 Likes

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

Hello @a10554,

This should help you to compare queries: https://neo4j.com/docs/cypher-manual/current/query-tuning/how-do-i-profile-a-query/

Thank you Maxime ! I was wondering exactly how to do that.

1 Like

Ameyasoft. Your query returns all node types, how can i specify which nodes type i want and through which relationship type ?

@a10554 I think this documentation should answer your questions: https://neo4j.com/docs/labs/apoc/current/graph-querying/expand-spanning-tree/

Cool Maxime, found the parameter

relationshipFilter: "KNOWS",
1 Like

Do you need anything else?

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 !

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

1 Like

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/

Thank you ameysoft !