RoK
RoK

Reputation: 1922

Neo4j: Customize Path Traversal

I am pretty new to Neo4j. I have implemented an example use case with the following setup:

With the help of stackoverflow and several tutorials I was able to formulate a Cypher query which gets me all paths from any start node with one externalID to the matching end node with the same externalID.

MATCH p=(a:S)-[r*]->(b:E) 
WHERE a.externalID=b.externalID
WITH p, relationships(p) as rcoll 
RETURN p

The query works more or less good so far ...

However, I have no idea how to change the behavior on how the graph is scanned for possible paths. Actually I only need a subset of all possible paths. Such paths fulfill the following requirement:

Can I somehow adjust the query or is there any other possibility with Neo4j to get all paths using the strategy above?

Thanks a lot for your help in advance.

Upvotes: 2

Views: 530

Answers (1)

cybersam
cybersam

Reputation: 66967

This Cypher query might be suitable for your use case:

MATCH p = (a:S)-[r*]->(b:E) 
WHERE a.externalID = b.externalID
WITH
  p,
  REDUCE(c = a.capacity, r IN RELATIONSHIPS(p) |
    CASE WHEN c < 0 THEN -1 ELSE c - r.weight END) AS residual
WHERE residual >= 0
RETURN p;

The REDUCE clause will set residual to a negative value if the capacity is ever reduced below 0, even if subsequent weights would normally cause it to go positive.

Upvotes: 2

Related Questions