In computer science, Ukkonen's algorithm is a linear-time, online algorithm for constructing suffix trees, proposed by Esko Ukkonen in 1995.[1] The algorithm begins with an implicit suffix tree containing the first character of the string. Then it steps through the string, adding successive characters until the tree is complete. This order addition of characters gives Ukkonen's algorithm its "on-line" property. The original algorithm presented by Peter Weiner proceeded backward from the last character to the first one from the shortest to the longest suffix.[2] A simpler algorithm was found by Edward M. McCreight, going from the longest to the shortest suffix.[3]
While generating suffix tree using Ukkonen's algorithm, we will see implicit suffix tree in intermediate steps depending on characters in string S. In implicit suffix trees, there will be no edge with $ (or any other termination character) label and no internal node with only one edge going out of it.
Ukkonen's algorithm constructs an implicit suffix tree T for each prefix S[1...i] of S (S being the string of length n). It first builds T using the 1 character, then T using the 2 character, then T using the 3 character, ..., T using the n character. You can find the following characteristics in a suffix tree that uses Ukkonen's algorithm:
Suffix extension is all about adding the next character into the suffix tree built so far. In extension j of phase i+1, algorithm finds the end of S[j...i] (which is already in the tree due to previous phase i) and then it extends S[j...i] to be sure the suffix S[j...i+1] is in the tree. There are three extension rules:
One important point to note is that from a given node (root or internal), there will be one and only one edge starting from one character. There will not be more than one edge going out of any node starting with the same character.
The naive implementation for generating a suffix tree going forward requires or even time complexity in big O notation, where is the length of the string. By exploiting a number of algorithmic techniques, Ukkonen reduced this to (linear) time, for constant-size alphabets, and in general, matching the runtime performance of the earlier two algorithms.
To better illustrate how a suffix tree is constructed using Ukkonen's algorithm, we can consider the string S = xabxac
.
T1
S[1]
by adding the first character of the string. Rule 2 applies, which creates a new leaf node.T2
S[1..2]
by adding suffixes of xa
(xa
and a
). Rule 1 applies, which extends the path label in existing leaf edge. Rule 2 applies, which creates a new leaf node.T3
S[1..3]
by adding suffixes of xab
(xab
, ab
and b
). Rule 1 applies, which extends the path label in existing leaf edge. Rule 2 applies, which creates a new leaf node.T4
S[1..4]
by adding suffixes of xabx
(xabx
, abx
, bx
and x
). Rule 1 applies, which extends the path label in existing leaf edge. Rule 3 applies, do nothing.T5
S[1..5]
by adding suffixes of xabxa
(xabxa
, abxa
, bxa
, xa
and a
). Rule 1 applies, which extends the path label in existing leaf edge. Rule 3 applies, do nothing.T6
S[1..6]
by adding suffixes of xabxac
(xabxac
, abxac
, bxac
, xac
, ac
and c
). Rule 1 applies, which extends the path label in existing leaf edge. Rule 2 applies, which creates a new leaf node (in this case, three new leaf edges and two new internal nodes are created).