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)?
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)?
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.