What Is Dynamic Programming?
What Is Dynamic Programming?
Sean R. Eddy
Howard Hughes Medical Institute & Department of Genetics,
Washington University School of Medicine
4444 Forest Park Blvd., Box 8510
Saint Louis, Missouri 63108 USA
[email protected]
June 8, 2004
1
The best way to understand how dynamic programming works is to see an
example. Conveniently, optimal sequence alignment provides an example that is
both simple and biologically relevant.
2
Crucially, our scoring system allows us to define the score of these three cases
recursively, in terms of optimal alignments of the preceding subsequences. Let
S(i, j) be the score of the optimum alignment of sequence prefix x1 ..xi to prefix
y1 ..yj . The score for case (1) above is the score σ(xM , yN ) for aligning xM to yN ,
plus the score S(M − 1, N − 1) for an optimal alignment of everything else up to
this point. Case (2) is the gap penalty γ plus the score S(M − 1, N ); case (3) is
the gap penalty γ plus the score S(M, N − 1).
This works because the problem breaks into independently optimizable pieces,
since the scoring system is strictly local to one aligned column at a time. That
is, for instance, the optimal alignment of x1 ..xM −1 to y1 ..yN −1 is unaffected by
adding on the aligned residue pair xM , yN , and likewise, the score σ(xM , yN ) we
add on is independent of the previous optimal alignment.
So, to calculate the score of the three cases, we will need to know three more
alignment scores for three smaller problems:
S(M − 1, N − 1), S(M − 1, N ), S(M, N − 1).
And to calculate those, we need the solutions for nine still smaller problems:
S(M − 2, N − 2), S(M − 2, N − 1), S(M − 1, N − 2),
S(M − 2, N − 1), S(M − 2, N ), S(M − 1, N − 1),
S(M − 1, N − 2), S(M − 1, N − 1), S(M, N − 2).
and so on, until we reach tiny alignment subproblems with obvious solutions (the
score S(0, 0) for aligning nothing to nothing is zero).
Thus, we can write a general recursive definition of all our partial optimal
alignment scores S(i, j):
S(i − 1, j − 1) + σ(xi , yj )
S(i, j) = max S(i − 1, j) + γ (1)
S(i, j − 1) + γ.
3
memorizes the solutions of optimal subproblems in an organized, tabular form (a
dynamic programming matrix), so that each subproblem is solved just once.
For the pairwise sequence alignment algorithm, the optimal scores S(i, j) are
tabulated in a two-dimensional matrix, with i running from 0..M and j running
from 0..N , as showing in Figure 1. As we calculate solutions to subproblems
S(i, j), their optimal alignment scores are stored in the appropriate (i, j) cell of
the matrix.
4
Fine. But what do I really need to know?
Dynamic programming is guaranteed to give you a mathematically optimal (high-
est scoring) solution. Whether that corresponds to the biologically correct align-
ment is a problem for your scoring system, not for the algorithm.
Similarly, the dynamic programming algorithm will happily align unrelated
sequences. (The two sequences in Figure 1 might look well-aligned; but in fact,
they are unrelated, randomly generated DNA sequences!) The question of when a
score is statistically significant is also a separate problem, requiring clever statis-
tical theory.
Dynamic programming is surprisingly computationally demanding. You can
see that filling in the matrix takes time proportional to M N . Alignment of two
200-mers will take four times as long as two 100-mers. This is why there is so
much research devoted to finding good, fast approximations to dynamic program-
ming alignment, like the venerable workhorses BLAST and FASTA, and newer
programs like BLAT and FLASH.
Only certain scoring systems are amenable to dynamic programming. The
scoring system has to allow the optimal solution to be broken up into independent
parts, or else it can’t be dealt with recursively. The reason that programs use
simple alignment scoring systems is that we’re striking a reasonable compromise
between biological realism and efficient computation.
Further study.
To study a working example, you can download a small, bare-bones C imple-
mentation of this algorithm from http://blah-blah.blah. I used this C
program to generate Figure 1.
References
[1] R. E. Bellman. Eye of the Hurricane: An Autobiography. World Scientific,
1984.
5
Figure legend
The filled dynamic programming matrix for two randomly generated sequences,
x = TTCATA and y = TGCTCGTA, for a scoring system of +5 for a match, −2
for a mismatch, and −6 for each insertion or deletion. The cells in the optimum
path are shown in red. Arrowheads are “traceback pointers”, indicating which of
the three cases were optimal for reaching each cell. (Some cells can be reached by
two or three different optimal paths of equal score: whenever two or more cases
are equally optimal, dynamic programming implementations usually choose one
case arbitrarily. In this example, though, the optimal path is unique.)
6
dynamic programming matrix:
j (sequence y)
0 1 2 3 4 5 6 7 8 =N
T G C T C G T A
i 0 0 -6 -12 -18 -24 -30 -36 -42 -48
3 C -18 -7 -3 8 2 3 -3 -9 -15
4 A -24 -13 -9 2 6 0 1 -5 -4