Varliable length path with "path pattern" best practice

Hi there,
in cypher one can query variable length path through (b)-[:Reltype*]->(b) pattern
FOAF is a good example for that.

Now assume i have the following graph

i could search the varables and related function with patterns like

i can distingish between inputs outputs Functions and Variables (important)
this would kind of represent the following mathematical term.

X being a vector with x1,...,xn
Y being a vector with y1,....yn

well now one could have

in other words the result of a function is the input of an other function

how can i query all functions and intermediate variables for a specific output value?
(a:Variable)-[:in*]->(f:Funciton) .... would not work
i need something like ((i:Variable)-[:in]-(f:Function)-[:out]->(o:Variable))[0...*]

in this example relation and node filters on path extension (apoc) would be sufficient. But there are other graphs where label and relation filters are not sufficient. The order of relations would be important.


the order of relationships would be important
To clarify: Are you're requiring that after each in relationship there needs to be a out relationship. So two subsequent in would not be a valid pattern?
If that is your requirement, take a look at sequences for apoc.path.expandConfig at

Hi Stefan,
first, thank you very much for your reply.
yes the sequence is important
a sequence would be this example
the graph might have other relations/paths that i do not care of.

I words, search all paths that

  1. start with a Variable (V)
  2. as iinput (in) for a Funktion (F)
  3. having an output (out) Variable (V) (recurse to 1 or return this path)

reading about sequences how do a formulate the sequence then?
beginSequenceAtStart:false, sequence:'V,in>, F, out>,V

would be
match (x1:Variable) where"x1"
call apoc.path.expandConfig(x1,{sequence:"V,in>, F, out>,V"}) yield path as p return p




Close, just remember that the sequence repeats, so you don't need the V at the end, since it's at the start: sequence:'V, in>, F, out>'

If you want this to only return paths that end at V nodes, then you can use an end node label filter symbol > before the V: sequence:'>V, in>, F, out>'

Also beginSequenceAtStart:false changes the behavior and ordering of the sequence: the starting element is a relationship (which will not be a part of the subsequent sequence, it only gets us to the start of the sequence), but the ending element is still a relationship. For example, if the start node wasn't a :V node, but some other node, and wasn't important in the sequence, we could use:

beginSequenceAtStart:false, sequence:'in>, F, out>, >V, in>'

And likewise if the first relationship to the start of a sequence isn't in the sequence. Let's say you had a :begin relationship from the start node to the sequence, then you would use:

beginSequenceAtStart:false, sequence:'begin>, F, out>, >V, in>'
As noted, the first relationship here isn't really the start of the sequence, but gets us one hop away to the sequence's real start.


Hi Stefan, Andrew,
i appreciate your help. This makes a whole category of problems much easier to takle.

I tested and i am very happy with it.