1

Suppose you have given dp table for string X = "AGGGCT" and string Y = "AGGCA"

m = length of X + 1

n = length of Y + 1

            0 1 2 3 4 5
            1 0 1 2 3 4
            2 1 0 1 2 3
dp[m][n] =  3 2 1 0 1 2
            4 3 2 1 1 2
            5 4 3 2 1 2
            6 5 4 3 2 2

and you want to reconstruct three strings as follows

string row1 = "AGGGCT" ;
string row2 = "||| | " ;
string row3 = "AGG-CA" ;

How to recontruct strings row1, row2 and row3, if possible post code in C/C++/Java.

Vikrant Singh
  • 669
  • 1
  • 6
  • 18

1 Answers1

0

I think this page can be a good starting point:

http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Java

You have to make a few modifications, but the core idea should be to store in the "min" which case was choosed for a given (i,j), and before the return you can walk through the matrix backwards starting by distance[str1.length()][str2.length()] step-by-step. If in the steps the distances are the same you show a |, if they differ but stepping diagonal then it was a change step, otherwise if vertical/horizontal a remove/add. You can store this "backwards" information in a string and later display it in a reverse order.

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56