We’ve got a tree structure with parents and children, and we want to figure out, starting from two children, which ancestor is the most recent common ancestor (MRCA). Let’s start by constructing an example graph (I usually just type this out into the console!). The root will be the common ancestor of all the others, so we’ll always get an answer in this case.
1 2 3 4 5 6 7 8 9 10
So, an initial attempt might be something like this, where we find common ancestors via variable length paths–let’s try it with nodes
1 2 3
Cool, well, we got our three ancestors. How can we figure out which one is the most recent? In reality, this is as easy as sorting by the length of the returned path! So we define a
path variable so we can check the length of the whole path pattern we’re matching easily. Then we just
limit by one, and we’re done.
1 2 3 4 5
Try it for other children!
The original question was posted on Stack Overflow.
If you want to see more Cypher stuff, check out my Cypher page.