Chris Arthur
Chris Arthur

Reputation: 1149

Neo4J / Cypher Match subtrees of choices to identify valid outcomes

Given a Neo4J graph that represents potential choices (the Green nodes represent potential choices in the picture below) Only certain choices are valid - indicated with a valid outcome of having all leaf nodes in the choice path being orange.

I would like to identify paths (series of choices (and associated nodes)) from the yellow start node where all Leaf Nodes (to an arbitrary depth) are orange. The graph is directed and whether a node is blue or green is saved as a node property.

Example Graph

Blue and Orange Nodes may be shared between choices (ie an orange node may not necessarily have degree = 1)

Given the graph in the figure I would like the following returned …

Yellow -> A -> E A -> G

Yellow -> C -> M

This is a more complicated extension of my earlier question... Neo4J - count outermost nodes

Upvotes: 0

Views: 68

Answers (1)

stdob--
stdob--

Reputation: 29172

You can use the predicate ALL to search for appropriate nodes:

MATCH (Y {color: 'Yellow'}) WITH Y
MATCH (G {color: 'Green'})-->(O)
WITH Y,
     G, 
     ALL(color IN COLLECT(O.color) WHERE color = 'Orange') AS valid
     WHERE valid = TRUE
MATCH path = (Y)-[*]->(G)
RETURN path

Upvotes: 0

Related Questions