0

I have two directed acyclic graph and I need to compute the longest common subsequence (LCS) of these graphs. For two strings/subsequences I use LCS algorithm using dynamic programming (DP) but how can I modify this algorithm to the graphs?

Any idea?

whole task:

Let G is a directed acyclic graph in which each vertex is labeled with a symbol from a finite alphabet. Different vertices may be marked with the same symbol. Each directed path in G has trace formed by concatenating the symbol of vertices from the path. Sequence of graph G is a subsequence of trace of some directed path in G.

Design an efficient algorithm that computes the longest common sequence of two given directed acyclic graph.

Example: strings DYNAMIC, PROGRAM and DEPTHFIRST are sequences of oriented acyclic graph of the picture. String PROGRAM is its trace.

enter image description here

Pepa Zapletal
  • 2,879
  • 3
  • 39
  • 69
  • 2
    You fail to demonstrate any kind of research effort. Also this seems to be a repost of another question that has been deleted by now – Niklas B. May 12 '14 at 10:13
  • 1
    Here it is: http://stackoverflow.com/questions/23563825/algorithm-compute-the-longest-sequence-of-two-dags – Niklas B. May 12 '14 at 10:17
  • I try to search information, excercies,... but I don´t know how to modify the LCS algorithm. So some hint would be helpful, not this comments... – Pepa Zapletal May 12 '14 at 10:20
  • Think about the implicit graph of node pairs (x, y) with x being a node from the first DAG and y from the second. add edges (A(x), y), (x, A(y)) and (A(x), A(y)) where A is your adjacency lists. Assign edge weights so that a longest path in this graph corresponds to the LCS, like in the case of strings. Then solve the longest path problem. – Niklas B. May 12 '14 at 10:26

1 Answers1

1

Let A be the first DAG and B the second DAG. For any two nodes a in A and b in B having, define the length of the longest common subsequence that starts from a in A and from b in B to be

LCS(a,b) =  0 + max LCS(a',b') for any pair (a',b') s.t. a->a' and b->b'
                               or a=a' and b->b', or b=b' and a->a'
                               if a and b do NOT share same label

            1 + max LCS(a',b') for any pair (a',b')
                               s.t. a -> a' and b -> b'
                               if a and b DO share same label

Then apply dynamic programming over |A| x |B| pairs (a, b) and read the value for the pairs that pertain to DAG sources ("start nodes").

Antti Huima
  • 25,136
  • 3
  • 52
  • 71
  • I think LCS(a,b) should be 0 if a and b do NOT share same label. The way you wrote in a (a1)D->(a2)A->(a3)B and (b1)D->(b2)C->(b3)A->(b4)B graphs LCS(a2,b2) would be 0+LCS(a2,b3)=1+LCS(a3,b3)=2. Then LCS(a1,b1) would be 1 + LCS(a2,b2) = 3, which is wrong. – Kicsi May 12 '14 at 14:26