The aim is to find the best alignment of two strings. The dynamic programming approach is to reduce the program to some simpler ones. Unlike in a divide-and-conquer strategy, we solve the set of programs in a clever order: from smaller to larger ones.
To align string S(1..i) and T(1..j), we consider three case:
The maximal score from the three cases is chosen as the score M(i,j) for the alignment S(1..i) and T(1..j). In formula
{ | M(i-1, j-1) + f(S(i), T(j)) | } | |
M(i, j) = max | M(i-1, j) + f(S(i), ␣) | ||
M(i, j-1) + f(␣, T(j)) |
Now, instead of solving the problem recursively, i.e., M(i, j) → {M(i-1, j-1), M(i-1, j), M(i, j-1)} → ..., we start from smaller problems. First, we solve all M(1, 1), M(1, 2), ..., M(1, m), i.e., aligning the first character of S with parts of T. We then proceed to M(2, 1), M(2, 2), ..., M(2, m). We repeat the process till M(n, m) is reached.
Download: