cancel
Showing results for 
Search instead for 
Did you mean: 

Create all pathes where each path has any condition

I have a such question:
I have nodes which are connected in a such way:
3X_4_6_46fcb5d110d9a18ca89fcfb7d46524e4d9d69adc.png
Now I wanna find all pathes from main to every leaf with condition that every path contains only one direction etc:
main -> west -> west -> leaf
main -> east -> east -> leaf
and return all nodes in each way.
In one query, i mean
I can do something like that for one path
match p=(start:Square)-[d:HOP*3]->(end:Square)
where start.row=4 and start.col=4 and
(all (x in relationships(p) where x.type="NORTH"))
return p;

1 ACCEPTED SOLUTION

giuseppe_villan
Graph Fellow

@dimachichlov

If I got it, the dataset is something like this:

create (n:Square) with n
create (n)-[:HOP {type: 'NORTH'}]->(:Square)-[:HOP {type: 'NORTH'}]->(:Square)-[:HOP {type: 'NORTH'}]->(:Square)
create (n)-[:HOP {type: 'EAST'}]->(:Square)-[:HOP {type: 'EAST'}]->(:Square)-[:HOP {type: 'EAST'}]->(:Square)
create (n)-[:HOP {type: 'SOUTH'}]->(:Square)-[:HOP {type: 'SOUTH'}]->(:Square)-[:HOP {type: 'SOUTH'}]->(:Square)
create (n)-[:HOP {type: 'WEST'}]->(:Square)-[:HOP {type: 'WEST'}]->(:Square)-[:HOP {type: 'WEST'}]->(:Square)

In this case, you could search all "coordinate" paths with this query:

MATCH (start:Square) with start
MATCH north=(start)-[d:HOP*3 {type: "NORTH"}]->(end:Square) // search rels with prop type "NORTH"
WITH north, start 
MATCH south=(start)-[d:HOP*3 {type: "SOUTH"}]->(end:Square) // search rels with prop type "SOUTH"
WITH north, south, start
MATCH east=(start)-[d:HOP*3 {type: "EAST"}]->(end:Square)
WITH north, south, east, start
MATCH west=(start)-[d:HOP*3 {type: "WEST"}]->(end:Square)
RETURN DISTINCT north, east, west, south

View solution in original post

1 REPLY 1

giuseppe_villan
Graph Fellow

@dimachichlov

If I got it, the dataset is something like this:

create (n:Square) with n
create (n)-[:HOP {type: 'NORTH'}]->(:Square)-[:HOP {type: 'NORTH'}]->(:Square)-[:HOP {type: 'NORTH'}]->(:Square)
create (n)-[:HOP {type: 'EAST'}]->(:Square)-[:HOP {type: 'EAST'}]->(:Square)-[:HOP {type: 'EAST'}]->(:Square)
create (n)-[:HOP {type: 'SOUTH'}]->(:Square)-[:HOP {type: 'SOUTH'}]->(:Square)-[:HOP {type: 'SOUTH'}]->(:Square)
create (n)-[:HOP {type: 'WEST'}]->(:Square)-[:HOP {type: 'WEST'}]->(:Square)-[:HOP {type: 'WEST'}]->(:Square)

In this case, you could search all "coordinate" paths with this query:

MATCH (start:Square) with start
MATCH north=(start)-[d:HOP*3 {type: "NORTH"}]->(end:Square) // search rels with prop type "NORTH"
WITH north, start 
MATCH south=(start)-[d:HOP*3 {type: "SOUTH"}]->(end:Square) // search rels with prop type "SOUTH"
WITH north, south, start
MATCH east=(start)-[d:HOP*3 {type: "EAST"}]->(end:Square)
WITH north, south, east, start
MATCH west=(start)-[d:HOP*3 {type: "WEST"}]->(end:Square)
RETURN DISTINCT north, east, west, south
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.