Proofs: Greedy superstring merging
These are the human proofs of Greedy superstring merging, at full length.
GREEDY-SUPERSTRING returns a superstring and equals greedy merging full
This proof shows: correctness, termination.
Notation. Strings are over an alphabet with equality of letters only; indices are 0-based. For strings , is the length of the longest suffix of that is a prefix of . After line L1 the list has no repeats and no string inside another (substring-free), and for .
Lemma 1. If are in a substring-free list, then .
Proof. An overlap of length makes a suffix of , and one of length makes a prefix of ; both are excluded.
Lemma 2 (removed strings are covered). Each input string occurs in some .
Proof. A repeat of a string is covered by the copy that L1 keeps or by what covers that copy, so consider a removed string . It occurs inside a different string of the list, and then , since a different string of the same length cannot contain . If is removed too, repeat the argument with . The lengths grow strictly, so this stops at a kept string, and occurrence is transitive.
Invariant of the scan. Before and after each step of line L4: the accepted edges form vertex-disjoint paths (chains) covering all vertices; exactly for the tails, exactly for the heads; is the head of the chain with tail , and the tail of the chain with head .
Initially each vertex is a chain alone, and , . An accepted edge has a tail, a head, and , so the chain ending at and the chain starting at differ; the edge joins them into one path with head and tail , and line L6 records these two values. The other chains do not change. A rejected edge changes nothing.
Lemma 3 (one chain at the end). After the scan there is exactly one chain.
Proof. Suppose there are two chains at the end; let be the tail of and the head of . The pair was scanned, and . Chains only merge, and , are never reset, so at that time was a tail, a head, and they were in different chains (otherwise they would be in the same chain at the end). So , and line L5 accepted , which makes at the end, a contradiction.
Termination. Line L4 scans the finite list of pairs. By Lemma 3 the accepted edges form one path ; line L7 finds its unique head , and line L8 runs times.
Lemma 4 (the spelling). After the step of line L8 that reaches , ends with and contains , and .
Proof. True for . The step appends without its prefix of length . That prefix is a suffix of , hence of , so the new ends with , and its length grows by .
With Lemma 2, the output contains every input string. For the list is empty; we return the empty string, which is correct.
Lemma 5 (inherited overlaps). Let be a substring-free list, let with maximal over all pairs of distinct strings of , and let . Then for each : does not occur in , does not occur in , and .
Proof. is longer than , which is not inside , so is not inside . If occurs in at index , it is not inside , so it ends after ; it is not inside , which occupies the indices from on, so . Then the suffix of from , of length , is a prefix of , and , against the choice of . For the overlaps: a suffix of that is a prefix of has length less than , otherwise , a suffix of , would be inside (as a suffix of that prefix). So it is a suffix of , and . Symmetrically, a prefix of that is a suffix of has length less than , so it is a prefix of , and .
Equivalence with greedy merging. Consider the string form: keep the list of chains, each spelled as a string, and at each step merge the pair of chains with the largest overlap of their strings, ties broken by the smaller tail of , then the smaller head of . By Lemma 5 and induction on the number of merges, the list of chain strings stays substring-free, and the overlap of the string of with the string of equals . So the string form chooses, at each step, the valid edge (from a tail to the head of another chain) that is first in the order of line L3. The scan does the same: an edge rejected at line L5 stays invalid forever, since tails stop being tails, heads stop being heads, and chains only merge; so the next accepted edge is the first valid edge in the order. Both forms accept the same edges in the same order and spell the same string.