# Hirschberg's algorithm

In computer science, Hirschberg's algorithm, named after its inventor, Dan Hirschberg, is a dynamic programming algorithm that finds the optimal sequence alignment between two strings. Optimality is measured with the Levenshtein distance, defined to be the sum of the costs of insertions, replacements, deletions, and null actions needed to change one string into the other. Hirschberg's algorithm is simply described as a more space-efficient version of the Needleman–Wunsch algorithm that uses divide and conquer. Hirschberg's algorithm is commonly used in computational biology to find maximal global alignments of DNA and protein sequences.

## Algorithm information

Hirschberg's algorithm is a generally applicable algorithm for optimal sequence alignment. BLAST and FASTA are suboptimal heuristics. If $X$ and $Y$ are strings, where $\operatorname {length} (X)=n$ and $\operatorname {length} (Y)=m$, the Needleman–Wunsch algorithm finds an optimal alignment in $O(nm)$ time, using $O(nm)$ space. Hirschberg's algorithm is a clever modification of the Needleman–Wunsch Algorithm, which still takes $O(nm)$ time, but needs only $O(\min\{n,m\})$ space and is much faster in practice. One application of the algorithm is finding sequence alignments of DNA or protein sequences. It is also a space-efficient way to calculate the longest common subsequence between two sets of data such as with the common diff tool.
The Hirschberg algorithm can be derived from the Needleman–Wunsch algorithm by observing that:
* one can compute the optimal alignment score by only storing the current and previous row of the Needleman–Wunsch score matrix;
* if $(Z,W)=\operatorname {NW} (X,Y)$ is the optimal alignment of $(X,Y)$, and $X=X^{l}+X^{r}$ is an arbitrary partition of $X$, there exists a partition $Y^{l}+Y^{r}$ of $Y$ such that $\operatorname {NW} (X,Y)=\operatorname {NW} (X^{l},Y^{l})+\operatorname {NW} (X^{r},Y^{r})$.

## Algorithm description

$X_{i}$ denotes the i-th character of $X$, where $1\leqslant i\leqslant \operatorname {length} (X)$. $X_{i:j}$ denotes a substring of size $j-i+1$, ranging from the i-th to the j-th character of $X$. $\operatorname {rev} (X)$ is the reversed version of $X$.
$X$ and $Y$ are sequences to be aligned. Let $x$ be a character from $X$, and $y$ be a character from $Y$. We assume that $\operatorname {Del} (x)$, $\operatorname {Ins} (y)$ and $\operatorname {Sub} (x,y)$ are well defined integer-valued functions. These functions represent the cost of deleting $x$, inserting $y$, and replacing $x$ with $y$, respectively.
We define $\operatorname {NWScore} (X,Y)$, which returns the last line of the Needleman–Wunsch score matrix $\mathrm {Score} (i,j)$:
Note that at any point, $\operatorname {NWScore}$ only requires the two most recent rows of the score matrix. Thus, $\operatorname {NWScore}$ is implemented in $O(\min\{\operatorname {length} (X),\operatorname {length} (Y)\})$ space.
The Hirschberg algorithm follows:
In the context of observation (2), assume that $X^{l}+X^{r}$ is a partition of $X$. Index $\mathrm {ymid}$ is computed such that $Y^{l}=Y_{1:\mathrm {ymid} }$ and $Y^{r}=Y_{\mathrm {ymid} +1:\operatorname {length} (Y)}$.

## Example

Let
${\begin{aligned}X&={\text{AGTACGCA}},\\Y&={\text{TATGC}},\\\operatorname {Del} (x)&=-2,\\\operatorname {Ins} (y)&=-2,\\\operatorname {Sub} (x,y)&={\begin{cases}+2,&{\text{if }}x=y\\-1,&{\text{if }}x\neq y.\end{cases}}\end{aligned}}$
The optimal alignment is given by
Indeed, this can be verified by backtracking its corresponding Needleman–Wunsch matrix:
One starts with the top level call to $\operatorname {Hirschberg} ({\text{AGTACGCA}},{\text{TATGC}})$, which splits the first argument in half: $X={\text{AGTA}}+{\text{CGCA}}$. The call to $\operatorname {NWScore} ({\text{AGTA}},Y)$ produces the following matrix:
Likewise, $\operatorname {NWScore} (\operatorname {rev} ({\text{CGCA}}),\operatorname {rev} (Y))$ generates the following matrix:
Their last lines (after reversing the latter) and sum of those are respectively
The maximum (shown in bold) appears at ymid = 2, producing the partition $Y={\text{TA}}+{\text{TGC}}$.
The entire Hirschberg recursion (which we omit for brevity) produces the following tree:
The leaves of the tree contain the optimal alignment.