cancel
Showing results forΒ
Did you mean:Β

## ShortestPath

Node

The picture shown below describes a section from a family tree.
The nodes N and O have one child R.
N has Mother J.
O has Mother K.
O and K both have D as Mother.
I'm trying to find the shortestPath between the nodes N and O, but I want to exclude the path N->R->O (a path with length 2).
The path I'm looking for is N -> J -> D -> K -> O (a path with length 4).
How can I construct a query to achieve this? I plan to run the query on the entire graph (>10.000 nodes), so it would be nice if it performed reasonably well.

2 REPLIES 2
Ninja

If the hop count is small, the search speed will not be a problem.

``````MATCH p1=(n:Person {name: 'N'})-[:MOTHER*..2]->(mother:Person),
p2=(d:Person {name: 'O'})-[:MOTHER*..2]->(mother)
RETURN p1,p2
``````

``````CREATE (n:Person {name:'N'}),
(j:Person {name:'J'}),
(d:Person {name:'D'}),
(k:Person {name:'K'}),
(o:Person {name:'O'}),
(r:Person {name:'R'})
CREATE (r)-[:MOTHER]->(n)-[:MOTHER]->(j)-[:MOTHER]->(d),
(r)-[:MOTHER]->(o)-[:MOTHER]->(k)-[:MOTHER]->(d)
CREATE (r)<-[:OFFSPRING]-(n)<-[:OFFSPRING]-(j)<-[:OFFSPRING]-(d),
(r)<-[:OFFSPRING]-(o)<-[:OFFSPRING]-(k)<-[:OFFSPRING]-(d);
``````
Node

``````Match p=shortestPath((d1:Person)-[*]-(d2:Person))