I have a cyclic directed graph and I was wondering if there is any algorithm (preferably an optimum one) to make a list of common descendants between any two nodes? Something almost opposite of what Lowest Common Ancestor (LCA) does.
As user1990169 suggests, you can compute the set of vertices reachable from each of the starting vertices using DFS and then return the intersection.
If you're planning to do this repeatedly on the same graph, then it might be worthwhile first to compute and contract the strong components to supervertices representing a set of vertices. As a side effect, you can get a topological order on supervertices. This allows a data-parallel algorithm to compute reachability from multiple starting vertices at the same time. Initialize all vertex labels to {}
. For each start vertex v
, set the label to {v}
. Now, sweep all vertices w
in topological order, updating the label of w
's out-neighbors x
by setting it to the union of x
's label and w
's label. Use bitsets for a compact, efficient representation of the sets. The downside is that we cannot prune as with single reachability computations.