The answer to My question might be obvious, and I know that obvious answer on paper. I mean when it comes to some examples I understand why we aren't allowed to have loops to run the Lowest Common Ancestor algorithm, but I have problems understanding the papers written for solution of LCA in DAGs. and so which part of solution stops us from using it on cyclic graphs..
what I'm willing to know and would be thankful to be informed about:
- can you explain one of solutions to LCA problem in DAGs, without too much formuls?
- can you determine which step has problems with cylcles and why?
in my problem, pairs of nodes to find their LCA are not inside one loop, so I think there might be a way to solve that..
Thanks in advance
The problem with cycles starts with the definition itself.
The LCA of
u
andv
is defined as a set of such nodesz
thatz
is reachable fromu
andv
andz
is not reachable from any other nodez'
such thatz'
is reachable fromu
andv
.It may not exist in a cyclic graph. For instance, if there's a cycle
1->2->3
and two other nodes and edges:4->3
and5->3
, there is no LCA for4
and5
(as all1, 2
and3
are reachable from both of them, but they're also reachable from each other).You could find all nodes that are reachable from
u
andv
(using a dfs or something else) and then check of there is such a nodez
that is reachable from both of them but not from any other node that satisfies this condition (it would work in polynomial time).So it's more about having a meaningful a definition of lowest common ancestor than about being able to compute it (as I have shown above, you can compute something like that, but it might not make sense even for two nodes which are not on the same cycle).