Hunt–Szymanski algorithm
In computer science, the Hunt–Szymanski algorithm, also known as Hunt–McIlroy algorithm, is a solution to the longest common subsequence problem.[src] 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.[src] To this day, variations of this algorithm are found in incremental version control systems, wiki engines, and molecular phylogenetics research software.[src] The worst-case complexity for this algorithm is , but in practice is rather expected.[src]
In computer science, the Hunt–Szymanski algorithm, also known as Hunt–McIlroy algorithm, is a solution to the longest common subsequence problem.[src] 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.[src] To this day, variations of this algorithm are found in incremental version control systems, wiki engines, and molecular phylogenetics research software.[src] The worst-case complexity for this algorithm is , but in practice is rather expected.[src] ==Algorithm== The Hunt–Szymanski algorithm is a modification to a basic solution for the longest common subsequence problem, which has complexity .[src] The solution is modified so that there are lower time and space requirements for the algorithm when it is working with typical inputs.[src] ===Basic longest common subsequence solution=== ====Algorithm==== Let be the th element of the first sequence.[src] Let be the th element of the second sequence.[src] Let be the length of the longest common subsequence for the first elements of and the first elements .[src] 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 \ne B_j.[src] \end{cases} ====Example==== Consider the sequences and .[src] contains three elements: \begin{align} A_1 = a, \\ A_2 = b, \\ A_3 = c.[src] \end{align} contains three elements: \begin{align} B_1 = a, \\ B_2 = c, \\ B_3 = b.[src] \end{align} 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.[src] The algorithm correctly reports that the longest common subsequence of the two sequences is two elements long.[src] ====Complexity==== The above algorithm has worst-case time and space complexities of (see big O notation), where is the number of elements in sequence , and is the number of elements in sequence .[src] The Hunt–Szymanski algorithm modifies this algorithm to have a worst-case time complexity of and space complexity of , though it regularly beats the worst case with typical inputs.[src] ===Essential matches=== ==== -candidates==== The Hunt–Szymanski algorithm only considers what the authors call essential matches, or -candidates.[src] -candidates are pairs of indices such that \begin{align} A_i &= B_j, \\ P_{ij} &> \max(P_{i-1, j}, P_{i, j-1}).[src] \end{align} The second point implies two properties of -candidates: There is a common subsequence of length in the first elements of sequence and the first elements of sequence .[src] There are no common subsequences of length for any fewer than elements of sequence or elements of sequence .[src] ====Connecting -candidates==== To create the longest common subsequence from a collection of -candidates, a grid with each sequence's contents on each axis is created.[src] The -candidates are marked on the grid.[src] A common subsequence can be created by joining marked coordinates of the grid such that any increase in is accompanied by an increase in .[src] This is illustrated in the adjacent diagram.[src] 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.[src] Red dots represent -candidates that are considered by the Hunt–Szymanski algorithm, and the red line is the connection that creates a common subsequence of length 3.[src] ==See also== Levenshtein distance Longest common subsequence problem Wagner–Fischer algorithm ==References==[src]
32 sentences · 32 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Hunt–Szymanski algorithm." encyclopegia.org, 2026, https://encyclopegia.org/wiki/hunt-szymanski-algorithm. Accessed 02 December 2026.
Chicago
"Hunt–Szymanski algorithm." encyclopegia.org. Accessed December 2, 2026. https://encyclopegia.org/wiki/hunt-szymanski-algorithm.
APA
encyclopegia.org. (2026). Hunt–Szymanski algorithm. Retrieved December 2, 2026, from https://encyclopegia.org/wiki/hunt-szymanski-algorithm
Accessed December 2, 2026 · CC BY-SA 4.0 · citation page · JSON