cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

find paths in graph but if node X in the path than node Y must be in to have a valid path

(new on graphs)

What is the best strategy finding all paths but if node with name BXX_1 in in the path only paths with node BXX_2 are valid paths.  ( B13_1 then B13_2 needs to be in the path,..... )

The nodes are not neighbours.

And which strategy has the highest performance.

- do I tag a node B13_1 with property : NodeDepency : B13_2  and how to take this in account

- do I make a relationship between Node B13_1 and B13_2 and how to take this in account

 

Koen_Algoet_0-1661495261536.png

 

1 ACCEPTED SOLUTION

I guess you could filter out the paths that don't meet your dependency criteria.  Assuming you have a list of dependent node name, such as [["B13_1", "B13_2"], ["B12_1", "B12_2"]], then the following is an example of a predicate you can use to filter only the paths that meet your dependency criteria. PathNodesData is test data that represents the node names for two paths. 

with [["a", "b"],["B13_1", "B13_2", "a", "c"]] as PathNodesData, 
[["B13_1", "B13_2"], ["B12_1", "B12_2"]] as dependencies
unwind PathNodesData as PathNodes
with PathNodes, dependencies
where all(x in dependencies where ((not x[0] in PathNodes) OR ((x[0] in PathNodes) AND (x[1] in PathNodes))))
return PathNodes

 Understanding that the OR operation is executed left-to-right and that the expression will stop evaluating if the first  operand is true, the predicate can be safely sampled to:

where all(x in dependencies where ((not x[0] in PathNodes) OR (x[1] in PathNodes)))

You will have to investigate the performance if you require this over a large number of paths or a large number of dependencies.

View solution in original post

4 REPLIES 4

MATCH (n:Tank {Name:"T0101"} )
CALL apoc.path.expandConfig(n, { relationshipFilter:"CanExportToBarge>",minLevel:1, maxLevel:12,limit:100,labelFilter:"/Connection"}) 
YIELD path
with ([in nodes(path) | n.Name] ) as PathNodes
return PathNodes as ListofNodesInPath
 
Koen_Algoet_0-1661513433371.png

 

I guess you could filter out the paths that don't meet your dependency criteria.  Assuming you have a list of dependent node name, such as [["B13_1", "B13_2"], ["B12_1", "B12_2"]], then the following is an example of a predicate you can use to filter only the paths that meet your dependency criteria. PathNodesData is test data that represents the node names for two paths. 

with [["a", "b"],["B13_1", "B13_2", "a", "c"]] as PathNodesData, 
[["B13_1", "B13_2"], ["B12_1", "B12_2"]] as dependencies
unwind PathNodesData as PathNodes
with PathNodes, dependencies
where all(x in dependencies where ((not x[0] in PathNodes) OR ((x[0] in PathNodes) AND (x[1] in PathNodes))))
return PathNodes

 Understanding that the OR operation is executed left-to-right and that the expression will stop evaluating if the first  operand is true, the predicate can be safely sampled to:

where all(x in dependencies where ((not x[0] in PathNodes) OR (x[1] in PathNodes)))

You will have to investigate the performance if you require this over a large number of paths or a large number of dependencies.

Thanks very much!! Will tests this asap. 
Was trying a very bad performing solution with a very fix path layout.
match (n:Tank {Name:'T0101'})-[:CanExportToBarge*]->(m:Pipe )-[:CanExportToBarge*]->(o:Pipe )-[:CanExportToBarge*]->(p:Pump )-[:CanExportToBarge*]->(q:Pipe )-[:CanExportToBarge*]->(r:Pipe )-[:CanExportToBarge*]->(s:Header )-[:CanExportToBarge*]->(z:Connection )
where left(o.Name,3) = left(q.Name,3)
return *

Works perfectly

 

MATCH (n:Tank )
CALL apoc.path.expandConfig(n, { relationshipFilter:"CanExportToBarge>",minLevel:1, maxLevel:12,labelFilter:"/Connection"}) 
YIELD path
with ([in nodes(path) | n.Name] ) as PathNodes, [["B11_1","B11_2"],
["B12_1","B12_2"],
["B13_1","B13_2"],
["B14_1","B14_2"],
["B15_1","B15_2"],
["B21_1","B21_2"],
["B22_1","B22_2"],
["B23_1","B23_2"],
["B24_1","B24_2"],
["B25_1","B25_2"],
["B31_1","B31_2"],
["B32_1","B32_2"],
["B33_1","B33_2"],
["B34_1","B34_2"],
["V11_1","V11_2"],
["V12_1","V12_2"],
["V13_1","V13_2"],
["V14_1","V14_2"],
["V15_1","V15_2"],
["V21_1","V21_2"],
["V22_1","V22_2"],
["V23_1","V23_2"],
["V24_1","V24_2"],
["V25_1","V25_2"],
["V31_1","V31_2"],
["V32_1","V32_2"],
["V33_1","V33_2"],
["V34_1","V34_2"]
] as dependencies
where all(in dependencies where ((not x[0] in PathNodes) OR (x[1] in PathNodes)))
return  PathNodes as ListofNodesInPath