Proofs: Hunt-Szymanski algorithm
These are the human proofs of Hunt-Szymanski algorithm, at full length.
The Hunt-Szymanski thresholds give the LCS length and the links give an LCS full
This proof shows: correctness.
We use the pseudocode of the page. A match is a pair with . A chain of length is a list of matches with and . The common subsequences of and with a fixed embedding are exactly the chains: the chain spells . For define
with (the empty chain) and if no chain of length has all rows at most . So is the smallest such that and have a common subsequence of length .
Lemma 1 (monotone). If for some , then .
Proof. Drop the last match of a chain of length that ends in column . This gives a chain of length with all rows at most that ends in a column below (or the empty chain, with end 0).
Lemma 2 (row update). For and ,
Proof. A chain with rows at most either has all rows at most , or its last match is in row ; then it has only one match in row , since rows strictly increase, and the rest is a chain of length with rows at most that ends in a column below . Such a rest exists if and only if . Conversely, each such rest followed by is a chain. Taking the smallest last column over both kinds gives the formula.
Lemma 3 (one row of the loop). Suppose that before row , for all . Then after the loop of line L4 for row , for all .
Proof. Let the matches of row be with , the order of line L4, and let be the index that line L5 returns for . Writes of line L7 only lower an entry, and each value written in this row is some .
Claim. When is handled, each entry changed earlier in this row holds a value greater than , and each other entry holds . This holds because the values written before are , all greater than .
The index . For , , so by the claim is unchanged and . At , : if is unchanged, ; if it was changed, then is larger than each value written at , and these are larger than . With Lemma 1, is the smallest with .
The update. By Lemma 2, is a candidate for exactly when , that is, when . For the candidate does not matter, because . So . Lines L6 and L7 lower to if is smaller, so after the row each is this minimum, which is .
Lemma 4 (links). Whenever line L7 sets in row , is a chain of length whose last match is .
Proof. By induction over the writes. is the empty chain, which ends in column . At a write of line L7 in row , the entry has not been changed in row (by the claim in the proof of Lemma 3, since ), so was set in an earlier row, or is the empty chain, and by induction it is a chain whose last match has row below and column . Appending gives a chain of length .
Theorem. HUNT-SZYMANSKI returns a longest common subsequence of and .
Proof. Before row 1, : and for , which is line L2. By Lemma 3 and induction on , after the loop . A common subsequence of length exists if and only if , and no chain is longer than , so of line L8 is . By Lemma 4, is a chain of length , and lines L9 and L10 output its items in the order of the chain, which is a common subsequence of length .
Cost. Each match is handled once in line L4, with one binary search over at most entries in line L5 and other work, so the loop takes time, and at most link nodes are made.