Proofs: Suffix automaton
These are the human proofs of Suffix automaton, at full length.
SAM-EXTEND keeps the end-position classes, transitions and suffix links full
This proof shows: correctness.
Let . For a string , for , and . Write if ; the classes are the substrings of grouped by .
Lemma 1. For substrings with : if is a suffix of then ; otherwise . Proof. If is in both, then and both end at , so the shorter is a suffix of the longer.
Lemma 2 (classes are suffix intervals). A class has a longest string , and is the set of suffixes of with lengths in an interval . Proof. By Lemma 1, all members of are suffixes of the longest one. If and are in and is a suffix of longer than , then by Lemma 1, so .
Define as the class of the suffix of of length ; by Lemma 2 it is the class of the longest suffix of the strings of that is not in , and .
Lemma 3 (transitions are well defined). If and is a substring, then is a substring and , since .
So the automaton with one state per class, transitions , and accepting states the classes that contain a suffix of is deterministic and accepts exactly the suffixes of (reading from the initial state succeeds exactly when is a substring, and ends in ). It is minimal: two substrings have, say, , and then is a suffix of while is not, so the states are distinguishable (Myhill-Nerode), and all non-substrings go to one dead state that is not stored.
The claim. After each call, the stored states, with , and , are exactly the classes of the current text with , as defined, and transitions as in Lemma 3; and is the class of the whole text. This holds for : one class , no transitions.
One step. Assume the claim for , of length , and let . For each substring of ,
with when is not a substring of . The suffixes of are and for the suffixes of . By the claim and Lemma 2, the suffixes of are the strings of the states on the link path , with lengths decreasing along the path, and every length from down to 0 appears once.
Line L1. will be the class of , with .
Line L3. Let be the states without a transition on . For in such a state, is not a substring of (by Lemma 3 and the claim), so , and belongs to the class of . Line L3 sets , which is right by Lemma 3.
Line L4, case $p_t = $ NIL. Then no occurred in , so does not occur in ; the class of holds all nonempty suffixes of , its is 1, and its link is . No old class changes: the only old strings whose changes are suffixes of , and only is old among them, whose class is always .
Otherwise, let , , and . The strings for in are in (Lemma 3), and every shorter suffix is a substring of (a suffix of ). So the class of holds exactly the suffixes of longer than , its is , and its link must be the class in of .
Case (line L5). Then , a suffix of ; by Lemma 2 every string of is a suffix of and so of , and they all gain . So remains one class, unchanged in its strings, and . Every other old class either consists of suffixes of (those on the link path of , whose strings are suffixes of ; they all gain and stay classes) or of non-suffixes (unchanged). Transitions out of old classes into are exactly those set in line L3, since for an old lies in only if it is a suffix of longer than , which means and in .
Case (lines L6 to L8). The strings of with length at most are suffixes of , hence suffixes of , and gain . The strings of with length greater than are not suffixes of : such a string , being in , has in a state with a transition to , and if were a suffix of , would be a suffix of longer than with a transition on , so it would be in one of , which have no such transition. So splits into , the strings of lengths to , and , the rest. The clone is , with and the old link of ; becomes , with , so its link is the clone; and is the clone, the class of . Both parts take the transitions of as they are after line L3, which line L6 copies: by Lemma 3 in , applied to each part, the extensions of the strings of one part lie in one class, and that class contains the old target of the transition of on (an old class or ); the only class that is split in this step is itself, and transitions into are handled next. An incoming transition must now go to the part that contains . If or is not a suffix of , then is not a suffix of , so it is in . If and is a suffix of , then is in one of , and is in exactly when it goes to : along the link path from , the states whose transition on is form a prefix (a shorter has shorter, so once falls below it is in a class of the link path of , never again in ). Line L7 redirects exactly these to the clone. All other old classes are unchanged in their strings, by the same argument as in the first case.
Line L9. , the class of .
So the claim holds for . Taking all states as accepting gives the substrings of , and the accepting states of the suffix automaton are those on the link path of , as stated in (A4).