Proofs: Generalized suffix automaton
These are the human proofs of Generalized suffix automaton, at full length.
The insertion with the extra check builds the generalized suffix automaton full
This proof shows: correctness, termination, time.worst.
Notation. Let be a list of strings, positions 0-based. For a nonempty string , is the set of pairs such that is a suffix of ; is a substring of if this set is nonempty. Two substrings are equivalent if they have the same end set. The generalized suffix automaton has a root for the empty string and one state for each class; is the length of the longest string of ; is the class of the longest suffix of the longest string of that is not in (the root if there is none); and is the class of for any , defined when is a substring.
Lemma 1 (classes). (a) If is a suffix of and both are substrings, then . (b) Each class consists of the suffixes of its longest string with lengths in an interval . (c) is well defined. (d) A prefix is the longest string of its class.
Proof. (a) is clear. (b) If and are in one class with , take an end of both: both are suffixes of , so is a suffix of ; and any between them in length that is a suffix of is in the class, by (a) on both sides. (c) If , then . (d) A string longer than cannot end at .
The step. Suppose the structure is , where the last string of is the prefix (possibly empty, with ), and is the class of (the root if is empty). Let be with extended by . Then if is a suffix of , and otherwise. Let be the length of the longest suffix of that is a substring of ( and empty if there is none).
Lemma 2 (what changes). (i) The suffixes of longer than form one new class with end set , if . (ii) Among the old classes, only the class of can split, and it splits if and only if : into the strings of length greater than , with the old end set, and those of length at most , with the new end. (iii) Every other old class keeps its strings.
Proof. (i) These suffixes are new substrings, and each ends only at . (ii, iii) A class with no suffix of keeps its end set. Let be a suffix of of length , with the longest string of its class. Take an end of ; it is also an end of , so of , so and are suffixes of one prefix. If , is a suffix of , and by Lemma 1(b) the whole class of consists of suffixes of , which all gain the same end. If , then is a suffix of of length between and , so is in the class of ; that is the class . In , the strings of length at most are suffixes of , and the longer ones are not, since they are not suffixes of and is the longest old suffix of .
Lemma 3 (the clone keeps the transitions). If splits into (long part) and (short part), then and for every symbol , and the targets are old classes that do not split.
Proof. For , a pair cannot be an end of , since has length . So , the shifted old ends of , which are the shifted ends of all of . The target class contains strings of length or less that are not suffixes of ending at the new place, so it is not , and by Lemma 2 it keeps its strings.
Theorem 1 (correctness). GSA-EXTEND turns into and returns the class of .
Proof. By Lemma 1(d), . The transitions that change are for the classes whose strings have in a new or split class: the suffixes of with (whose is new), and those with when splits (whose moves to ). The suffix links change only for the new class, for and for : a class with points to the longest string of , which stays in the long part. The classes of the suffixes of are the states on the suffix-link path from , by Lemma 1(b) applied to the prefix .
Case 1: exists (lines L1 to L3). Then is a substring of , so , no class is new, and is the class of . If , nothing splits and no transition or link changes, so is returned correctly. Otherwise SPLIT makes the clone with , the old link of and the transitions of (Lemma 3), moves from to the clone on the path from while it pointed to , which are the classes with , and sets to the clone. The clone is the class of , and it is returned.
Case 2: no transition (lines L4 to L9). Then and the new class has . Line L5 walks the path from and sets while has no transition on ; these are the classes whose strings have , since exists for all members of a class or for none. If the walk passes the root, no suffix of was a substring, , and is the root (L6). Otherwise the walk stops at the class of the suffix of of length , and , since a longer member of would give an old suffix of longer than . So is the class of . If , nothing splits and (L7). Otherwise SPLIT with splits as in Case 1, starting the walk at , and is the clone (L8). The new class is the class of and is returned (L9).
Theorem 2 (build). Inserting the strings one by one, each from the root, gives .
Proof. of the empty list, and of a list that ends with an empty string, is the root alone. Starting a new string adds an empty last string, which changes no end set, and = root is its class. Theorem 1 then applies at each symbol.
Theorem 3 (time). For a constant alphabet, the build takes time.
Proof. Each call creates at most two states, so there are states, and the work outside the loops of L5 and L11 is per call. For one string, Blumer et al. bound the total work of these loops by with a potential: the number of steps of the loop of L5 is paid by the drop in the depth, in the suffix-link tree, of the link of the state returned, compared with the depth of , and the redirect loop of L11 by the same drop for the clone. The same accounting holds here within one string, and starting a new string at the root sets the depth to 0, which only lowers the potential. So the total is . With a transition map of symbols, each transition access adds a factor of for sorted maps, or expected time for hashing.