Suffix automaton | |||||||||
---|---|---|---|---|---|---|---|---|---|
Type | Substring index | ||||||||
Invented | 1983 | ||||||||
Invented by | Anselm Blumer; Janet Blumer; Andrzej Ehrenfeucht; David Haussler; Ross McConnell | ||||||||
Time complexity in big O notation | |||||||||
|
In computer science, a suffix automaton is an efficient data structure for representing the substring index of a given string which allows the storage, processing, and retrieval of compressed information about all its substrings. The suffix automaton of a string [math]\displaystyle{ S }[/math] is the smallest directed acyclic graph with a dedicated initial vertex and a set of "final" vertices, such that paths from the initial vertex to final vertices represent the suffixes of the string.
In terms of automata theory, a suffix automaton is the minimal partial deterministic finite automaton that recognizes the set of suffixes of a given string [math]\displaystyle{ S=s_1 s_2 \dots s_n }[/math]. The state graph of a suffix automaton is called a directed acyclic word graph (DAWG), a term that is also sometimes used for any deterministic acyclic finite state automaton.
Suffix automata were introduced in 1983 by a group of scientists from the University of Denver and the University of Colorado Boulder. They suggested a linear time online algorithm for its construction and showed that the suffix automaton of a string [math]\displaystyle{ S }[/math] having length at least two characters has at most [math]\displaystyle{ 2|S| - 1 }[/math] states and at most [math]\displaystyle{ 3|S| - 4 }[/math] transitions. Further works have shown a close connection between suffix automata and suffix trees, and have outlined several generalizations of suffix automata, such as compacted suffix automaton obtained by compression of nodes with a single outgoing arc.
Suffix automata provide efficient solutions to problems such as substring search and computation of the largest common substring of two and more strings.
The concept of suffix automaton was introduced in 1983[1] by a group of scientists from University of Denver and University of Colorado Boulder consisting of Anselm Blumer, Janet Blumer, Andrzej Ehrenfeucht, David Haussler and Ross McConnell, although similar concepts had earlier been studied alongside suffix trees in the works of Peter Weiner,[2] Vaughan Pratt[3] and Anatol Slissenko.[4] In their initial work, Blumer et al. showed a suffix automaton built for the string [math]\displaystyle{ S }[/math] of length greater than [math]\displaystyle{ 1 }[/math] has at most [math]\displaystyle{ 2|S|-1 }[/math] states and at most [math]\displaystyle{ 3|S|-4 }[/math] transitions, and suggested a linear algorithm for automaton construction.[5]
In 1983, Mu-Tian Chen and Joel Seiferas independently showed that Weiner's 1973 suffix-tree construction algorithm[2] while building a suffix tree of the string [math]\displaystyle{ S }[/math] constructs a suffix automaton of the reversed string [math]\displaystyle{ S^R }[/math] as an auxiliary structure.[6] In 1987, Blumer et al. applied the compressing technique used in suffix trees to a suffix automaton and invented the compacted suffix automaton, which is also called the compacted directed acyclic word graph (CDAWG).[7] In 1997, Maxime Crochemore and Renaud Vérin developed a linear algorithm for direct CDAWG construction.[1] In 2001, Shunsuke Inenaga et al. developed an algorithm for construction of CDAWG for a set of words given by a trie.[8]
Usually when speaking about suffix automata and related concepts, some notions from formal language theory and automata theory are used, in particular:[9]
Formally, deterministic finite automaton is determined by 5-tuple [math]\displaystyle{ \mathcal A = (\Sigma, Q, q_0, F, \delta) }[/math], where:[10]
Most commonly, deterministic finite automaton is represented as a directed graph ("diagram") such that:[10]
In terms of its diagram, the automaton recognizes the word [math]\displaystyle{ \omega=\omega_1 \omega_2 \dots \omega_m }[/math] only if there is a path from the initial vertex [math]\displaystyle{ q_0 }[/math] to some final vertex [math]\displaystyle{ q \in F }[/math] such that concatenation of characters on this path forms [math]\displaystyle{ \omega }[/math]. The set of words recognized by an automaton forms a language that is set to be recognized by the automaton. In these terms, the language recognized by a suffix automaton of [math]\displaystyle{ S }[/math] is the language of its (possibly empty) suffixes.[9]
"Right context" of the word [math]\displaystyle{ \omega }[/math] with respect to language [math]\displaystyle{ L }[/math] is a set [math]\displaystyle{ [\omega]_R=\{\alpha : \omega\alpha \in L\} }[/math] that is a set of words [math]\displaystyle{ \alpha }[/math] such that their concatenation with [math]\displaystyle{ \omega }[/math] forms a word from [math]\displaystyle{ L }[/math]. Right contexts induce a natural equivalence relation [math]\displaystyle{ [\alpha]_R = [\beta]_R }[/math] on the set of all words. If language [math]\displaystyle{ L }[/math] is recognized by some deterministic finite automaton, there exists unique up to isomorphism automaton that recognizes the same language and has the minimum possible number of states. Such an automaton is called a minimal automaton for the given language [math]\displaystyle{ L }[/math]. Myhill–Nerode theorem allows it to define it explicitly in terms of right contexts:[11][12]
Theorem — Minimal automaton recognizing language [math]\displaystyle{ L }[/math] over the alphabet [math]\displaystyle{ \Sigma }[/math] may be explicitly defined in the following way:
In these terms, a "suffix automaton" is the minimal deterministic finite automaton recognizing the language of suffixes of the word [math]\displaystyle{ S=s_1s_2\dots s_n }[/math]. The right context of the word [math]\displaystyle{ \omega }[/math] with respect to this language consists of words [math]\displaystyle{ \alpha }[/math], such that [math]\displaystyle{ \omega \alpha }[/math] is a suffix of [math]\displaystyle{ S }[/math]. It allows to formulate the following lemma defining a bijection between the right context of the word and the set of right positions of its occurrences in [math]\displaystyle{ S }[/math]:[13][14]
Theorem — Let [math]\displaystyle{ endpos(\omega)=\{r: \omega=s_l \dots s_r \} }[/math] be the set of right positions of occurrences of [math]\displaystyle{ \omega }[/math] in [math]\displaystyle{ S }[/math].
There is a following bijection between [math]\displaystyle{ endpos(\omega) }[/math] and [math]\displaystyle{ [\omega]_R }[/math]:
For example, for the word [math]\displaystyle{ S=abacaba }[/math] and its subword [math]\displaystyle{ \omega = ab }[/math], it holds [math]\displaystyle{ endpos(ab)=\{2,6\} }[/math] and [math]\displaystyle{ [ab]_R = \{a,acaba\} }[/math]. Informally, [math]\displaystyle{ [ab]_R }[/math] is formed by words that follow occurrences of [math]\displaystyle{ ab }[/math] to the end of [math]\displaystyle{ S }[/math] and [math]\displaystyle{ endpos(ab) }[/math] is formed by right positions of those occurrences. In this example, the element [math]\displaystyle{ x=2 \in endpos(ab) }[/math] corresponds with the word [math]\displaystyle{ s_3 s_4 s_5 s_6 s_7 = acaba \in [ab]_R }[/math] while the word [math]\displaystyle{ a \in [ab]_R }[/math] corresponds with the element [math]\displaystyle{ 7-|a|=6 \in endpos(ab) }[/math].
It implies several structure properties of suffix automaton states. Let [math]\displaystyle{ |\alpha| \leq |\beta| }[/math], then:[14]
Any state [math]\displaystyle{ q = [\alpha]_R }[/math] of the suffix automaton recognizes some continuous chain of nested suffixes of the longest word recognized by this state.[14]
"Left extension" [math]\displaystyle{ \overset{\scriptstyle{\leftarrow}}{\gamma} }[/math] of the string [math]\displaystyle{ \gamma }[/math] is the longest string [math]\displaystyle{ \omega }[/math] that has the same right context as [math]\displaystyle{ \gamma }[/math]. Length [math]\displaystyle{ |\overset{\scriptstyle{\leftarrow}}{\gamma}| }[/math] of the longest string recognized by [math]\displaystyle{ q=[\gamma]_R }[/math] is denoted by [math]\displaystyle{ len(q) }[/math]. It holds:[15]
Theorem — Left extension of [math]\displaystyle{ \gamma }[/math] may be represented as [math]\displaystyle{ \overleftarrow{\gamma} = \beta \gamma }[/math], where [math]\displaystyle{ \beta }[/math] is the longest word such that any occurrence of [math]\displaystyle{ \gamma }[/math] in [math]\displaystyle{ S }[/math] is preceded by [math]\displaystyle{ \beta }[/math].
"Suffix link" [math]\displaystyle{ link(q) }[/math] of the state [math]\displaystyle{ q=[\alpha]_R }[/math] is the pointer to the state [math]\displaystyle{ p }[/math] that contains the largest suffix of [math]\displaystyle{ \alpha }[/math] that is not recognized by [math]\displaystyle{ q }[/math].
In this terms it can be said [math]\displaystyle{ q=[\alpha]_R }[/math] recognizes exactly all suffixes of [math]\displaystyle{ \overset{\scriptstyle{\leftarrow}}{\alpha} }[/math] that is longer than [math]\displaystyle{ len(link(q)) }[/math] and not longer than [math]\displaystyle{ len(q) }[/math]. It also holds:[15]
Theorem — Suffix links form a tree [math]\displaystyle{ \mathcal T(V, E) }[/math] that may be defined explicitly in the following way:
A "prefix tree" (or "trie") is a rooted directed tree in which arcs are marked by characters in such a way no vertex [math]\displaystyle{ v }[/math] of such tree has two out-going arcs marked with the same character. Some vertices in trie are marked as final. Trie is said to recognize a set of words defined by paths from its root to final vertices. In this way prefix trees are a special kind of deterministic finite automata if you perceive its root as an initial vertex.[16] The "suffix trie" of the word [math]\displaystyle{ S }[/math] is a prefix tree recognizing a set of its suffixes. "A suffix tree" is a tree obtained from a suffix trie via the compaction procedure, during which consequent edges are merged if the degree of the vertex between them is equal to two.[15]
By its definition, a suffix automaton can be obtained via minimization of the suffix trie. It may be shown that a compacted suffix automaton is obtained by both minimization of the suffix tree (if one assumes each string on the edge of the suffix tree is a solid character from the alphabet) and compaction of the suffix automaton.[17] Besides this connection between the suffix tree and the suffix automaton of the same string there is as well a connection between the suffix automaton of the string [math]\displaystyle{ S=s_1 s_2 \dots s_n }[/math] and the suffix tree of the reversed string [math]\displaystyle{ S^R = s_n s_{n-1} \dots s_1 }[/math].[18]
Similarly to right contexts one may introduce "left contexts" [math]\displaystyle{ [\omega]_L = \{\beta \in \Sigma^* : \beta \omega \in L\} }[/math], "right extensions" [math]\displaystyle{ \overset{\scriptstyle{\rightarrow}}{\omega~} }[/math] corresponding to the longest string having same left context as [math]\displaystyle{ \omega }[/math] and the equivalence relation [math]\displaystyle{ [\alpha]_L = [\beta]_L }[/math]. If one considers right extensions with respect to the language [math]\displaystyle{ L }[/math] of "prefixes" of the string [math]\displaystyle{ S }[/math] it may be obtained:[15]
Theorem — Suffix tree of the string [math]\displaystyle{ S }[/math] may be defined explicitly in the following way:
Here triplet [math]\displaystyle{ (v_1, \omega, v_2) \in E }[/math] means there is an edge from [math]\displaystyle{ v_1 }[/math] to [math]\displaystyle{ v_2 }[/math] with the string [math]\displaystyle{ \omega }[/math] written on it
, which implies the suffix link tree of the string [math]\displaystyle{ S }[/math] and the suffix tree of the string [math]\displaystyle{ S^R }[/math] are isomorphic:[18]
Suffix structures of words "abbcbc" and "cbcbba" |
---|
Similarly to the case of left extensions, the following lemma holds for right extensions:[15]
Theorem — Right extension of the string [math]\displaystyle{ \gamma }[/math] may be represented as [math]\displaystyle{ \overrightarrow{\gamma} = \gamma\alpha }[/math], where [math]\displaystyle{ \alpha }[/math] is the longest word such that every occurrence of [math]\displaystyle{ \gamma }[/math] in [math]\displaystyle{ S }[/math] is succeeded by [math]\displaystyle{ \beta }[/math].
A suffix automaton of the string [math]\displaystyle{ S }[/math] of length [math]\displaystyle{ n\gt 1 }[/math] has at most [math]\displaystyle{ 2n-1 }[/math] states and at most [math]\displaystyle{ 3n-4 }[/math] transitions. These bounds are reached on strings [math]\displaystyle{ abb\dots bb=ab^{n-1} }[/math] and [math]\displaystyle{ abb\dots bc=ab^{n-2}c }[/math] correspondingly.[13] This may be formulated in a stricter way as [math]\displaystyle{ |\delta| \leq |Q| + n - 2 }[/math] where [math]\displaystyle{ |\delta| }[/math] and [math]\displaystyle{ |Q| }[/math] are the numbers of transitions and states in automaton correspondingly.[14]
Maximal suffix automata |
---|
Initially the automaton only consists of a single state corresponding to the empty word, then characters of the string are added one by one and the automaton is rebuilt on each step incrementally.[19]
After a new character is appended to the string, some equivalence classes are altered. Let [math]\displaystyle{ [\alpha]_{R_\omega} }[/math] be the right context of [math]\displaystyle{ \alpha }[/math] with respect to the language of [math]\displaystyle{ \omega }[/math] suffixes. Then the transition from [math]\displaystyle{ [\alpha]_{R_\omega} }[/math] to [math]\displaystyle{ [\alpha]_{R_{\omega x}} }[/math] after [math]\displaystyle{ x }[/math] is appended to [math]\displaystyle{ \omega }[/math] is defined by lemma:[14]
Theorem — Let [math]\displaystyle{ \alpha, \omega \in \Sigma^* }[/math] be some words over [math]\displaystyle{ \Sigma }[/math] and [math]\displaystyle{ x \in \Sigma }[/math] be some character from this alphabet. Then there is a following correspondence between [math]\displaystyle{ [\alpha]_{R_\omega} }[/math] and [math]\displaystyle{ [\alpha]_{R_{\omega x} } }[/math]:
After adding [math]\displaystyle{ x }[/math] to the current word [math]\displaystyle{ \omega }[/math] the right context of [math]\displaystyle{ \alpha }[/math] may change significantly only if [math]\displaystyle{ \alpha }[/math] is a suffix of [math]\displaystyle{ \omega x }[/math]. It implies equivalence relation [math]\displaystyle{ \equiv_{R_{\omega x} } }[/math] is a refinement of [math]\displaystyle{ \equiv_{R_\omega} }[/math]. In other words, if [math]\displaystyle{ [\alpha]_{R_{\omega x} } = [\beta]_{R_{\omega x} } }[/math], then [math]\displaystyle{ [\alpha]_{R_{\omega} } = [\beta]_{R_{\omega} } }[/math]. After the addition of a new character at most two equivalence classes of [math]\displaystyle{ \equiv_{R_\omega} }[/math] will be split and each of them may split in at most two new classes. First, equivalence class corresponding to empty right context is always split into two equivalence classes, one of them corresponding to [math]\displaystyle{ \omega x }[/math] itself and having [math]\displaystyle{ \{\varepsilon\} }[/math] as a right context. This new equivalence class contains exactly [math]\displaystyle{ \omega x }[/math] and all its suffixes that did not occur in [math]\displaystyle{ \omega }[/math], as the right context of such words was empty before and contains only empty word now.[14]
Given the correspondence between states of the suffix automaton and vertices of the suffix tree, it is possible to find out the second state that may possibly split after a new character is appended. The transition from [math]\displaystyle{ \omega }[/math] to [math]\displaystyle{ \omega x }[/math] corresponds to the transition from [math]\displaystyle{ \omega^R }[/math] to [math]\displaystyle{ x \omega^R }[/math] in the reversed string. In terms of suffix trees it corresponds to the insertion of the new longest suffix [math]\displaystyle{ x\omega^R }[/math] into the suffix tree of [math]\displaystyle{ \omega^R }[/math]. At most two new vertices may be formed after this insertion: one of them corresponding to [math]\displaystyle{ x \omega^R }[/math], while the other one corresponds to its direct ancestor if there was a branching. Returning to suffix automata, it means the first new state recognizes [math]\displaystyle{ \omega x }[/math] and the second one (if there is a second new state) is its suffix link. It may be stated as a lemma:[14]
Theorem — Let [math]\displaystyle{ \omega \in \Sigma^* }[/math], [math]\displaystyle{ x \in \Sigma }[/math] be some word and character over [math]\displaystyle{ \Sigma }[/math]. Also let [math]\displaystyle{ \alpha }[/math] be the longest suffix of [math]\displaystyle{ \omega x }[/math], which occurs in [math]\displaystyle{ \omega }[/math], and let [math]\displaystyle{ \beta=\overset{\scriptstyle{\leftarrow} }{\alpha} }[/math]. Then for any substrings [math]\displaystyle{ u,v }[/math] of [math]\displaystyle{ \omega }[/math] it holds:
It implies that if [math]\displaystyle{ \alpha=\beta }[/math] (for example, when [math]\displaystyle{ x }[/math] didn't occur in [math]\displaystyle{ \omega }[/math] at all and [math]\displaystyle{ \alpha=\beta=\varepsilon }[/math]), then only the equivalence class corresponding to the empty right context is split.[14]
Besides suffix links it is also needed to define final states of the automaton. It follows from structure properties that all suffixes of a word [math]\displaystyle{ \alpha }[/math] recognized by [math]\displaystyle{ q=[\alpha]_R }[/math] are recognized by some vertex on suffix path [math]\displaystyle{ (q, link(q), link^2(q), \dots) }[/math] of [math]\displaystyle{ q }[/math]. Namely, suffixes with length greater than [math]\displaystyle{ len(link(q)) }[/math] lie in [math]\displaystyle{ q }[/math], suffixes with length greater than [math]\displaystyle{ len(link(link(q)) }[/math] but not greater than [math]\displaystyle{ len(link(q)) }[/math] lie in [math]\displaystyle{ link(q) }[/math] and so on. Thus if the state recognizing [math]\displaystyle{ \omega }[/math] is denoted by [math]\displaystyle{ last }[/math], then all final states (that is, recognizing suffixes of [math]\displaystyle{ \omega }[/math]) form up the sequence [math]\displaystyle{ (last, link(last), link^2(last), \dots) }[/math].[19]
After the character [math]\displaystyle{ x }[/math] is appended to [math]\displaystyle{ \omega }[/math] possible new states of suffix automaton are [math]\displaystyle{ [\omega x]_{R_{\omega x} } }[/math] and [math]\displaystyle{ [\alpha]_{R_{\omega x} } }[/math]. Suffix link from [math]\displaystyle{ [\omega x]_{R_{\omega x} } }[/math] goes to [math]\displaystyle{ [\alpha]_{R_{\omega x} } }[/math] and from [math]\displaystyle{ [\alpha]_{R_{\omega x} } }[/math] it goes to [math]\displaystyle{ link([\alpha]_{R_{\omega} }) }[/math]. Words from [math]\displaystyle{ [\omega x]_{R_{\omega x} } }[/math] occur in [math]\displaystyle{ \omega x }[/math] only as its suffixes therefore there should be no transitions at all from [math]\displaystyle{ [\omega x]_{R_{\omega x}} }[/math] while transitions to it should go from suffixes of [math]\displaystyle{ \omega }[/math] having length at least [math]\displaystyle{ \alpha }[/math] and be marked with the character [math]\displaystyle{ x }[/math]. State [math]\displaystyle{ [\alpha]_{R_{\omega x}} }[/math] is formed by subset of [math]\displaystyle{ [\alpha]_{R_\omega} }[/math], thus transitions from [math]\displaystyle{ [\alpha]_{R_{\omega x}} }[/math] should be same as from [math]\displaystyle{ [\alpha]_{R_\omega} }[/math]. Meanwhile, transitions leading to [math]\displaystyle{ [\alpha]_{R_{\omega x}} }[/math] should go from suffixes of [math]\displaystyle{ \omega }[/math] having length less than [math]\displaystyle{ |\alpha| }[/math] and at least [math]\displaystyle{ len(link([\alpha]_{R_{\omega} })) }[/math], as such transitions have led to [math]\displaystyle{ [\alpha]_{R_\omega} }[/math] before and corresponded to seceded part of this state. States corresponding to these suffixes may be determined via traversal of suffix link path for [math]\displaystyle{ [\omega]_{R_\omega} }[/math].[19]
Theoretical results above lead to the following algorithm that takes character [math]\displaystyle{ x }[/math] and rebuilds the suffix automaton of [math]\displaystyle{ \omega }[/math] into the suffix automaton of [math]\displaystyle{ \omega x }[/math]:[19]
The whole procedure is described by the following pseudo-code:[19]
function add_letter(x): define p = last assign last = new_state() assign len(last) = len(p) + 1 while δ(p, x) is undefined: assign δ(p, x) = last, p = link(p) define q = δ(p, x) if q = last: assign link(last) = q0 else if len(q) = len(p) + 1: assign link(last) = q else: define cl = new_state() assign len(cl) = len(p) + 1 assign δ(cl) = δ(q), link(cl) = link(q) assign link(last) = link(q) = cl while δ(p, x) = q: assign δ(p, x) = cl, p = link(p)
Here [math]\displaystyle{ q_0 }[/math] is the initial state of the automaton and [math]\displaystyle{ new\_state() }[/math] is a function creating new state for it. It is assumed [math]\displaystyle{ last }[/math], [math]\displaystyle{ len }[/math], [math]\displaystyle{ link }[/math] and [math]\displaystyle{ \delta }[/math] are stored as global variables.[19]
Complexity of the algorithm may vary depending on the underlying structure used to store transitions of the automaton. It may be implemented in [math]\displaystyle{ O(n \log |\Sigma|) }[/math] with [math]\displaystyle{ O(n) }[/math] memory overhead or in [math]\displaystyle{ O(n) }[/math] with [math]\displaystyle{ O(n |\Sigma|) }[/math] memory overhead if one assumes that memory allocation is done in [math]\displaystyle{ O(1) }[/math]. To obtain such complexity, one has to use the methods of amortized analysis. The value of [math]\displaystyle{ len(p) }[/math] strictly reduces with each iteration of the cycle while it may only increase by as much as one after the first iteration of the cycle on the next add_letter call. Overall value of [math]\displaystyle{ len(p) }[/math] never exceeds [math]\displaystyle{ n }[/math] and it is only increased by one between iterations of appending new letters that suggest total complexity is at most linear as well. The linearity of the second cycle is shown in a similar way.[19]
The suffix automaton is closely related to other suffix structures and substring indices. Given a suffix automaton of a specific string one may construct its suffix tree via compacting and recursive traversal in linear time.[20] Similar transforms are possible in both directions to switch between the suffix automaton of [math]\displaystyle{ S }[/math] and the suffix tree of reversed string [math]\displaystyle{ S^R }[/math].[18] Other than this several generalizations were developed to construct an automaton for the set of strings given by trie,[8] compacted suffix automation (CDAWG),[7] to maintain the structure of the automaton on the sliding window,[21] and to construct it in a bidirectional way, supporting the insertion of a characters to both the beginning and the end of the string.[22]
As was already mentioned above, a compacted suffix automaton is obtained via both compaction of a regular suffix automaton (by removing states which are non-final and have exactly one out-going arc) and the minimization of a suffix tree. Similarly to the regular suffix automaton, states of compacted suffix automaton may be defined in explicit manner. A two-way extension [math]\displaystyle{ \overset{\scriptstyle{\longleftrightarrow}}{\gamma} }[/math] of a word [math]\displaystyle{ \gamma }[/math] is the longest word [math]\displaystyle{ \omega = \beta \gamma \alpha }[/math], such that every occurrence of [math]\displaystyle{ \gamma }[/math] in [math]\displaystyle{ S }[/math] is preceded by [math]\displaystyle{ \beta }[/math] and succeeded by [math]\displaystyle{ \alpha }[/math]. In terms of left and right extensions it means that two-way extension is the left extension of the right extension or, which is equivalent, the right extension of the left extension, that is [math]\displaystyle{ \overset{\scriptstyle\longleftrightarrow}{\gamma} = \overset{\scriptstyle\leftarrow}{\overset{\rightarrow}{\gamma}} = \overset{\rightarrow}{\overset{\scriptstyle\leftarrow}{\gamma}} }[/math]. In terms of two-way extensions compacted automaton is defined as follows:[15]
Theorem — Compacted suffix automaton of the word [math]\displaystyle{ S }[/math] is defined by a pair [math]\displaystyle{ (V, E) }[/math], where:
Two-way extensions induce an equivalence relation [math]\displaystyle{ \overset{\scriptstyle\longleftrightarrow}{\alpha} = \overset{\scriptstyle\longleftrightarrow}{\beta} }[/math] which defines the set of words recognized by the same state of compacted automaton. This equivalence relation is a transitive closure of the relation defined by [math]\displaystyle{ (\overset{\scriptstyle{\rightarrow}}{\alpha\,}=\overset{\scriptstyle{\rightarrow}}{\beta\,}) \vee (\overset{\scriptstyle{\leftarrow}}{\alpha} = \overset{\scriptstyle{\leftarrow}}{\beta}) }[/math], which highlights the fact that a compacted automaton may be obtained by both gluing suffix tree vertices equivalent via [math]\displaystyle{ \overset{\scriptstyle{\leftarrow}}{\alpha} = \overset{\scriptstyle{\leftarrow}}{\beta} }[/math] relation (minimization of the suffix tree) and gluing suffix automaton states equivalent via [math]\displaystyle{ \overset{\scriptstyle{\rightarrow}}{\alpha\,}=\overset{\scriptstyle{\rightarrow}}{\beta\,} }[/math] relation (compaction of suffix automaton).[23] If words [math]\displaystyle{ \alpha }[/math] and [math]\displaystyle{ \beta }[/math] have same right extensions, and words [math]\displaystyle{ \beta }[/math] and [math]\displaystyle{ \gamma }[/math] have same left extensions, then cumulatively all strings [math]\displaystyle{ \alpha }[/math], [math]\displaystyle{ \beta }[/math] and [math]\displaystyle{ \gamma }[/math] have same two-way extensions. At the same time it may happen that neither left nor right extensions of [math]\displaystyle{ \alpha }[/math] and [math]\displaystyle{ \gamma }[/math] coincide. As an example one may take [math]\displaystyle{ S=\beta=ab }[/math], [math]\displaystyle{ \alpha=a }[/math] and [math]\displaystyle{ \gamma=b }[/math], for which left and right extensions are as follows: [math]\displaystyle{ \overset{\scriptstyle{\rightarrow}}{\alpha\,}=\overset{\scriptstyle{\rightarrow}}{\beta\,}=ab=\overset{\scriptstyle{\leftarrow}}{\beta} = \overset{\scriptstyle{\leftarrow}}{\gamma} }[/math], but [math]\displaystyle{ \overset{\scriptstyle{\rightarrow}}{\gamma\,}=b }[/math] and [math]\displaystyle{ \overset{\scriptstyle{\leftarrow}}{\alpha}=a }[/math]. That being said, while equivalence relations of one-way extensions were formed by some continuous chain of nested prefixes or suffixes, bidirectional extensions equivalence relations are more complex and the only thing one may conclude for sure is that strings with the same two-way extension are substrings of the longest string having the same two-way extension, but it may even happen that they don't have any non-empty substring in common. The total number of equivalence classes for this relation does not exceed [math]\displaystyle{ n+1 }[/math] which implies that compacted suffix automaton of the string having length [math]\displaystyle{ n }[/math] has at most [math]\displaystyle{ n+1 }[/math] states. The amount of transitions in such automaton is at most [math]\displaystyle{ 2n-2 }[/math].[15]
Consider a set of words [math]\displaystyle{ T=\{S_1, S_2, \dots, S_k\} }[/math]. It is possible to construct a generalization of suffix automaton that would recognize the language formed up by suffixes of all words from the set. Constraints for the number of states and transitions in such automaton would stay the same as for a single-word automaton if you put [math]\displaystyle{ n = |S_1| + |S_2| + \dots + |S_k| }[/math].[23] The algorithm is similar to the construction of single-word automaton except instead of [math]\displaystyle{ last }[/math] state, function add_letter would work with the state corresponding to the word [math]\displaystyle{ \omega_i }[/math] assuming the transition from the set of words [math]\displaystyle{ \{\omega_1, \dots, \omega_i, \dots, \omega_k\} }[/math] to the set [math]\displaystyle{ \{\omega_1, \dots, \omega_i x, \dots, \omega_k\} }[/math].[24][25]
This idea is further generalized to the case when [math]\displaystyle{ T }[/math] is not given explicitly but instead is given by a prefix tree with [math]\displaystyle{ Q }[/math] vertices. Mohri et al. showed such an automaton would have at most [math]\displaystyle{ 2Q-2 }[/math] and may be constructed in linear time from its size. At the same time, the number of transitions in such automaton may reach [math]\displaystyle{ O(Q|\Sigma|) }[/math], for example for the set of words [math]\displaystyle{ T=\{\sigma_1, a\sigma_1, a^2\sigma_1, \dots, a^n \sigma_1, a^n \sigma_2, \dots, a^n \sigma_k \} }[/math] over the alphabet [math]\displaystyle{ \Sigma=\{a,\sigma_1,\dots,\sigma_k\} }[/math] the total length of words is equal to [math]\displaystyle{ O(n^2+nk) }[/math], the number of vertices in corresponding suffix trie is equal to [math]\displaystyle{ O(n+k) }[/math] and corresponding suffix automaton is formed of [math]\displaystyle{ O(n+k) }[/math] states and [math]\displaystyle{ O(nk) }[/math] transitions. Algorithm suggested by Mohri mainly repeats the generic algorithm for building automaton of several strings but instead of growing words one by one, it traverses the trie in a breadth-first search order and append new characters as it meet them in the traversal, which guarantees amortized linear complexity.[26]
Some compression algorithms, such as LZ77 and RLE may benefit from storing suffix automaton or similar structure not for the whole string but for only last [math]\displaystyle{ k }[/math] its characters while the string is updated. This is because compressing data is usually expressively large and using [math]\displaystyle{ O(n) }[/math] memory is undesirable. In 1985, Janet Blumer developed an algorithm to maintain a suffix automaton on a sliding window of size [math]\displaystyle{ k }[/math] in [math]\displaystyle{ O(nk) }[/math] worst-case and [math]\displaystyle{ O(n \log k) }[/math] on average, assuming characters are distributed independently and uniformly. She also showed [math]\displaystyle{ O(nk) }[/math] complexity cannot be improved: if one considers words construed as a concatenation of several [math]\displaystyle{ (ab)^m c (ab)^m d }[/math] words, where [math]\displaystyle{ k=6m+2 }[/math], then the number of states for the window of size [math]\displaystyle{ k }[/math] would frequently change with jumps of order [math]\displaystyle{ m }[/math], which renders even theoretical improvement of [math]\displaystyle{ O(nk) }[/math] for regular suffix automata impossible.[27]
The same should be true for the suffix tree because its vertices correspond to states of the suffix automaton of the reversed string but this problem may be resolved by not explicitly storing every vertex corresponding to the suffix of the whole string, thus only storing vertices with at least two out-going edges. A variation of McCreight's suffix tree construction algorithm for this task was suggested in 1989 by Edward Fiala and Daniel Greene;[28] several years later a similar result was obtained with the variation of Ukkonen's algorithm by Jesper Larsson.[29][30] The existence of such an algorithm, for compacted suffix automaton that absorbs some properties of both suffix trees and suffix automata, was an open question for a long time until it was discovered by Martin Senft and Tomasz Dvorak in 2008, that it is impossible if the alphabet's size is at least two.[31]
One way to overcome this obstacle is to allow window width to vary a bit while staying [math]\displaystyle{ O(k) }[/math]. It may be achieved by an approximate algorithm suggested by Inenaga et al. in 2004. The window for which suffix automaton is built in this algorithm is not guaranteed to be of length [math]\displaystyle{ k }[/math] but it is guaranteed to be at least [math]\displaystyle{ k }[/math] and at most [math]\displaystyle{ 2k+1 }[/math] while providing linear overall complexity of the algorithm.[32]
Suffix automaton of the string [math]\displaystyle{ S }[/math] may be used to solve such problems as:[33][34]
It is assumed here that [math]\displaystyle{ T }[/math] is given on the input after suffix automaton of [math]\displaystyle{ S }[/math] is constructed.[33]
Suffix automata are also used in data compression,[35] music retrieval[36][37] and matching on genome sequences.[38]
Original source: https://en.wikipedia.org/wiki/Suffix automaton.
Read more |