Suffix automaton

{{short description|Deterministic finite automaton accepting set of all suffixes of particular string}}

{{Use shortened footnotes|date=December 2020}}

{{Infobox data structure

|name = Suffix automaton

|image = File:Suffix automaton bold.svg

|type = Substring index

|invented_by = Anselm Blumer; Janet Blumer; Andrzej Ehrenfeucht; David Haussler; Ross McConnell

|invented_year = 1983

|space_avg = O(n)

|space_worst = O(n)

}}

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 S 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 S=s_1 s_2 \dots s_n. 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 S having length at least two characters has at most 2|S| - 1 states and at most 3|S| - 4 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.

History

File:Anselm Blumer with DAWG.jpg

The concept of suffix automaton was introduced in 1983 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, Vaughan Pratt{{harvp|Pratt|1973}} and Anatol Slissenko.{{harvp|Slisenko|1983}} In their initial work, Blumer et al. showed a suffix automaton built for the string S of length greater than 1 has at most 2|S| -1 states and at most 3|S| -4 transitions, and suggested a linear algorithm for automaton construction.{{harvp|Blumer|Blumer|Ehrenfeucht|Haussler|1984|loc=|p=109}}

In 1983, Mu-Tian Chen and Joel Seiferas independently showed that Weiner's 1973 suffix-tree construction algorithm{{harvp|Weiner|1973}} while building a suffix tree of the string S constructs a suffix automaton of the reversed string S^R as an auxiliary structure.{{harvp|Chen|Seiferas|1985|loc=|p=97}} 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).{{harvp|Blumer|Blumer|Haussler|McConnell|1987|loc=|p=578}} In 1997, Maxime Crochemore and Renaud Vérin developed a linear algorithm for direct CDAWG construction.{{harvp|Crochemore|Vérin|1997|loc=|p=192}} In 2001, Shunsuke Inenaga et al. developed an algorithm for construction of CDAWG for a set of words given by a trie.{{harvp|Inenaga|Hoshino|Shinohara|Takeda|2001|loc=|p=1}}

Definitions

Usually when speaking about suffix automata and related concepts, some notions from formal language theory and automata theory are used, in particular:

  • "Alphabet" is a finite set \Sigma that is used to construct words. Its elements are called "characters";
  • "Word" is a finite sequence of characters \omega = \omega_1 \omega_2 \dots \omega_n. "Length" of the word \omega is denoted as |\omega|=n;
  • "Formal language" is a set of words over given alphabet;
  • "Language of all words" is denoted as \Sigma^*(where the "*" character stands for Kleene star), "empty word" (the word of zero length) is denoted by the character \varepsilon;
  • "Concatenation of words" \alpha = \alpha_1 \alpha_2 \dots \alpha_n and \beta = \beta_1 \beta_2 \dots \beta_m is denoted as \alpha \cdot \beta or \alpha\beta and corresponds to the word obtained by writing \beta to the right of \alpha, that is, \alpha \beta = \alpha_1 \alpha_2 \dots \alpha_n \beta_1 \beta_2 \dots \beta_m;
  • "Concatenation of languages" A and B is denoted as A \cdot B or AB and corresponds to the set of pairwise concatenations AB = \{\alpha \beta : \alpha \in A, \beta \in B\};
  • If the word \omega\in\Sigma^* may be represented as \omega = \alpha\gamma\beta, where \alpha,\beta,\gamma \in \Sigma^*, then words \alpha, \beta and \gamma are called "prefix", "suffix" and "subword" (substring) of the word \omega correspondingly;
  • If T = T_1 \dots T_n and T_l T_{l+1} \dots T_r = S (with 1 \leq l \leq r \leq n) then S is said to "occur" in T as a subword. Here l and r are called left and right positions of occurrence of S in T correspondingly.

Automaton structure

Formally, deterministic finite automaton is determined by 5-tuple \mathcal A = (\Sigma, Q, q_0, F, \delta), where:

  • \Sigma is an "alphabet" that is used to construct words,
  • Q is a set of automaton "states",
  • q_0 \in Q is an "initial" state of automaton,
  • F \subset Q is a set of "final" states of automaton,
  • \delta : Q \times \Sigma \mapsto Q is a partial "transition" function of automaton, such that \delta(q, \sigma) for q \in Q and \sigma \in \Sigma is either undefined or defines a transition from q over character \sigma.

Most commonly, deterministic finite automaton is represented as a directed graph ("diagram") such that:{{harvp|Serebryakov|Galochkin|Furugian|Gonchar|2006|loc=|pp=50—54}}

  • Set of graph vertices corresponds to the state of states Q,
  • Graph has a specific marked vertex corresponding to initial state q_0,
  • Graph has several marked vertices corresponding to the set of final states F,
  • Set of graph arcs corresponds to the set of transitions \delta,
  • Specifically, every transition \delta(q_1, \sigma) = q_2 is represented by an arc from q_1 to q_2 marked with the character \sigma. This transition also may be denoted as q_1 \begin{smallmatrix}{\sigma}\\[-5pt]{\longrightarrow}\end{smallmatrix} q_2.

In terms of its diagram, the automaton recognizes the word \omega=\omega_1 \omega_2 \dots \omega_m only if there is a path from the initial vertex q_0 to some final vertex q \in F such that concatenation of characters on this path forms \omega. 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 S is the language of its (possibly empty) suffixes.{{harvp|Crochemore|Hancart|1997|pp=3—6|loc=}}

= Automaton states =

{{Main|DFA minimization}}

"Right context" of the word \omega with respect to language L is a set [\omega]_R=\{\alpha : \omega\alpha \in L\} that is a set of words \alpha such that their concatenation with \omega forms a word from L. Right contexts induce a natural equivalence relation [\alpha]_R = [\beta]_R on the set of all words. If language L 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 L. Myhill–Nerode theorem allows it to define it explicitly in terms of right contexts:{{harvp|Рубцов|2019|loc=|pp=89—94}}{{harvp|Hopcroft|Ullman|1979|loc=|pp=65—68}}

{{Math theorem

|math_statement = Minimal automaton recognizing language L over the alphabet \Sigma may be explicitly defined in the following way:

  • Alphabet \Sigma stays the same,
  • States Q correspond to right contexts [\omega]_R of all possible words \omega \in \Sigma^*,
  • Initial state q_0 corresponds to the right context of the empty word [\varepsilon]_R,
  • Final states F correspond to right contexts [\omega]_R of words from \omega \in L,
  • Transitions \delta are given by [\omega]_R \begin{smallmatrix}{\sigma}\\[-5pt]{\longrightarrow}\end{smallmatrix} [\omega\sigma]_R, where \omega \in \Sigma^* and \sigma \in \Sigma.

}}

In these terms, a "suffix automaton" is the minimal deterministic finite automaton recognizing the language of suffixes of the word S=s_1s_2\dots s_n. The right context of the word \omega with respect to this language consists of words \alpha, such that \omega \alpha is a suffix of S. 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 S:{{harvp|Blumer|Blumer|Ehrenfeucht|Haussler|1984|loc=|pp=111—114}}{{harvp|Crochemore|Hancart|1997|loc=|pp=27—31}}

{{Math theorem

|math_statement = Let endpos(\omega)=\{r: \omega=s_l \dots s_r \} be the set of right positions of occurrences of \omega in S.

There is a following bijection between endpos(\omega) and [\omega]_R:

  • If x \in endpos(\omega), then s_{x+1}s_{x+2}\dots s_n \in [\omega]_R;
  • If \alpha \in [\omega]_R, then n-\vert\alpha\vert \in endpos(\omega).

}}

For example, for the word S=abacaba and its subword \omega = ab, it holds endpos(ab)=\{2,6\} and [ab]_R = \{a,acaba\}. Informally, [ab]_R is formed by words that follow occurrences of ab to the end of S and endpos(ab) is formed by right positions of those occurrences. In this example, the element x=2 \in endpos(ab) corresponds with the word s_3 s_4 s_5 s_6 s_7 = acaba \in [ab]_R while the word a \in [ab]_R corresponds with the element 7-|a|=6 \in endpos(ab).

It implies several structure properties of suffix automaton states. Let |\alpha|\leq |\beta|, then:

  • If [\alpha]_R and [\beta]_R have at least one common element x, then endpos(\alpha) and endpos(\beta) have a common element as well. It implies \alpha is a suffix of \beta and therefore endpos(\beta) \subset endpos(\alpha) and [\beta]_R \subset [\alpha]_R. In aforementioned example, a \in [ab]_R \cap [cab]_R, so ab is a suffix of cab and thus [cab]_R=\{a\} \subset \{a,acaba\} = [ab]_R and endpos(cab) = \{6\} \subset \{2,6\} = endpos(ab);
  • If [\alpha]_R=[\beta]_R, then endpos(\alpha)=endpos(\beta), thus \alpha occurs in S only as a suffix of \beta. For example, for \alpha=b and \beta = ab it holds that [b]_R = [ab]_R = \{a,acaba\} and endpos(b)=endpos(ab)=\{2,6\};
  • If [\alpha]_R=[\beta]_R and \gamma is a suffix of \beta such that |\alpha|\leq |\gamma|\leq |\beta|, then [\alpha]_R = [\gamma]_R = [\beta]_R. In the example above [c]_R = [bac]_R = \{aba\} and it holds for "intermediate" suffix \gamma = ac that [ac]_R = \{aba\}.

Any state q = [\alpha]_R of the suffix automaton recognizes some continuous chain of nested suffixes of the longest word recognized by this state.

"Left extension" \overset{\scriptstyle{\leftarrow}}{\gamma} of the string \gamma is the longest string \omega that has the same right context as \gamma. Length |\overset{\scriptstyle{\leftarrow}}{\gamma}| of the longest string recognized by q=[\gamma]_R is denoted by len(q). It holds:{{harvp|Inenaga|Hoshino|Shinohara|Takeda|2005|loc=|pp=159—162}}

{{Math theorem

|math_statement = Left extension of \gamma may be represented as \overleftarrow{\gamma} = \beta \gamma, where \beta is the longest word such that any occurrence of \gamma in S is preceded by \beta.

}}

"Suffix link" link(q) of the state q=[\alpha]_R is the pointer to the state p that contains the largest suffix of \alpha that is not recognized by q.

In this terms it can be said q=[\alpha]_R recognizes exactly all suffixes of \overset{\scriptstyle{\leftarrow}}{\alpha} that is longer than len(link(q)) and not longer than len(q). It also holds:

{{Math theorem

|math_statement = Suffix links form a tree \mathcal T(V, E) that may be defined explicitly in the following way:

  1. Vertices V of the tree correspond to left extensions \overleftarrow{\omega} of all S substrings,
  2. Edges E of the tree connect pairs of vertices (\overleftarrow{\omega},\overleftarrow{\alpha\omega}), such that \alpha \in \Sigma and \overleftarrow{\omega} \neq \overleftarrow{\alpha\omega}.

}}

= Connection with suffix trees =

File:Suffix structures diamond.svg

{{Main|Suffix tree}}

A "prefix tree" (or "trie") is a rooted directed tree in which arcs are marked by characters in such a way no vertex v 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.{{harvp|Rubinchik|Shur|2018|pp=1—2}} The "suffix trie" of the word S 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.

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.{{harvp|Inenaga|Hoshino|Shinohara|Takeda|2005|с=|loc=|pp=156—158}} 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 S=s_1 s_2 \dots s_n and the suffix tree of the reversed string S^R = s_n s_{n-1} \dots s_1.{{harvp|Fujishige|Tsujimaru|Inenaga|Bannai|2016|loc=|pp=1—3}}

Similarly to right contexts one may introduce "left contexts" [\omega]_L = \{\beta \in \Sigma^* : \beta \omega \in L\}, "right extensions" \overset{\scriptstyle{\rightarrow}}{\omega~} corresponding to the longest string having same left context as \omega and the equivalence relation [\alpha]_L = [\beta]_L. If one considers right extensions with respect to the language L of "prefixes" of the string S it may be obtained:

{{Math theorem

|math_statement = Suffix tree of the string S may be defined explicitly in the following way:

  • Vertices V of the tree correspond to right extensions \overrightarrow{\omega} of all S substrings,
  • Edges E correspond to triplets (\overrightarrow{\omega},x\alpha, \overrightarrow{\omega x}) such that x \in \Sigma and \overrightarrow{\omega x}=\overrightarrow{\omega} x \alpha.

Here triplet (v_1, \omega, v_2) \in E means there is an edge from v_1 to v_2 with the string \omega written on it

}}, which implies the suffix link tree of the string S and the suffix tree of the string S^R are isomorphic:

class="wikitable mw-collapsible mw-collapsed" style="margin-left: auto; margin-right: auto;"
Suffix structures of words "abbcbc" and "cbcbba" 

File:Suffix automaton for abbcbc.svg|Suffix automaton of the word "abbcbc"

File:Suffix structures.svg|Suffix trie, suffix tree and CDAWG of the word "abbcbc"

File:Suffix tree for cbcbba.svg|Suffix tree of the word "cbcbba"
(Suffix link tree of the word "abbcbc")

Similarly to the case of left extensions, the following lemma holds for right extensions:

{{Math theorem

|math_statement = Right extension of the string \gamma may be represented as \overrightarrow{\gamma} = \gamma\alpha, where \alpha is the longest word such that every occurrence of \gamma in S is succeeded by \alpha.

}}

= Size =

A suffix automaton of the string S of length n>1 has at most 2n-1 states and at most 3n-4 transitions. These bounds are reached on strings abb\dots bb=ab^{n-1} and abb\dots bc=ab^{n-2}c correspondingly. This may be formulated in a stricter way as |\delta|\leq |Q|+ n - 2 where |\delta| and |Q| are the numbers of transitions and states in automaton correspondingly.

class="wikitable mw-collapsible mw-collapsed" style="margin-left: auto; margin-right: auto; border: none;"
Maximal suffix automata

File:DAWG for abb...b.svg|Suffix automaton of ab^{n-1}

File:DAWG for abb...bc.svg|Suffix automaton of ab^{n-2}c

Construction

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.{{harvp|Crochemore|Hancart|1997|loc=|pp=31—36}}

= State updates =

After a new character is appended to the string, some equivalence classes are altered. Let [\alpha]_{R_\omega} be the right context of \alpha with respect to the language of \omega suffixes. Then the transition from [\alpha]_{R_\omega} to [\alpha]_{R_{\omega x}} after x is appended to \omega is defined by lemma:

{{Math theorem

|math_statement = Let \alpha, \omega \in \Sigma^* be some words over \Sigma and x \in \Sigma be some character from this alphabet. Then there is a following correspondence between [\alpha]_{R_\omega} and [\alpha]_{R_{\omega x} }:

  • [\alpha]_{R_{\omega x} } = [\alpha]_{R_\omega}x \cup \{\varepsilon\} if \alpha is a suffix of \omega x;
  • [\alpha]_{R_{\omega x} } = [\alpha]_{R_\omega}x otherwise.

}}

After adding x to the current word \omega the right context of \alpha may change significantly only if \alpha is a suffix of \omega x. It implies equivalence relation \equiv_{R_{\omega x} } is a refinement of \equiv_{R_\omega}. In other words, if [\alpha]_{R_{\omega x} } = [\beta]_{R_{\omega x} }, then [\alpha]_{R_{\omega} } = [\beta]_{R_{\omega} }. After the addition of a new character at most two equivalence classes of \equiv_{R_\omega} 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 \omega x itself and having \{\varepsilon\} as a right context. This new equivalence class contains exactly \omega x and all its suffixes that did not occur in \omega, as the right context of such words was empty before and contains only empty word now.

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 \omega to \omega x corresponds to the transition from \omega^R to x \omega^R in the reversed string. In terms of suffix trees it corresponds to the insertion of the new longest suffix x\omega^R into the suffix tree of \omega^R. At most two new vertices may be formed after this insertion: one of them corresponding to x \omega^R, 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 \omega x and the second one (if there is a second new state) is its suffix link. It may be stated as a lemma:

{{Math theorem

|math_statement = Let \omega \in \Sigma^*, x \in \Sigma be some word and character over \Sigma. Also let \alpha be the longest suffix of \omega x, which occurs in \omega, and let \beta=\overset{\scriptstyle{\leftarrow} }{\alpha}. Then for any substrings u,v of \omega it holds:

  • If [u]_{R_\omega} = [v]_{R_\omega} and [u]_{R_\omega} \neq [\alpha]_{R_\omega}, then [u]_{R_{\omega x} } = [v]_{R_{\omega x} };
  • If [u]_{R_\omega} = [\alpha]_{R_\omega} and \vert u\vert \leq \vert \alpha\vert , then [u]_{R_{\omega x} } = [\alpha]_{R_{\omega x} };
  • If [u]_{R_\omega} = [\alpha]_{R_\omega} and \vert u\vert > \vert \alpha\vert , then [u]_{R_{\omega x} } = [\beta]_{R_{\omega x} }.

}}

It implies that if \alpha=\beta (for example, when x didn't occur in \omega at all and \alpha=\beta=\varepsilon), then only the equivalence class corresponding to the empty right context is split.

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 \alpha recognized by q=[\alpha]_R are recognized by some vertex on suffix path (q, link(q), link^2(q), \dots) of q. Namely, suffixes with length greater than len(link(q)) lie in q, suffixes with length greater than len(link(link(q)) but not greater than len(link(q)) lie in link(q) and so on. Thus if the state recognizing \omega is denoted by last, then all final states (that is, recognizing suffixes of \omega) form up the sequence (last, link(last), link^2(last), \dots).

= Construction algorithm =

Theoretical results above lead to the following algorithm that takes character {{mvar|x}} and rebuilds the suffix automaton of {{mvar|ω}} into the suffix automaton of \omega x:

  1. The state corresponding to the word {{mvar|ω}} is kept as {{mvar|last}};
  2. After {{mvar|x}} is appended, previous value of {{mvar|last}} is stored in the variable {{mvar|p}} and {{mvar|last}} itself is reassigned to the new state corresponding to \omega x;
  3. States corresponding to suffixes of {{mvar|ω}} are updated with transitions to {{mvar|last}}. To do this one should go through p, link(p), link^2(p),\dots, until there is a state that already has a transition by {{mvar|x}};
  4. Once the aforementioned loop is over, there are 3 cases:
  5. If none of states on the suffix path had a transition by {{mvar|x}}, then {{mvar|x}} never occurred in {{mvar|ω}} before and the suffix link from {{mvar|last}} should lead to q_0;
  6. If the transition by {{mvar|x}} is found and leads from the state {{mvar|p}} to the state {{mvar|q}}, such that len(p)+1=len(q), then {{mvar|q}} does not have to be split and it is a suffix link of {{mvar|last}};
  7. If the transition is found but len(q) > len(p)+1, then words from {{mvar|q}} having length at most len(p)+1 should be segregated into new "clone" state {{mvar|cl}};
  8. If the previous step was concluded with the creation of {{mvar|cl}}, transitions from it and its suffix link should copy those of {{mvar|q}}, at the same time {{mvar|cl}} is assigned to be common suffix link of both {{mvar|q}} and {{mvar|last}};
  9. Transitions that have led to {{mvar|q}} before but corresponded to words of the length at most len(p)+1 are redirected to {{mvar|cl}}. To do this, one continues going through the suffix path of {{mvar|p}} until the state is found such that transition by {{mvar|x}} from it doesn't lead to {{mvar|q}}.

The whole procedure is described by the following pseudo-code:

function {{nowrap|add_letter(x)}}:

define {{nowrap|p {{=}} last}}

assign {{nowrap|last {{=}} new_state()}}

assign {{nowrap|len(last) {{=}} len(p) + 1}}

while {{nowrap|δ(p, x)}} is undefined:

assign {{nowrap|δ(p, x) {{=}} last, p {{=}} link(p)}}

define {{nowrap|q {{=}} δ(p, x)}}

if {{nowrap|q {{=}} last}}:

assign {{nowrap|link(last) {{=}} q0}}

else if {{nowrap|len(q) {{=}} len(p) + 1}}:

assign {{nowrap|link(last) {{=}} q}}

else:

define {{nowrap|cl {{=}} new_state()}}

assign {{nowrap|len(cl) {{=}} len(p) + 1}}

assign {{nowrap|δ(cl) {{=}} δ(q), link(cl) {{=}} link(q)}}

assign {{nowrap|link(last) {{=}} link(q) {{=}} cl}}

while {{nowrap|δ(p, x) {{=}} q}}:

assign {{nowrap|δ(p, x) {{=}} cl, p {{=}} link(p)}}

Here q_0 is the initial state of the automaton and new_state() is a function creating new state for it. It is assumed last, len, link and δ are stored as global variables.

= Complexity =

Complexity of the algorithm may vary depending on the underlying structure used to store transitions of the automaton. It may be implemented in O(n \log |\Sigma|) with O(n) memory overhead or in O(n) with O(n |\Sigma|) memory overhead if one assumes that memory allocation is done in O(1). To obtain such complexity, one has to use the methods of amortized analysis. The value of len(p) 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 len(p) never exceeds n 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.

Generalizations

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.{{harvp|Паращенко|2007|loc=|pp=19—22}} Similar transforms are possible in both directions to switch between the suffix automaton of S and the suffix tree of reversed string S^R. Other than this several generalizations were developed to construct an automaton for the set of strings given by trie, compacted suffix automation (CDAWG), to maintain the structure of the automaton on the sliding window,{{harvp|Blumer|1987|loc=|p=451}} and to construct it in a bidirectional way, supporting the insertion of a characters to both the beginning and the end of the string.{{harvp|Inenaga|2003|loc=|p=1}}

= Compacted suffix automaton =

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 \overset{\scriptstyle{\longleftrightarrow}}{\gamma} of a word \gamma is the longest word \omega = \beta \gamma \alpha, such that every occurrence of \gamma in S is preceded by \beta and succeeded by \alpha. 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 \overset{\scriptstyle\longleftrightarrow}{\gamma} = \overset{\scriptstyle\leftarrow}{\overset{\rightarrow}{\gamma}} = \overset{\rightarrow}{\overset{\scriptstyle\leftarrow}{\gamma}}. In terms of two-way extensions compacted automaton is defined as follows:

{{Math theorem

|math_statement = Compacted suffix automaton of the word S is defined by a pair (V, E), where:

  • V = \{\overleftrightarrow \omega : \omega \in \Sigma^*\} is a set of automaton states;
  • E = \{(\overleftrightarrow \omega, x \alpha, \overleftrightarrow {\omega x}) : x \in \Sigma, \alpha \in \Sigma^*, \overleftrightarrow{\omega x} = \overleftrightarrow{\omega} x\alpha\} is a set of automaton transitions.

}}

Two-way extensions induce an equivalence relation \overset{\scriptstyle\longleftrightarrow}{\alpha} = \overset{\scriptstyle\longleftrightarrow}{\beta} 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 (\overset{\scriptstyle{\rightarrow}}{\alpha\,}=\overset{\scriptstyle{\rightarrow}}{\beta\,}) \vee (\overset{\scriptstyle{\leftarrow}}{\alpha} = \overset{\scriptstyle{\leftarrow}}{\beta}), which highlights the fact that a compacted automaton may be obtained by both gluing suffix tree vertices equivalent via \overset{\scriptstyle{\leftarrow}}{\alpha} = \overset{\scriptstyle{\leftarrow}}{\beta} relation (minimization of the suffix tree) and gluing suffix automaton states equivalent via \overset{\scriptstyle{\rightarrow}}{\alpha\,}=\overset{\scriptstyle{\rightarrow}}{\beta\,} relation (compaction of suffix automaton).{{harvp|Blumer|Blumer|Haussler|McConnell|1987|loc=|p=|pp=585—588}} If words \alpha and \beta have same right extensions, and words \beta and \gamma have same left extensions, then cumulatively all strings \alpha, \beta and \gamma have same two-way extensions. At the same time it may happen that neither left nor right extensions of \alpha and \gamma coincide. As an example one may take S=\beta=ab, \alpha=a and \gamma=b, for which left and right extensions are as follows: \overset{\scriptstyle{\rightarrow}}{\alpha\,}=\overset{\scriptstyle{\rightarrow}}{\beta\,}=ab=\overset{\scriptstyle{\leftarrow}}{\beta} = \overset{\scriptstyle{\leftarrow}}{\gamma}, but \overset{\scriptstyle{\rightarrow}}{\gamma\,}=b and \overset{\scriptstyle{\leftarrow}}{\alpha}=a. 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 n+1 which implies that compacted suffix automaton of the string having length n has at most n+1 states. The amount of transitions in such automaton is at most 2n-2.

= Suffix automaton of several strings =

Consider a set of words T=\{S_1, S_2, \dots, S_k\}. 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 n = |S_1|+ |S_2|+ \dots + |S_k|. The algorithm is similar to the construction of single-word automaton except instead of last state, function add_letter would work with the state corresponding to the word \omega_i assuming the transition from the set of words \{\omega_1, \dots, \omega_i, \dots, \omega_k\} to the set \{\omega_1, \dots, \omega_i x, \dots, \omega_k\}.{{harvp|Blumer|Blumer|Haussler|McConnell|1987|loc=|p=|pp=588—589}}{{harvp|Blumer|Blumer|Haussler|McConnell|1987|loc=|p=593|pp=}}

This idea is further generalized to the case when T is not given explicitly but instead is given by a prefix tree with Q vertices. Mohri et al. showed such an automaton would have at most 2Q-2 and may be constructed in linear time from its size. At the same time, the number of transitions in such automaton may reach O(Q|\Sigma|), for example for the set of words T=\{\sigma_1, a\sigma_1, a^2\sigma_1, \dots, a^n \sigma_1, a^n \sigma_2, \dots, a^n \sigma_k \} over the alphabet \Sigma=\{a,\sigma_1,\dots,\sigma_k\} the total length of words is equal to O(n^2+nk), the number of vertices in corresponding suffix trie is equal to O(n+k) and corresponding suffix automaton is formed of O(n+k) states and O(nk) 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.{{harvp|Mohri|Moreno|Weinstein|2009|loc=|страницы=|p=|pp=3558—3560}}

= Sliding window =

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 k its characters while the string is updated. This is because compressing data is usually expressively large and using O(n) memory is undesirable. In 1985, Janet Blumer developed an algorithm to maintain a suffix automaton on a sliding window of size k in O(nk) worst-case and O(n \log k) on average, assuming characters are distributed independently and uniformly. She also showed O(nk) complexity cannot be improved: if one considers words construed as a concatenation of several (ab)^m c (ab)^m d words, where k=6m+2, then the number of states for the window of size k would frequently change with jumps of order m, which renders even theoretical improvement of O(nk) for regular suffix automata impossible.{{harvp|Blumer|1987|loc=|p=|pp=461—465}}

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;{{harvp|Fiala|Greene|1989|p=490}} several years later a similar result was obtained with the variation of Ukkonen's algorithm by Jesper Larsson.{{harvp|Larsson|1996}}{{harvp|Brodnik|Jekovec|2018|p=1}} 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.{{harvp|Senft|Dvořák|2008|p=109}}

One way to overcome this obstacle is to allow window width to vary a bit while staying O(k). 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 k but it is guaranteed to be at least k and at most 2k+1 while providing linear overall complexity of the algorithm.{{harvp|Inenaga|Shinohara|Takeda|Arikawa|2004}}

Applications

Suffix automaton of the string S may be used to solve such problems as:{{harvp|Crochemore|Hancart|1997|pp=36—39|loc=}}{{harvp|Crochemore|Hancart|1997|страницы=|loc=|pp=39—41}}

  • Counting the number of distinct substrings of S in O(|S|) on-line,
  • Finding the longest substring of S occurring at least twice in O(|S|),
  • Finding the longest common substring of S and T in O(|T|),
  • Counting the number of occurrences of T in S in O(|T|),
  • Finding all occurrences of T in S in O(|T|+k), where k is the number of occurrences.

It is assumed here that T is given on the input after suffix automaton of S is constructed.

Suffix automata are also used in data compression,{{harvp|Yamamoto|I|Bannai|Inenaga|2014|loc=|p=675}} music retrieval{{harvp|Crochemore|Iliopoulos|Navarro|Pinzon|2003|loc=|p=211}}{{harvp|Mohri|Moreno|Weinstein|2009|p=3553}} and matching on genome sequences.{{harvp|Faro|2016|loc=|p=145}}

References

{{reflist}}

=Bibliography=

{{refbegin|2}}

  • {{cite book|doi=10.1007/3-540-13345-3_9 |chapter=Building the minimal DFA for the set of all subwords of a word on-line in linear time |title=Automata, Languages and Programming |series=Lecture Notes in Computer Science |date=1984 |last1=Blumer |first1=A. |last2=Blumer |first2=J. |last3=Ehrenfeucht |first3=A. |last4=Haussler |first4=D. |last5=McConnell |first5=R. |volume=172 |pages=109–118 |isbn=978-3-540-13345-2 }}
  • {{cite journal|doi=10.1145/28869.28873 |zbl=1433.68118 |title=Complete inverted files for efficient text retrieval and analysis |date=1987 |last1=Blumer |first1=A. |last2=Blumer |first2=J. |last3=Haussler |first3=D. |last4=McConnell |first4=R. |last5=Ehrenfeucht |first5=A. |journal=Journal of the ACM |volume=34 |issue=3 |pages=578–595 }}
  • {{cite journal|doi=10.1016/0196-6774(87)90045-9 |zbl=0636.68109 |title=How much is that DAWG in the window? A moving window algorithm for the directed acyclic word graph |date=1987 |last1=Blumer |first1=Janet A. |journal=Journal of Algorithms |volume=8 |issue=4 |pages=451–469 }}
  • {{cite journal|doi=10.3390/A11080118 |doi-access=free |zbl=1458.68043 |title=Sliding Suffix Tree |date=2018 |last1=Brodnik |first1=Andrej |last2=Jekovec |first2=Matevž |journal=Algorithms |volume=11 |issue=8 |page=118 }}
  • {{cite book|doi=10.1007/978-3-642-82456-2_7 |chapter=Efficient and Elegant Subword-Tree Construction |title=Combinatorial Algorithms on Words |date=1985 |last1=Chen |first1=M. T. |last2=Seiferas |first2=Joel |pages=97–107 |isbn=978-3-642-82458-6 }}
  • {{cite book|doi=10.1007/978-3-662-07675-0_9 |chapter=Automata for Matching Patterns |title=Handbook of Formal Languages |date=1997 |last1=Crochemore |first1=Maxime |last2=Hancart |first2=Christophe |pages=399–462 |isbn=978-3-642-08230-6 }}
  • {{cite book|doi=10.1007/3-540-63246-8_12 |chapter=On compact directed acyclic word graphs |title=Structures in Logic and Computer Science |series=Lecture Notes in Computer Science |date=1997 |last1=Crochemore |first1=Maxime |last2=Vérin |first2=Renaud |volume=1261 |pages=192–211 |isbn=978-3-540-63246-7 }}
  • {{cite book|doi=10.1007/978-3-540-39984-1_16 |chapter=A Bit-Parallel Suffix Automaton Approach for (δ,γ)-Matching in Music Retrieval |title=String Processing and Information Retrieval |series=Lecture Notes in Computer Science |date=2003 |last1=Crochemore |first1=Maxime |last2=Iliopoulos |first2=Costas S. |last3=Navarro |first3=Gonzalo |last4=Pinzon |first4=Yoan J. |volume=2857 |pages=211–223 |isbn=978-3-540-20177-9 }}
  • {{cite book |last1=Serebryakov |first1=Vladimir |last2=Galochkin |first2=Maksim Pavlovich |last3=Furugian |first3=Meran Gabibullaevich |last4=Gonchar |first4=Dmitriy Ruslanovich |year=2006 |title=Теория и реализация языков программирования: Учебное пособие |url=http://trpl7.ru/t-books/_TRYAPBOOK_pdf.pdf |language=Russian |location=Moscow |publisher=MZ Press |isbn=5-94073-094-9}}
  • {{cite book|doi=10.1007/978-3-319-38827-4_12 |chapter=Evaluation and Improvement of Fast Algorithms for Exact Matching on Genome Sequences |title=Algorithms for Computational Biology |series=Lecture Notes in Computer Science |date=2016 |last1=Faro |first1=Simone |volume=9702 |pages=145–157 |isbn=978-3-319-38826-7 }}
  • {{cite journal|doi=10.1145/63334.63341 |title=Data compression with finite windows |date=1989 |last1=Fiala |first1=E. R. |last2=Greene |first2=D. H. |journal=Communications of the ACM |volume=32 |issue=4 |pages=490–505 }}
  • {{cite book|doi=10.4230/LIPICS.MFCS.2016.38 |doi-access=free |zbl=1398.68703 |date=2016 |last1=Fujishige |first1=Yuta |last2=Tsujimaru |first2=Yuki |last3=Inenaga |first3=Shunsuke |last4=Bannai |first4=Hideo |last5=Takeda |first5=Masayuki |chapter=Computing DAWGs and Minimal Absent Words in Linear Time for Integer Alphabets |title=41st International Symposium on Mathematical Foundations of Computer Science (MFCS 2016) |series=Leibniz International Proceedings in Informatics |pages=38:1–38:14 |publisher=Schloss Dagstuhl – Leibniz-Zentrum für Informatik }}
  • {{cite book |last1=Hopcroft |first1=John Edward |last2=Ullman |first2=Jeffrey David |year=1979 |title=Introduction to Automata Theory, Languages, and Computation |edition=1st |location=Massachusetts |publisher=Addison-Wesley |isbn=978-81-7808-347-6 |ol=9082218M}}
  • {{cite journal |last=Inenaga |first=Shunsuke |date=2003 |title=Bidirectional Construction of Suffix Trees |url=https://str.i.kyushu-u.ac.jp/~inenaga/papers/NJC_bidirectional.pdf |journal=Nordic Journal of Computing |volume=10 |issue=1 |pages=52–67 |citeseerx=10.1.1.100.8726}}
  • {{cite journal|doi=10.1016/J.DAM.2004.04.012 |zbl=1084.68137 |title=On-line construction of compact directed acyclic word graphs |date=2005 |last1=Inenaga |first1=Shunsuke |last2=Hoshino |first2=Hiromasa |last3=Shinohara |first3=Ayumi |last4=Takeda |first4=Masayuki |last5=Arikawa |first5=Setsuo |last6=Mauri |first6=Giancarlo |last7=Pavesi |first7=Giulio |journal=Discrete Applied Mathematics |volume=146 |issue=2 |pages=156–179 }}
  • {{cite book |last1=Inenaga |first1=Shunsuke |last2=Hoshino |first2=Hiromasa |last3=Shinohara |first3=Ayumi |last4=Takeda |first4=Masayuki |last5=Arikawa |first5=Setsuo |year=2001 |chapter=Construction of the CDAWG for a trie |chapter-url=http://www.shino.ecei.tohoku.ac.jp/~ayumi/papers/PSC2001.pdf |title=Prague Stringology Conference. Proceedings |pages=37–48 |citeseerx=10.1.1.24.2637}}
  • {{cite journal|doi=10.1016/S1570-8667(03)00064-9 |zbl=1118.68755 |title=Compact directed acyclic word graphs for a sliding window |date=2004 |last1=Inenaga |first1=Shunsuke |last2=Shinohara |first2=Ayumi |last3=Takeda |first3=Masayuki |last4=Arikawa |first4=Setsuo |journal=Journal of Discrete Algorithms |volume=2 |pages=33–51 }}
  • {{cite book|doi=10.1109/DCC.1996.488324 |chapter=Extended application of suffix trees to data compression |title=Proceedings of Data Compression Conference - DCC '96 |date=1996 |last1=Larsson |first1=N.J. |pages=190–199 |isbn=0-8186-7358-3 }}
  • {{cite journal|doi=10.1016/J.TCS.2009.03.034 |zbl=1194.68143 |title=General suffix automaton construction algorithm and space bounds |date=2009 |last1=Mohri |first1=Mehryar |last2=Moreno |first2=Pedro |last3=Weinstein |first3=Eugene |journal=Theoretical Computer Science |volume=410 |issue=37 |pages=3553–3562 }}
  • {{Cite book |last=Паращенко |first=Дмитрий А. |year=2007 |title=Обработка строк на основе суффиксных автоматов |url=http://is.ifmo.ru/diploma-theses/paraschenko/doc.pdf |language=Russian |location=Saint Petersburg |publisher=ITMO University}}
  • {{Cite book |last=Pratt |first=Vaughan Ronald |year=1973 |title=Improvements and applications for the Weiner repetition finder |oclc=726598262}}
  • {{Cite book |last=Рубцов |first=Александр Александрович |url=http://rubtsov.su/public/books/zz-a5-online.pdf |year=2019 |title=Заметки и задачи о регулярных языках и конечных автоматах |language=Russian |location=Moscow |publisher=Moscow Institute of Physics and Technology |isbn=978-5-7417-0702-9}}
  • {{cite journal|doi=10.1016/J.EJC.2017.07.021 |zbl=1374.68131 |title=EERTREE: An efficient data structure for processing palindromes in strings |date=2018 |last1=Rubinchik |first1=Mikhail |last2=Shur |first2=Arseny M. |journal=European Journal of Combinatorics |volume=68 |pages=249–265 |arxiv=1506.04862 }}
  • {{cite book|doi=10.1007/978-3-540-89097-3_12 |chapter=Sliding CDAWG Perfection |title=String Processing and Information Retrieval |series=Lecture Notes in Computer Science |date=2008 |last1=Senft |first1=Martin |last2=Dvořák |first2=Tomáš |volume=5280 |pages=109–120 |isbn=978-3-540-89096-6 }}
  • {{cite journal|doi=10.1007/BF01084395 |zbl=0509.68043 |title=Detection of periodicities and string-matching in real time |date=1983 |last1=Slisenko |first1=A. O. |journal=Journal of Soviet Mathematics |volume=22 |issue=3 |pages=1316–1387 }}
  • {{cite book|doi=10.1109/SWAT.1973.13 |chapter=Linear pattern matching algorithms |title=14th Annual Symposium on Switching and Automata Theory (Swat 1973) |date=1973 |last1=Weiner |first1=Peter |pages=1–11 }}
  • {{cite book |doi=10.4230/LIPICS.STACS.2014.675 |doi-access=free |zbl=1359.68341 |date=2014 |last1=Yamamoto |first1=Jun'ichi |last2=I |first2=Tomohiro |last3=Bannai |first3=Hideo |last4=Inenaga |first4=Shunsuke |last5=Takeda |first5=Masayuki |chapter=Faster Compact On-Line Lempel-Ziv Factorization |title=31st International Symposium on Theoretical Aspects of Computer Science (STACS 2014) |series=Leibniz International Proceedings in Informatics |pages=675–686 |publisher=Schloss Dagstuhl – Leibniz-Zentrum für Informatik }}

{{refend}}