This website uses cookies. By clicking Accept, you consent to the use of cookies. Click Here to learn more about how we use cookies.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results forΒ

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

- Neo4j
- Technical Discussions
- Neo4j Graph Platform
- Validating Shortest-Path status inside another Pat...

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Validating Shortest-Path status inside another Path

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β12-03-2021 05:25 AM

```
MERGE (Z1:Z {z_id : 'z1'})-[:HAS_CHILD]->(z2:Z {z_id : 'z2'})-[:HAS_CHILD]->(z3:Z {z_id : 'z3'})-[:HAS_CHILD]->(z4:Z {z_id : 'z4'})-[:HAS_CHILD]->(z5:Z {z_id : 'z5'})
Create (x1:X {x_id: 'x1'})-[:HAS_OBJ]->(y1: Y {y_id: 'y1'})-[:HAS_OBJ]->(x2:X {x_id: 'x2'})-[:HAS_OBJ]->(y2: Y {y_id: 'y2'})-[:HAS_OBJ]->(x3:X {x_id: 'x3'});
MATCH (y:Y {y_id: 'y1'}),(z:Z {z_id: 'z2'}) WITH y,z CREATE (y)-[:HAS_Z {status: true}]->(z);
MATCH (y:Y {y_id: 'y2'}),(z:Z {z_id: 'z2'}) WITH y,z CREATE (y)-[:HAS_Z {status: true}]->(z);
MATCH (y:Y {y_id: 'y2'}),(z:Z {z_id: 'z4'}) WITH y,z CREATE (y)-[:HAS_Z {status: false}]->(z);
```

- Given: starNode (of type X), endNode (of type X) and z:Z node as input,
- Find: All available paths between startNode to endNode
- Condition: When all shortest-paths between all Y nodes (in the returned paths) to the given Z node are healthy (status=true)

Say, in the example above & considering x1 as startNode & x2 s endNode ...

- given Z: z1 there should be no path -> since there's no way to rearch z1 from any of Y Nodes
- given Z: z3: there should be a valid path, since the shortest-paths available for all y nodes to Z nodes are in a healthy state (y1->z2-_z3 & y2->z2->z3)
- given Z: v5: there should be no path -> Although the shortest path from y1 to z5 is in the healthy state (y1->z2->z3->z4->z5), but the shortest path from Y2 to z6 is not healthy (y2->z4->z5), since the status of y2->z4 is false

This was the first version of my Cypher query, but it's not working as expected (returns path in case of z: z5)

```
MATCH (z:Z) WHERE z.z_id = 'z5'
WITH z MATCH (start:X) WHERE start.x_id = 'x1'
with z,start MATCH (end:X) WHERE end.x_id = 'x3'
WITH z,start,end
MATCH p=(start)-[:HAS_OBJ*1..]->(y:Y)-[:HAS_OBJ*1..]->(end), p2=shortestPath((y)-[*1..]->(z))
WHERE ALL (
r in relationships(p2) WHERE
type(r)= 'HAS_CHILD' OR (type(r) = 'HAS_Z' AND r.status = true)
)
return p
```

Thanks in advance

1 REPLY 1

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

β12-05-2021 05:32 AM

I think I found the solution (by adding one extra line: `WITH p,p2`

between the last MATCH and WHERE clause) - letting the shortest-path function first find the existing shortest-path available (& not searching for a (shortest-)path which is compatible with predicate)

```
MATCH (z:Z) WHERE z.z_id = 'z5'
WITH z MATCH (start:X) WHERE start.x_id = 'x1'
with z,start MATCH (end:X) WHERE end.x_id = 'x3'
WITH z,start,end
MATCH p=(start)-[:HAS_OBJ*1..]->(y:Y)-[:HAS_OBJ*1..]->(end), p2=shortestPath((y)-[*1..]->(z))
WITH p,p2
WHERE ALL (
r in relationships(p2) WHERE
type(r)= 'HAS_CHILD' OR (type(r) = 'HAS_Z' AND r.status = true)
)
return p,p2
```

Nodes 2022

October 19th -

October 20th -

October 25th -

October 26th -

November 2nd -

Related Content

- Get all relationships within a list of nodes in Neo4j Graph Platform
- Return a relationship Label in shortest Path in Neo4j Graph Platform
- Parameterized query performance degradation in Neo4j Graph Platform
- SHACL - validate nodes have 1 class A and 1 class B via same predicate in Neo4j Graph Platform
- find paths in graph but if node X in the path than node Y must be in to have a valid path in Neo4j Graph Platform