efficiently calculate nearest common ancestor in a family tree (each person has two parents)

I’m well aware of ways to efficiently calculate the lowest common ancestor in a tree of nodes which converge to a single root (ie, each node has only one parent). Just iterate back to root for each person, then walk back from root tossing off anything common.

In a matriarchal society, for example, this could be used to quickly calculate how any two people are related as long as only the mothers are considered.

But if both parents are considered, eg mother AND father, then the algorithm just described breaks down.

So I wondered is there an algorithm to tell two people how they are related in a family tree where both parents are considered? For example, see the Icelandic genealogy app (https://www.islendingabok.is/) which does precisely that. How’s it done, algorithmically speaking?