cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Possible paths from a node without a destination node

a10554
Graph Buddy

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
1 ACCEPTED SOLUTION

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. 

View solution in original post

14 REPLIES 14

a10554
Graph Buddy

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. 

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

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

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 !

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

@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",

Do you need anything else?

a10554
Graph Buddy

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