Needleman-Wunsch
The Needleman-Wunsch algorithm, published by Saul Needleman and Christian Wunsch in 1970, is a dynamic programming algorithm for global sequence alignment: finding the optimal alignment of two entire sequences from end to end. It is the foundational algorithm of computational biology and the ancestor of every alignment method that followed.
The algorithm fills an (n+1) × (m+1) matrix where each cell S[i][j] represents the optimal alignment score for the prefixes A[1..i] and B[1..j]. The recurrence:
S[i][j] = max(
S[i-1][j-1] + match_score(A[i], B[j]), S[i-1][j] + gap_penalty, S[i][j-1] + gap_penalty
)
The first term extends the alignment by matching or mismatching the current characters; the second and third terms insert a gap in one sequence or the other. The traceback from the bottom-right corner to the top-left reconstructs the optimal alignment.
The Scoring Matrix as Prior Knowledge
The match/mismatch scores encode biological knowledge about substitution frequencies. The PAM and BLOSUM matrices, derived from empirical observations of protein evolution, assign higher scores to substitutions that occur frequently in nature and lower scores to rare or damaging substitutions. The gap penalty encodes prior knowledge about insertion and deletion rates. The alignment is therefore not an objective computation but a Bayesian inference: the optimal alignment is the maximum a posteriori estimate under a model of molecular evolution.
This framing reveals that the Needleman-Wunsch algorithm is not merely a string-matching technique. It is a method for inferring evolutionary history from present-day sequences. The alignment it produces is a hypothesis about the mutational events that transformed a common ancestor into the two observed sequences.
Limitations and Descendants
The Needleman-Wunsch algorithm is O(nm) in time and space, which is tractable for typical protein sequences but becomes prohibitive for whole-genome alignments. It also requires the entire sequences to be aligned; for finding local regions of similarity (e.g., shared domains), the Smith-Waterman algorithm is preferred. For multiple sequences, heuristic methods like progressive alignment and the more recent MUSCLE algorithm are used.
The algorithm's structure — dynamic programming on a matrix with traceback — is identical to that of Floyd-Warshall, the Viterbi algorithm, and edit distance. This is not coincidence. The Needleman-Wunsch algorithm is the shortest-path problem on a grid graph, and the optimal alignment is the path of maximum weight from one corner to the opposite corner.
The Needleman-Wunsch algorithm is often taught as a bioinformatics technique, but that is a parochial framing. It is a method for finding the optimal correspondence between two ordered structures under a cost function. The biologist uses it to align DNA; the version-control system uses the same structure to diff code; the speech recognizer uses it to align phonemes. The algorithm does not care what it aligns. It cares only about the structure of the problem.