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. 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 , but in practice is rather expected. History The algorithm was proposed by Harold S. Stone as a generalization of a special case solved by Thomas G. Szymanski. 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. The description of the algorithm appeared as a technical report by Hunt and McIlroy in 1976. The following year, a variant of the algorithm was finally published in a joint paper by Hunt and Szymanski. Algorithm The Hunt–Szymanski algorithm is a modification to a basic solution for the longest common subsequence problem which has complexity . 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 be the th element of the first sequence. Let be the th element of the second sequence. Let be the length of the longest common subsequence for the first elements of and the first elements . Example Consider the sequences and . contains three elements: contains three elements: 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 (see big O notation), where is the number of elements in sequence and is the number of elements in sequence . 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. Essential matches -candidates The Hunt–Szymanski algorithm only considers what the authors call essential matches, or -candidates. -candidates are pairs of indices such that: 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 . There are no common subsequences of length for any fewer than elements of sequence or elements of sequence . 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. The -candidates are marked on the grid. A common subsequence can be created by joining marked coordinates of the grid such that any increase in is accompanied by an increase in . This is illustrated in the adjacent diagram. Black dots represent candidates