Proofs: Longest common substring via suffix tree
These are the human proofs of Longest common substring via suffix tree, at full length.
The deepest node with both marks spells a longest common substring full
This proof shows: correctness.
Let with and , and let . The suffix of at position is an -suffix if and a -suffix if . We use these facts about the suffix tree of : since occurs only at the end, no suffix is a prefix of another, so each suffix is the path label of exactly one leaf; for each node , the leaves below are the suffixes that have as a prefix; and every string that is a prefix of some suffix ends either at a node or inside an edge, and in the second case the leaves below that point are those below the lower end of the edge.
Lemma 1 (marks). After line L5, is the set of kinds of the suffixes that have as a prefix.
Proof. By induction in postorder. A leaf gets the kind of its own suffix at line L4. An inner node gets the union over its children, and the leaves below are the union of the leaves below its children.
Lemma 2 (common prefixes stay inside the strings). Let be a common prefix of an -suffix and a -suffix , with . Then and , so is a common substring of and .
Proof. The -suffix contains no , and its only is its last symbol. If contained , so would the -suffix, which is impossible. If contained , then would be the last symbol of the -suffix's prefix , so would be the whole -suffix; but is also a prefix of the -suffix, in which occurs only at the end after a (or, if , the -suffix starts with , and then the first symbols already differ, since ). In both cases would contain , which was excluded. So lies in . Since the -suffix has its first symbol outside at position , the prefix ends before it: . Likewise, ends before the of the -suffix.
Lemma 3 (every common substring is seen). If occurs in at and in at , then there is a node with and .
Proof. The -suffix at and the -suffix at both start with . Let be the node where ends, or the lower end of the edge inside which ends. Its label has as a prefix, so , and the leaves below are the suffixes that start with . Because ends at or inside the edge into , every suffix that starts with continues along that edge and so starts with : both leaves are below , and by Lemma 1, .
Theorem. Line L7 returns where is the length of a longest common substring of and , and .
Proof. If root has , then is an inner node (a leaf has one kind), and by Lemma 1 some -suffix and some -suffix have as a prefix. By Lemma 2, is a common substring, found at in and at in ; these are the positions that line L7 reports for . So , the length of a longest common substring. If , Lemma 3 applied to a longest common substring gives a node with both marks and depth at least , so . Line L6 keeps the maximum of over all such nodes, since the walk visits every node, so . If , Lemma 2 shows that no node other than the root has both marks, because its label would be a nonempty common substring. The root has depth 0, and line L6 does not choose it since starts at 0, so line L7 returns .