How to define LCA(Least Common Ancestor)(of two nodes) in case of a graph?

239 views Asked by At

I googled and tried to find about LCA(of two nodes) in a graph, but unfortunately, I didn't find much descriptive and understandable content.

So please can someone elaborate the LCA in a graph(both directed and undirected)?

1

There are 1 answers

0
ravenspoint On

To find the closest common ancestor to two nodes in a directed graph, do breadth first search upwards from each node. Add each node as it is visited to a vector of nodes, one for each search. When the searches are completed find the the first common node in the two vectors.

Optimization: pause the searches when they increment their depth. If a common node has been found, stop, otherwise continue to the next depth.