A suffix automaton (SAM) of a string ( S ) of length ( n ) is a finite automaton that recognizes all suffixes of ( S ). It is a directed acyclic graph (DAG) with states, transitions, and suffix links. Key properties:
States: Represent equivalence classes of end positions of substrings.
Transitions: Directed edges labeled by characters.
Suffix Links: Point to the longest proper suffix state.
Deterministic: Each state has at most one transition per character.
1.2 Comparison with Other Structures
Suffix Trees: Explicitly represent all suffixes but have ( O(n) ) space and construction time.
Suffix Automata: More compact (linear size) and faster for certain operations (e.g., substring search).
2. Mathematical Foundations
2.1 Graph Theory Basics
Vertices (States): ( Q \subseteq \mathbb{N} ).
Edges (Transitions): ( E \subseteq Q \times \Sigma \times Q ), where ( \Sigma ) is the alphabet.
Define an equivalence relation ( \sim ) on positions ( {0, 1, \dots, n} ):
For positions ( i, j ), ( i \sim j ) iff all suffixes ending at ( i ) and ( j ) are identical.
States ( Q ) are the equivalence classes of ( \sim ).
2.3 Formal Definitions
Length: ( \text{len}(q) = \max { i \mid i \in [q] } ), where ( [q] ) is the class of ( q ).
Transition: ( \delta(q, c) = q' ) iff ( \exists i \in [q] ) such that ( S[i+1] = c ) and ( i+1 \in [q'] ).
Suffix Link: ( \text{link}(q) ) is the class of suffixes of substrings in ( q ).
3. Construction of Suffix Automaton
3.1 Algorithm Overview
The algorithm builds the automaton incrementally by processing each character of ( S ). Variables:
last: Current end state.
cur: New state created for the current character.
link: Suffix links between states.
3.2 Pseudocode
python
Initialize:
new_state() creates a new state with len = 0, link = -1.
sa = [new_state()] # State 0 is the initial state.
last = 0
for c in S:
cur = new_state()
cur.len = last.len + 1
p = last
while p != -1 and sa[p].trans[c] == 0:
sa[p].trans[c] = cur
p = sa[p].link
if p == -1:
sa[cur].link = 0
else:
q = sa[p].trans[c]
if sa[q].len == sa[p].len + 1:
sa[cur].link = q
else:
mid = clone(q)
sa[mid].len = sa[p].len + 1
sa[mid].link = sa[q].link
while p != -1 and sa[p].trans[c] == q:
sa[p].trans[c] = mid
p = sa[p].link
sa[q].link = mid
sa[cur].link = mid
last = cur
3.3 Key Steps
New State Creation: ( \text{cur} ) tracks the new end state.
Transition Setup: Traverse suffix links to set transitions to ( \text{cur} ).
Cloning (Edge Splitting): Creates a new state ( \text{mid} ) when ( q )’s length is insufficient.
4. Theoretical Properties
4.1 Linear Size
Theorem: A suffix automaton has ( \leq 2n ) states and transitions.
Proof Sketch: Each new character adds at most one new state (cur) or splits an edge (mid). Total states ( \leq 2n ).
4.2 Suffix Link Tree
Theorem: Suffix links form a tree rooted at state 0.
Proof: Each non-root state has exactly one parent (its link), forming a tree.
4.3 Determinism
Theorem: The automaton is deterministic.
Proof: Transitions are uniquely defined for each state and character.
4.4 Substring Representation
Theorem: All substrings of ( S ) are represented exactly once.
Proof: By equivalence class construction.
5. Applications
5.1 Substring Search
To check if ( T \subseteq S ):
Simulate ( T ) on the automaton. If a final state is reached, ( T ) exists.
Proof: Each state contributes ( \text{len}(q) - \text{len}(\text{link}(q)) ) new substrings.
5.3 Longest Repeated Substring
Algorithm:
Find the state ( q ) maximizing ( \text{len}(q) - \text{len}(\text{link}(q)) ).
6. Examples and Exercises
6.1 Example: "ab"
Initial State (0): ( \text{len}=0 ).
Add 'a':
Create state 1 (( \text{len}=1 ), link=0).
Add 'b':
Create state 2 (( \text{len}=2 )).
Transition from 0 and 1 to 2 on 'b'.
State 2’s link is 0 (since 'b'’s suffix is empty).
6.2 Exercises
Prove Linear Size: Use induction and edge-splitting arguments.
Construct SAM for "aab":
Follow algorithm steps manually.
Substring Count: Compute distinct substrings for "ababa".
7. Conclusion
Suffix automata provide a mathematically elegant and efficient solution to string processing problems. Their linear size and time complexity, along with strong theoretical guarantees, make them a cornerstone in algorithmic stringology.
Further Reading: Ukkonen’s algorithm, Myhill-Nerode theorem, and advanced applications in bioinformatics.