# Hunt–Szymanski algorithm

In computer science, the Hunt–Szymanski algorithm,[1][2] also known as Hunt–McIlroy algorithm, is a solution to the longest common subsequence problem. It was one of the first non-heuristic algorithms used in diff which compares a pair of files each represented as a sequence of lines. To this day, variations of this algorithm are found in incremental version control systems, wiki engines, and molecular phylogenetics research software.

The worst-case complexity for this algorithm is O(n2 log n), but in practice O(n log n) is rather expected.[3][4]

## History

The algorithm was proposed by Harold S. Stone as a generalization of a special case solved by Thomas G. Szymanski.[5][6][7] James W. Hunt refined the idea, implemented the first version of the candidate-listing algorithm used by diff and embedded it into an older framework of Douglas McIlroy.[5]

The description of the algorithm appeared as a technical report by Hunt and McIlroy in 1976.[5] The following year, a variant of the algorithm was finally published in a joint paper by Hunt and Szymanski.[5][8]

## Algorithm

The Hunt–Szymanski algorithm is a modification to a basic solution for the longest common subsequence problem which has complexity O(n2). The solution is modified so that there are lower time and space requirements for the algorithm when it is working with typical inputs.

### Basic longest common subsequence solution

#### Algorithm

Let Ai be the ith element of the first sequence.

Let Bj be the jth element of the second sequence.

Let Pij be the length of the longest common subsequence for the first i elements of A and the first j elements B.

${\displaystyle P_{ij}={\begin{cases}0&{\text{ if }}\ i=0{\text{ or }}j=0\\1+P_{i-1,j-1}&{\text{ if }}A_{i}=B_{j}\\\max(P_{i-1,j},P_{i,j-1})&{\text{ if }}A_{i}\neq B_{j}\end{cases}}}$

#### Example

Consider the sequences A and B.

A contains three elements:

{\displaystyle {\begin{aligned}A_{1}=a\\A_{2}=b\\A_{3}=c\end{aligned}}}

B contains three elements:

{\displaystyle {\begin{aligned}B_{1}=a\\B_{2}=c\\B_{3}=b\end{aligned}}}

The steps that the above algorithm would perform to determine the length of the longest common subsequence for both sequences are shown in the diagram. The algorithm correctly reports that the longest common subsequence of the two sequences is two elements long.

#### Complexity

The above algorithm has worst-case time and space complexities of O(mn) (see big O notation), where m is the number of elements in sequence A and n is the number of elements in sequence B. The Hunt–Szymanski algorithm modifies this algorithm to have a worst-case time complexity of O(mn log m) and space complexity of O(mn), though it regularly beats the worst case with typical inputs.

### Essential matches

#### k-candidates

The Hunt–Szymanski algorithm only considers what the authors call essential matches, or k-candidates. k-candidates are pairs of indices (i, j) such that:

${\displaystyle A_{i}=B_{j}}$
${\displaystyle P_{ij}>max(P_{i-1,j},P_{i,j-1})}$

The second point implies two properties of k-candidates:

• There is a common subsequence of length k in the first i elements of sequence A and the first j elements of sequence B.
• There are no common subsequences of length k for any fewer than i elements of sequence A or j elements of sequence B.

#### Connecting k-candidates

To create the longest common subsequence from a collection of k-candidates, a grid with each sequence's contents on each axis is created. The k-candidates are marked on the grid. A common subsequence can be created by joining marked coordinates of the grid such that any increase in i is accompanied by an increase in j.

This is illustrated in the adjacent diagram.

Black dots represent candidates that would have to be considered by the simple algorithm and the black lines are connections that create common subsequences of length 3.

Red dots represent k-candidates that are considered by the Hunt–Szymanski algorithm and the red line is the connection that creates a common subsequence of length 3.