Fast and Memory-Efficient BWT Construction of Repetitive Texts Using Lyndon Grammars
Abstract
The Burrows-Wheeler Transform () serves as the basis for many important sequence indexes. On very large datasets (e.g. genomic databases), classical construction algorithms are often infeasible because they usually need to have the entire dataset in main memory. Fortunately, such large datasets are often highly repetitive. It can thus be beneficial to compute the from a compressed representation. We propose an algorithm for computing the via the Lyndon straight-line program, a grammar based on the standard factorization of Lyndon words. Our algorithm can also be used to compute the extended () of a multiset of sequences. We empirically evaluate our implementation and find that we can compute the and of very large datasets faster and/or with less memory than competing methods.
Keywords and phrases:
Burrows-Wheeler Transform, Grammar compression2012 ACM Subject Classification:
Theory of computation Online algorithms ; Theory of computation Shared memory algorithms ; Theory of computation Data compression ; Theory of computation Sorting and searching ; Mathematics of computing Combinatorics on wordsSupplementary Material:
Software (Source Code): https://gitlab.com/qwerzuiop/lyndongrammar [38]archived at
swh:1:dir:0274cae4abb893b49bc035ef3a57986a418afe40
Editors:
Anne Benoit, Haim Kaplan, Sebastian Wild, and Grzegorz HermanSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
The Burrows-Wheeler Transform () [13] is one of the most important data structures in sequence analysis and is applied in a wide range of fields from data compression to bioinformatics. It is obtained by assigning the th symbol of the to the last character of the th lexicographically smallest conjugate of the input text. The can be computed in linear time and space. The run-length compressed () uses the fact that the number of equal-character runs in the is small compared to the text length when the text is repetitive [13]. Mantaci et al. [32] extended the notion of the to string collections: the extended () of a string collection consists of the last characters of the strings in arranged in infinite periodic order (see Section 2 for definitions).
The serves as the basis of several self-indexes, i.e., data structures supporting access to the text as well as pattern matching queries. One of the most successful such indexes is the FM-index [20], which is used e.g. in the important bioinformatics programs BWA [30] and Bowtie [28]. However, the FM-index uses space proportional to the size of the dataset and is thus not applicable for huge datasets that vastly exceed the size of main memory. Such datasets often contain data from thousands or millions of individuals from the same species and are therefore extremely repetitive. The r-index [22] is based on the and is the first text index of size proportional to the number of runs in the . It can be constructed in space proportional to from the , which raises the problem of constructing the of huge datasets with small working memory. For this reason, various tools have emerged that exploit the repetitiveness of these datasets.
The Lyndon grammar of a text is a straight-line program (SLP), i.e., a context-free grammar in Chomsky normal form that generates a single text, where each symbol corresponds to a node in the Lyndon tree. It was introduced in [25] as the basis for a self-index. In this paper, we describe a method for efficiently computing several variants of a text or a text collection via the input’s Lyndon grammar and show that our method surpasses other tools with respect to time or memory used, especially when using multiple threads.
1.1 Related work
Boucher et al. [11] developed prefix-free parsing (PFP), a dictionary-compression technique that results in a dictionary and parse from which the () can be constructed in space proportional to their total size and in time linear to the size of the dataset. While the dictionary typically grows slowly as the size of the dataset increases, the parse grows linearly with the input size (with a small constant chosen at runtime). Consequently, for extremely large and repetitive datasets, “the parse is more burdensome than the size of the dictionary” [41]. In an effort to remedy this, Oliva et al. [41] developed recursive prefix-free parsing, which applies PFP to the parse. PFP has also been used to construct the [10]. The currently fastest implementation for computing the suffix array (and ) for general texts is libsais and is based on the linear-time suffix array induced sorting (SAIS) algorithm [37, 40]. ropebwt3 uses libsais to compute the of chunks of the input and successively merges these s [29]. Díaz-Domínguez and Navarro used grammar compression to maintain the intermediate data in the SAIS algorithm with low main memory usage, resulting in a linear-time semi-external algorithm that computes a variant of the [14, 17]. Specifically for a collection of sequences where each is similar to a given reference (e.g. human chromosomes), Masillo introduced CMS-BWT, which computes the via matching statistics [35].
Lyndon words have previously been used in the construction of and play an important role in the original . In [33, 34], a strategy for computing was presented where “local suffixes” (i.e., substrings ending at boundaries between Lyndon factors) can be processed separately in each Lyndon factor. Later, Baier effectively generalized the underlying idea by showing that sorting the suffixes by their longest Lyndon prefixes can be used for linear time construction [2]. The bijective () is the of the text’s Lyndon factors [23, 27, 32]. Bonomo et al. showed that it is possible to compute the via the and gave an construction algorithm based on properties of Lyndon words [8, 9]. Later, Bannai et al. modified the SAIS algorithm to compute the in linear time [3]. Since publication, Baier’s algorithm was improved in terms of time and memory usage [6, 39] and further generalized such that it can be used to compute and in linear time [40]. The principles used this generalized algorithm lie at the base of our algorithm for computing the , and variants from a Lyndon grammar.
1.2 Our contributions
Let be the size of a text or text collection, the maximum size of a string in the text collection, and the size of the Lyndon SLP of the text or text collection. We give two online in-memory algorithms for constructing the Lyndon SLP with and worst-case time complexity, both using words of memory. Notably, the text or text collection is streamed from right to left and does not have to reside in main memory. Additionally, we give an expected linear-time algorithm that uses words of memory. Furthermore, we give an algorithm for sorting the symbols of a Lyndon SLP lexicographically by their generated strings, and an algorithm for constructing the (run-length compressed) , , or from a Lyndon SLP. We implemented our algorithms and demonstrate empirically that we can construct the , or of repetitive texts or text collections faster and/or with less space than competing methods.
2 Preliminaries
For we denote the set by the interval notations . For an array we analogously denote the subarray from to by . We use zero-based indexing, i.e., the first entry of the array is .
A string of length over an alphabet is a sequence of characters from . We denote the length of by and the th symbol of by , i.e., strings are zero-indexed. In this paper we assume any string of length to be over a totally ordered and linearly sortable alphabet (i.e., the characters in can be sorted in ). Analogous to arrays we denote the substring from to by . For we let be the empty string . For two strings and and an integer we let be the concatenation of and and denote the -times concatenation of by . A string is primitive if it is non-periodic, i.e., implies and . The suffix of a string of length is the substring and is denoted by . Similarly, the substring is a prefix of . A suffix (prefix) is proper if (). For two possibly empty strings and , is a conjugate of .
We assume totally ordered alphabets. This induces a total order on strings. Specifically, we say a string of length is lexicographically smaller than another string of length if and only if there is some such that and either or and , and write in this case. A non-empty string is in its canonical form if and only if it is lexicographically minimal among its conjugates. If is additionally strictly lexicographically smaller than all of its other conjugates, is a Lyndon word. Equivalently, is a Lyndon word if and only if is lexicographically smaller than all its proper suffixes [18].
In the following, we use abbabcbcabb as our running example.
Theorem 1 (Chen-Fox-Lyndon theorem [15]).
Any non-empty string has a unique Lyndon factorization, that is, there is a unique sequence of Lyndon words (Lyndon factors) with .
The Lyndon factorization of our running example is .
Definition 2 (Standard Factorization [15]).
The standard factorization of a Lyndon word of length is the tuple where and is the longest proper suffix of that is Lyndon. The standard factorization of a Lyndon word with always exists and both and are Lyndon.
The standard factorization of the first Lyndon factor of our running example is as abcbc is Lyndon and no longer proper suffix is Lyndon.
Lemma 3 ([40, Lemma 3.20]).
Any Lyndon word with is of the form , where are the Lyndon factors of .
Figure 1 illustrates the following definitions.
Definition 4 (next smaller suffix array, ).
Let the next smaller suffix array of a string be such that .
Definition 5 (Infinite Periodic Order).
We write if and only if the infinite concatenation is lexicographically smaller than the infinite concatenation .
For instance, and since .
Definition 6 (Bijective Burrows-Wheeler Transform ()).
The bijective Burrows-Wheeler Transform () of a string is the string obtained by taking the last character of each conjugate of the Lyndon factors of arranged in infinite periodic order.
Definition 7 (Extended Burrows-Wheeler Transform ()).
The extended Burrows-Wheeler Transform () of a multiset of strings is the string obtained by taking the last character of each conjugate of the strings in arranged in infinite periodic order.
Note that, by definition, the of a string is the of the Lyndon factors of . Similarly, the of a multiset of strings is the of the concatenation of the canonical forms of the strings in arranged in lexicographically decreasing order [3] (the Lyndon factors of the resulting string are conjugates of the roots of the strings in ).111The was originally defined for sets of primitive strings [32]. This limitation is unnecessary [10, 40].
For a primitive string , its , the of , and the of the smallest conjugate of are identical. Consequently, the $- of a string (i.e., , where is smaller than all characters in ) commonly computed via the suffix array of , is identical to .
2.1 Lyndon Grammar
Definition 8 (Lyndon Tree/Forest [4, 25]).
The Lyndon tree of a Lyndon word – denoted by – is the ordered full binary tree defined recursively as follows: If , then consists of a single node labelled by , and if and has the standard factorization , the root of is labelled by , the left child of the root is , and the right child of the root is . For a non-Lyndon word, we let the Lyndon Forest be the sequence of Lyndon trees of the Lyndon factors.
The Lyndon Forest of the running example is shown in Figure 2. Note that the Lyndon Forest of a string is closely related to the Lyndon array of [25], where is the length of the longest prefix of that is Lyndon (equivalently, [7], cf. Figure 1). A succinct representation of occupies bits and can be computed in linear time directly from the text [7, 16, 31].
Definition 9 (Lyndon straight-line program [25]).
A Lyndon straight-line program (SLP) is a context-free grammar over an alphabet in Chomsky normal form, where is a Lyndon word for each symbol and each rule is such that the standard factorization of is , where denotes the unique string generated by . A symbol is called terminal symbol if there is a rule of the form (), and non-terminal symbol otherwise. When are the root symbols of ,222In slight deviation of the usual definition of SLPs, we allow multiple root/start symbols. The reason for this is that we want to represent arbitrary strings with the Lyndon SLP, not just Lyndon words. generates , and is the Lyndon factorization of . The size of is the number of production rules plus the number of start symbols. The derivation tree of the SLP is a labelled ordered tree where the root node has the children . We assume that distinct symbols generate distinct words.
A Lyndon SLP arises from renaming the nodes of a Lyndon Forest such that two nodes with isomorphic subtrees (i.e., representing the same string) are assigned the same symbol. The Lyndon Forest and Lyndon SLP of our running example can be seen in Figure 2.
Clearly, the size of a Lyndon SLP is bounded by . This bound is tight and there is no non-trivial bound in terms of e.g. the number of runs in the , the size of the smallest string attractor [26] or the size of the smallest SLP: consider for . The smallest SLP generating has size , its has two runs, and the smallest string attractor has size , but every suffix of is Lyndon and thus the Lyndon SLP generating has symbols.
In the following, we assume the set of symbols of an SLP to be numbered consecutively, i.e., .
3 Properties of sorted Lyndon Grammars
Consider a Lyndon grammar where the symbols are sorted lexicographically, i.e., . In this section we examine such Lyndon SLPs and find properties that enable sorting any Lyndon SLP lexicographically in linear time (Section 4) and give rise to an online algorithm for constructing Lyndon SLPs (Section 6.3). Note that the grammar shown in Figure 2 is lexicographically sorted.
Definition 10 (Prefix- and Suffix-symbols).
Define () as the set of symbols occurring on the leftmost (rightmost) path of ’s derivation tree. More formally, if is a terminal symbol define , and if is a non-terminal symbol define and . We correspondingly define () to be the set of symbols on whose leftmost (rightmost) path of the derivation tree occurs, i.e. and .
For example, we have , and for the grammar shown in Figure 2.
Definition 11.
Define as the set of symbols occurring in ’s derivation tree. More formally, if is a terminal symbol define , and if is a non-terminal symbol define .
Let be the largest symbol whose derivation tree has on the leftmost path, i.e., . Similarly, let be the smallest symbol whose derivation tree has on the leftmost path, i.e. . For example, and in the grammar from Figure 2. In the remainder of this section, we show that all symbols in the interval generate strings with as prefix and even satisfy . This then implies that these intervals form a tree-structure.
Lemma 12.
for all .
Proof.
For we have as is prefix of by definition.
The following lemma follows from basic properties of Lyndon words and the definition of the Lyndon grammar/tree. Its proof is omitted due to space constraints.
Lemma 13.
For all it holds , i.e., occurs on the leftmost path of ’s derivation tree. Equivalently, .
Corollary 14.
Any two intervals and either do not intersect or one is fully contained within the other.
Proof.
Corollary 15.
The intervals induce a forest.
The following definition defines this forest. An example can be seen in Figure 3.
Definition 16.
We say an interval is embedded in an interval if it is a subinterval of , i.e. . If is embedded in and there is no interval embedded in in which is embedded, is a child interval of .
The first-symbol forest is a rooted forest, where each symbol of the SLP corresponds to a node , and is a child of if and only if is a child interval of . Note that the parent of is . Also, the terminal symbols correspond to the roots of the trees in the forest.
Lemma 17.
Let be a symbol of a Lyndon SLP. We have where for all , is a terminal symbol, , and is the Lyndon factorization of .
Proof.
Follows immediately from Lemma 3.
4 Lexicographically sorting a Lyndon Grammar
Given a Lyndon SLP with roots , we want to rename the symbols such that holds if and only if . This is possible in using a similar principle to what is used for computing the Lyndon grouping in [2, 40]: consider a symbol . By Lemma 17 and simple properties of Lyndon words, the symbols in are lexicographically greater than . Therefore, when considering the symbols in lexicographically decreasing order we can “induce” the position of a symbol upon encountering : the symbols in must be the lexicographically largest symbols with as prefix symbol that have not yet been induced.
Algorithm 1 shows the procedure. Throughout, is either the th symbol in the lexicographical order or , and for each inserted , is the index in of the smallest child of that has been inserted into (or if no child has been inserted yet). In the first for-loop, all terminal symbols are inserted into , and in the second for-loop, the remaining symbols are induced. Consequently, after the second for-loop, contains the lexicographic order of the symbols, and is (almost) it’s inverse, i.e. .
There are two operations in Algorithm 1 that are not immediately obvious, namely finding and iterating over all with for a given . For the first, note that the values can be trivially computed in linear-time using the recurrence . Secondly, iterating efficiently over all with can be achieved using a linear-time preprocessing step where we collect for each symbol the symbols which have as second symbol on the right-hand side.
The following theorem follows. Its proof is omitted due to space constraints.
Theorem 18.
Algorithm 1 lexicographically sorts a Lyndon SLP in linear time.
5 BWT construction
In this section, we describe an algorithm that derives the from the Lyndon SLP. Specifically, combining the second phase of GSACA [2, 40] with run-length encoding on the lexicographically sorted Lyndon SLP results in a very efficient algorithm on real data.
The following lemmas and corollary establish a relationship between the lexicographical order of suffixes and the lexicographical order of certain symbols of the Lyndon grammar.
Lemma 19 ([40, Lemma 3.3]).
Let be the longest prefix of that is Lyndon. Then, implies .
Lemma 20.
Consider an occurrence of at position in the text (this implies ). Then, is the longest prefix of that is Lyndon.
Proof.
Follows immediately from the relationship between Lyndon forest and Lyndon grammar [25] and [21, Lemma 15].
Corollary 21.
Consider occurrences of at position and of at position . It holds if .
To see why Corollary 21 is useful, consider a symbol . Each occurrence of in the derivation tree corresponds to a suffix of the text with prefix , and each such suffix introduces an occurrence of the last character of to the in the -interval of . In our running example (cf. Figure 2), each occurrence of introduces a b in the -interval of .
In [40], an array 333 is essentially the analogue to for conjugates of Lyndon factors and the infinite periodic order. A precise definition is omitted because only the relationship between and is relevant for this paper. is computed such that holds for all . This array arises from sorting the conjugates of the Lyndon factors of and replacing each start position of a Lyndon word with the end position of the Lyndon word in . It was shown that Algorithm 2 correctly computes the array in linear time [40].444In [40], Lyndon factors (roots of the Lyndon forest) come after every non-root representing the same Lyndon word. To reflect this, we use if an occurrence of is a root and otherwise. Basically, Lemma 19 implies that the positions in are grouped by the longest Lyndon prefixes of the corresponding suffixes. The longest Lyndon prefix of is . Because by definition, we can proceed by induction from lexicographically small to large.555There is a slight detail omitted here for clarity and brevity. Namely, that for the we are concerned with the next smaller conjugate of the respective Lyndon factor instead of the next smaller suffix [40]. Consider for instance the suffixes starting with c at indices and in our running example (cf. Figure 2). By Lemma 19, they are in the same “Lyndon group” [2] in the sorted list of suffixes because c is their longest Lyndon prefix. We have and . When processing these Lyndon groups in lexicographically increasing order, the relative order of suffixes and is known at the point in time when the Lyndon group c is considered (because they are lexicographically smaller), and can therefore determine the lexicographical order of the suffixes and .
We are now going to transform Algorithm 2 such that it outputs the instead of and works without and the text indices. First, consider a text index and let be the highest node in the Lyndon forest at position . (Equivalently, corresponds to the longest Lyndon word starting at .) Now consider the parent of . By its definition, it is clear that is the right child of . Let be the left child of . We can observe that, starting from , the nodes on the rightmost path (excluding ) correspond exactly to the set . For instance, in our running example there is a node labelled with at position (cf. Figures 1 and 2). This node is the right child of a node labelled with , which has a node labelled with as left child. There are two nodes on the rightmost path from this latter node, namely one at position labelled with and one at position labelled with . We consequently have .
Second, for we have and it follows that is the last character of the string corresponding to ’s left child . Because of these two observations, we can replace the insertion of in Algorithm 2 with the insertion of . Consequently, we can reformulate Algorithm 2 to Algorithm 3.
Theorem 22.
Algorithm 3 correctly computes from the sorted Lyndon SLP in time.
Proof.
The linear worst-case time complexity trivially follows from the fact that each iteration of the inner loop appends at least one character to . A proof of correctness is omitted due to space constraints.
Note that runs in a list in compound: If there is a run of symbol , there is also a run of at least the same length of in for each non-terminal with . Thus, Algorithm 3 often requires much fewer than iterations.
5.1 Computing and
When the text is a Lyndon word, the of is equal to the original of [3]. Since the is independent of the rotation of the input and is a Lyndon word, we have . The latter is the $- commonly computed via the suffix array. Therefore, we can simply compute the $- by prepending a sentinel character to the text. Note that this is trivial and can be done after building the Lyndon grammar of .
The of a set of strings is the same as the of the string that arises from arranging the canonical forms of the strings in in lexicographically decreasing order [40]. In particular, the Lyndon factors of are exactly the canonical forms of the input strings (assuming that the input strings are primitive). Note that, in Algorithm 3, sorting the canonical forms of the input strings is done implicitly in the first for-loop.
Note that the Lyndon grammars of the input strings are independent of each other (as long as equal Lyndon words are assigned equal grammar symbols). Therefore, we can also construct their Lyndon grammars independently of each other while using the same dictionary. As a consequence, it is easy to use our algorithms for parallel construction of the Lyndon grammar of a collection of sequences. Although our algorithm for deriving the from the grammar is not parallelized, parallel construction of the Lyndon grammar leads to a substantial reduction in wall-clock-time because it is by far the most time-consuming part of the pipeline (see Section 7).
5.2 Other variants for string collections
Besides the original , several other variants for string collections have been proposed (see [14] for an overview). Several of the variants are especially suited to be computed with our algorithm, because there the input sequences can be parsed independently (and in parallel) like for the : The dollar- , the multidollar , and the concatenated , where and are smaller than all characters in . This is done by constructing the Lyndon SLP of each (with a shared dictionary) and then applying post-processing steps such that the desired variant can be derived from the resulting SLP. This is possible because the ’s separate the input strings in the sense that no Lyndon word starting inside some can contain a .
More specifically, for the , no post-processing is required and we can just apply our construction algorithm, except that finding the canonical forms of the strings is trivial because and is a Lyndon word. For the and , we replace each in and , respectively, with the roots of the grammar generating and compute the grammar of a rotation of the resulting string using Algorithm 3: both and are Lyndon. Therefore, and and we can apply our construction algorithm to these rotations.
Note that all other variants for string collections given in [14] can be simulated with the multidollar by using different relative orders of the separator symbols.
6 Practical Lyndon grammar construction
As shown in [1], the simple folklore algorithm for constructing the Lyndon array can be used to construct the Lyndon forest. With a fitting lookup data structure, we can trivially construct the Lyndon SLP instead. We essentially use Algorithm 4.
There are two steps that have to be explained: First, one must be able to compare and lexicographically, and secondly, one must be able to find with (this is also called the naming function). For the former, we store for each symbol on the stack a fixed-length prefix of the generated string. In many cases, this is sufficient for determining the lexicographical order. Possibilities to handle the other cases are explained in Sections 6.1, 6.2 and 6.3. The following paragraphs will deal with the naming function.
For the naming function, we use a hash table to find the symbol names. This provides (expected) constant time lookup/insertion.666Note that this is also possible in deterministic time with at most bits of memory, where is the number of symbols of the grammar [42]. In practice however, hash table lookups are unnecessarily slow, especially because we assume the input to be repetitive. In particular, having fewer but longer keys is cache friendlier and thus faster on modern computer architectures.
Specifically, we decrease the number of hash table accesses by partitioning the nodes of the Lyndon forest into heavy and light nodes and querying a second hash table for each heavy node. The naming function is then only used when such a query to does not return an answer. Leaves of the Lyndon forest are considered heavy. An inner node is considered heavy, if and only if its number of immediate heavy descendants (heavy descendants for which only light nodes occur on the path to ) exceeds a fixed constant (see Figure 4).777In our experiments, .
For a heavy node of the Lyndon forest we query with the sequence of immediate heavy descendants of . This means that we only store heavy nodes on the stack, for a light node we instead store all immediate heavy descendants.
Different occurrences of the same symbol represent the exact same Lyndon tree and therefore correspond to the exact same sequence of immediate heavy descendants. Consequently, if a subtree isomorphic to the subtree rooted at a heavy node has occurred previously, the query to is sufficient to resolve it. Otherwise, we use the naming function to find the correct symbol and insert the sequence of immediate heavy descendants with it into .
Note that each heavy node has at most immediate heavy descendants because it has exactly two children (or zero), each of which is either light (and thus has at most immediate heavy descendants) or heavy itself. This implies that the total memory usage increases by a factor of at most .
6.1 Constant time suffix comparisons
Because the Lyndon forest (and thus the Lyndon SLP) is closely related to the Lyndon array, we can use the Lyndon array to decide whether holds in constant time. Specifically, when considering in Algorithm 3, we have while .
Unfortunately, as far as we know, all linear-time algorithms for constructing the Lyndon array directly from the text need random access to the text [1, 7, 19] and thus the text should reside in main memory.
For a of a real-world string collection however, this is typically not a problem because, as noted in Section 5.1, the SLPs of the strings in can be computed independently (with a shared dictionary). Consequently, it suffices to have only the string (and its (succinct) Lyndon array) in main memory whose Lyndon SLP is currently computed. This increases the RAM usage by bits, where is the length of the longest string in . For real biological data, is generally small compared to the size of the entire input.
6.2 Naïve suffix comparisons using the Lyndon grammar
In this section, we describe a simple method for comparing and lexicographically which requires constant extra memory and works well in practice. We assume that holds for each rule . This is clearly the case for SLPs constructed with Algorithm 4 if we assign increasing indices to new symbols.
First note that, because we assign equal Lyndon words to equal symbols, if and only if . The key to our algorithm is that we find the longest common symbol . Additionally, we determine and with and . Note that, by definition, (otherwise, , contradicting the choice of ) and therefore the lexicographical order of and is the same as the lexicographical order of and . Formally, . If such a tuple does not exist, either and do not share a non-empty prefix (i.e., ), or one of and is a prefix of the other.
Note that the indices of the elements in have the same relative order as the lengths of the generated strings in the sense that for all we have if and only if . For this reason, we can proceed with a two-pointer search to find (and and ). More specifically, assuming that the desired exists, we have if for , and vice versa. A concrete implementation is omitted due to space constraints.
Because in each step, at least one of the symbol indices decreases, the time complexity for a comparison is at most linear in the size of the SLP. In fact, because in each step we go from a symbol to one of its children, the time complexity is actually bounded by the height of the SLP. This in turn implies a time complexity of when using Algorithm 4 with the described method for constructing the Lyndon SLP.
6.3 Construction in
In this Section, we describe an algorithm that is able to compute the Lyndon grammar online in deterministic time from right to left in a streaming fashion using words of extra memory.
Basically, this is done by maintaining the grammar’s set of symbols in an ordered sequence, lexicographically sorted by their respective generated strings. Using e.g. a B-Tree [5], one can then find the rank of any symbol in time. Thus, determining the lexicographical order of two symbols in can also be done in time. What remains to be shown is how the symbols can be maintained in this sorted arrangement.
As shown in Section 3, a lexicographically sorted Lyndon grammar has a forest-structure (cf. Figure 3). This first-symbol forest can be represented using a balanced parenthesis sequence (BPS) of length [24], which can be obtained using a depth-first traversal of the first-symbol forest (starting at the roots) by writing an opening parenthesis ‘(’ when visiting a node for the first time and a closing parenthesis ‘)’ when all subtrees of a node have been visited [24]. Each parenthesis pair corresponds to a symbol in the grammar, where the th opening parenthesis corresponds to the th smallest symbol (by lexicographical order). For a grammar with symbol set , let this BPS be . We represent as an ordered sequence , which contains two markers and for each symbol in , such that the ranks of and are the indices of ’s opening and closing parenthesis in , respectively. For instance, the sequence for the lexicographically sorted Lyndon grammar of our running example (see Figure 3) would be .
Now consider adding a new symbol , where and are in (i.e. , ). By Lemma 15, the parent of ’s parenthesis pair in must be ’s parenthesis pair. Therefore, it suffices to determine ’s lexicographically smallest “sibling” () with ; ’s parenthesis pair must appear immediately in front of in . If there is no such sibling, is (currently) the largest child of and thus ’s parenthesis pair must be immediately in front of instead. Note that comparing and is possible in because both and are in .
In order to be able to find the correct sibling of as described, we additionally maintain for each an ordered sequence containing the symbols in lexicographical order. Inserting into can then be accomplished with comparisons, each of which is possible in via .
In total, we obtain a time complexity of for maintaining the grammar’s symbols lexicographically sorted, and for constructing the Lyndon forest.
7 Experiments
The source code of our implementation is publicly available.888https://gitlab.com/qwerzuiop/lyndongrammar
We compare our algorithms for single texts with the programs Big-BWT 999https://gitlab.com/manzai/Big-BWT, last accessed: 22.04.2025, git hash 944cb27 [11] and r-pfbwt 101010https://github.com/marco-oliva/r-pfbwt, last accessed: 22.04.2025, git hash 1fea5c3 [41], as well as libsais.111111https://github.com/IlyaGrebnov/libsais, last accessed: 22.04.2025, git hash a138159 The latter uses a modified version of the Suffix-Array Induced Sorting (SAIS) algorithm [37] to compute the and, since it is based on the currently fastest construction implementation for general real-world data [40], can be viewed as a lower bound for algorithms using the suffix array to compute the .
For the of text collections, we compare with PFP-eBWT 121212https://github.com/davidecenzato/PFP-eBWT, last accessed: 22.04.2025, git hash 4ca75ce [10], r-pfbwt [41], ropebwt3 131313https://github.com/lh3/ropebwt3, last accessed: 22.04.2025, git hash 36a6411 [29], grlBWT 141414https://github.com/ddiazdom/grlBWT, last accessed: 22.04.2025, git hash f09e7fa [17] and CMS-BWT 151515https://github.com/fmasillo/cms-bwt, last accessed: 22.04.2025, git hash 1099d07. Note that the speed and memory usage of CMS-BWT has improved massively since its publication in [35]. [35] (for CMS-BWT we used the first sequence in the collection as reference). All tool except the last one support multi-threading. Note that not all of these tools compute the same variant [14]. Also note that all algorithms based on PFP as well as grlBWT are semi-external, i.e., write/read some temporary data to/from disk.
As test data, we use up to 1000 human Chromosome 19 haplotypes from [12] (bp) and SARS-CoV-2 sequences (bp).161616Downloaded from https://www.covid19dataportal.org on 28.08.2024. All experiments were conducted on a Linux-6.8.0 machine with an Intel Xeon Gold 6338 CPU and 512 GB of RAM. All programs were compiled with GCC 13.3.0. Before each test, the test file was scanned once to ensure it is cached by the kernel. The results can be seen in Figures 5, 6 and 7. The subscripts for the PFP-based algorithms indicate the used modulus.
For our programs, computing the using the suffix comparison method from Section 6.2 is generally the fastest. Computing the original is slightly slower because we need to find the smallest rotation of each input string. In the single-threaded case, for both the 1000 Chromosome 19 haplotypes and SARS-CoV-2 sequences, over of the time was spent constructing the grammar (for our fastest algorithm). Using the linear-time algorithm for constructing the Lyndon array from [7] to ensure expected linear time complexity slows our programs down by up to . As expected, the suffix comparison method from Section 6.3 is much slower than our other methods. The increase in memory consumption of our program in the multithreaded case is due to the use of thread-safe hash tables and multiple sequences and stacks residing in main memory.
For the Chromosome 19 collections and a single thread, CMS-BWT is the fastest program (at the cost of high memory usage), followed by r-pfbwt (for larger cases) and our algorithms. For more threads, our program is always the fastest. Regarding the memory consumption, grlBWT uses the least amount of main memory for single-threaded processing, followed by our programs. For the SARS-CoV-2 sequences, our program is the fastest, especially with multiple threads, and for more than one thread also the most memory efficient.
8 Conclusion and Further Work
We described an algorithm to compute the – and by extension the common $- and various versions of the – from the lexicographically sorted Lyndon grammar of a text or text collection. Furthermore, we gave an algorithm that lexicographically sorts a Lyndon grammar and discussed approaches to efficiently compute the Lyndon grammar of a text or text collection. We implemented the described algorithms and found that they outperform other current state-of-the art programs in terms of time or memory consumed (often both).
References
- [1] Golnaz Badkobeh, Maxime Crochemore, Jonas Ellert, and Cyril Nicaud. Back-to-front online lyndon forest construction. In Hideo Bannai and Jan Holub, editors, 33rd Annual Symposium on Combinatorial Pattern Matching, volume 223 of LIPIcs. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2022. doi:10.4230/LIPICS.CPM.2022.13.
- [2] Uwe Baier. Linear-time Suffix Sorting - A New Approach for Suffix Array Construction. In Roberto Grossi and Moshe Lewenstein, editors, 27th Annual Symposium on Combinatorial Pattern Matching, volume 54 of Leibniz International Proceedings in Informatics, Dagstuhl, Germany, 2016. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. doi:10.4230/LIPIcs.CPM.2016.23.
- [3] Hideo Bannai, Juha Kärkkäinen, Dominik Köppl, and Marcin Piątkowski. Constructing the Bijective and the Extended Burrows-Wheeler Transform in Linear Time. In Paweł Gawrychowski and Tatiana Starikovskaya, editors, 32nd Annual Symposium on Combinatorial Pattern Matching, volume 191 of Leibniz International Proceedings in Informatics. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2021. doi:10.4230/LIPIcs.CPM.2021.7.
- [4] Hélène Barcelo. On the action of the symmetric group on the free lie algebra and the partition lattice. Journal of Combinatorial Theory, Series A, 55(1):93–129, 1990. doi:10.1016/0097-3165(90)90050-7.
- [5] Rudolf Bayer and Edward McCreight. Organization and maintenance of large ordered indices. In Proceedings of the 1970 ACM SIGFIDET (Now SIGMOD) Workshop on Data Description, Access and Control, pages 107–141, 1970. doi:10.1145/1734663.1734671.
- [6] Nico Bertram, Jonas Ellert, and Johannes Fischer. Lyndon Words Accelerate Suffix Sorting. In Petra Mutzel, Rasmus Pagh, and Grzegorz Herman, editors, 29th Annual European Symposium on Algorithms, volume 204 of Leibniz International Proceedings in Informatics. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2021. doi:10.4230/LIPIcs.ESA.2021.15.
- [7] Philip Bille, Jonas Ellert, Johannes Fischer, Inge Li Gørtz, Florian Kurpicz, J. Ian Munro, and Eva Rotenberg. Space Efficient Construction of Lyndon Arrays in Linear Time. In Artur Czumaj, Anuj Dawar, and Emanuela Merelli, editors, 47th International Colloquium on Automata, Languages, and Programming, volume 168 of Leibniz International Proceedings in Informatics. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2020. doi:10.4230/LIPIcs.ICALP.2020.14.
- [8] Silvia Bonomo, Sabrina Mantaci, Antonio Restivo, Giovanna Rosone, and Marinella Sciortino. Suffixes, Conjugates and Lyndon Words. In Marie-Pierre Béal and Olivier Carton, editors, Developments in Language Theory, pages 131–142, Berlin, Heidelberg, 2013. Springer Berlin Heidelberg. doi:10.1007/978-3-642-38771-5_13.
- [9] Silvia Bonomo, Sabrina Mantaci, Antonio Restivo, Giovanna Rosone, and Marinella Sciortino. Sorting conjugates and suffixes of words in a multiset. International Journal of Foundations of Computer Science, 25(08):1161–1175, 2014. doi:10.1142/S0129054114400309.
- [10] Christina Boucher, Davide Cenzato, Zsuzsanna Lipták, Massimiliano Rossi, and Marinella Sciortino. Computing the original eBWT faster, simpler, and with less memory. In International Symposium on String Processing and Information Retrieval, pages 129–142. Springer, 2021. doi:10.1007/978-3-030-86692-1_11.
- [11] Christina Boucher, Travis Gagie, Alan Kuhnle, Ben Langmead, Giovanni Manzini, and Taher Mun. Prefix-free parsing for building big BWTs. Algorithms for Molecular Biology, 14:1–15, 2019. doi:10.1186/s13015-019-0148-5.
- [12] Christina Boucher, Travis Gagie, I Tomohiro, Dominik Köppl, Ben Langmead, Giovanni Manzini, Gonzalo Navarro, Alejandro Pacheco, and Massimiliano Rossi. PHONI: Streamed matching statistics with multi-genome references. In 2021 Data Compression Conference, pages 193–202. IEEE, 2021. doi:10.1109/dcc50243.2021.00027.
- [13] Michael Burrows and David Wheeler. A block-sorting lossless data compression algorithm. Digital SRC Research Report, 124, 1994.
- [14] Davide Cenzato and Zsuzsanna Lipták. A survey of BWT variants for string collections. Bioinformatics, 40(7):btae333, 2024. doi:10.1093/bioinformatics/btae333.
- [15] Kuo Tsai Chen, Ralph H. Fox, and Roger C. Lyndon. Free differential calculus, iv. the quotient groups of the lower central series. Annals of Mathematics, pages 81–95, 1958. doi:10.2307/1970044.
- [16] Maxime Crochemore and Luís M.S. Russo. Cartesian and Lyndon trees. Theoretical Computer Science, 806, 2020. doi:10.1016/j.tcs.2018.08.011.
- [17] Diego Díaz-Domínguez and Gonzalo Navarro. Efficient construction of the BWT for repetitive text using string compression. Information and Computation, 294:105088, 2023. doi:10.1016/j.ic.2023.105088.
- [18] Jean-Pierre Duval. Factorizing words over an ordered alphabet. Journal of Algorithms, 4(4):363–381, 1983. doi:10.1016/0196-6774(83)90017-2.
- [19] Jonas Ellert. Lyndon Arrays Simplified. In Shiri Chechik, Gonzalo Navarro, Eva Rotenberg, and Grzegorz Herman, editors, 30th Annual European Symposium on Algorithms, volume 244 of Leibniz International Proceedings in Informatics, pages 48:1–48:14, Dagstuhl, Germany, 2022. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. doi:10.4230/LIPIcs.ESA.2022.48.
- [20] Paolo Ferragina and Giovanni Manzini. Indexing compressed text. Journal of the ACM (JACM), 52(4):552–581, 2005. doi:10.1145/1082036.1082039.
- [21] Frantisek Franek, ASM Sohidull Islam, M Sohel Rahman, and William F Smyth. Algorithms to Compute the Lyndon Array. In Jan Holub and Jan Žďárek, editors, Prague Stringology Conference, pages 172–184, 2016. URL: http://www.stringology.org/event/2016/p15.html.
- [22] Travis Gagie, Gonzalo Navarro, and Nicola Prezza. Optimal-time text indexing in BWT-runs bounded space. In Proceedings of the Twenty-Ninth Annual ACM-SIAM Symposium on Discrete Algorithms, pages 1459–1477. SIAM, 2018. doi:10.1137/1.9781611975031.96.
- [23] Joseph Yossi Gil and David Allen Scott. A bijective string sorting transform, 2012. doi:10.48550/arXiv.1201.3077.
- [24] Guy Jacobson. Space-efficient static trees and graphs. In 30th Annual Symposium on Foundations of Computer Science, pages 549–554, Los Alamitos, CA, USA, 1989. IEEE Computer Society. doi:10.1109/sfcs.1989.63533.
- [25] Tsuruta Kazuya, Köppl Dominik, Nakashima Yuto, Inenaga Shunsuke, Bannai Hideo, Takeda Masayuki, Tsuruta Kazuya, Köppl Dominik, Nakashima Yuto, Inenaga Shunsuke, Bannai Hideo, and Takeda Masayuki. Grammar-compressed Self-index with Lyndon Words. Information Processing Society of Japan, 13:84–92, 2020. URL: https://ipsj.ixsq.nii.ac.jp/records/206700.
- [26] Dominik Kempa and Nicola Prezza. At the roots of dictionary compression: string attractors. In Proceedings of the 50th Annual ACM SIGACT Symposium on Theory of Computing, Stoc 2018, pages 827–840, New York, NY, USA, 2018. Association for Computing Machinery. doi:10.1145/3188745.3188814.
- [27] Manfred Kufleitner. On bijective variants of the burrows-wheeler transform. In Jan Holub and Jan Žďárek, editors, Prague Stringology Conference, pages 65–79, Czech Technical University in Prague, Czech Republic, 2009. URL: http://www.stringology.org/event/2009/p07.html.
- [28] Ben Langmead and Steven L Salzberg. Fast gapped-read alignment with Bowtie 2. Nature methods, 9(4):357–359, 2012. doi:10.1038/nmeth.1923.
- [29] Heng Li. BWT construction and search at the terabase scale. Bioinformatics, 40(12):btae717, 2024. doi:10.1093/bioinformatics/btae717.
- [30] Heng Li and Richard Durbin. Fast and accurate short read alignment with Burrows–Wheeler transform. bioinformatics, 25(14):1754–1760, 2009. doi:10.1093/bioinformatics/btp324.
- [31] Felipe A. Louza, Sabrina Mantaci, Giovanni Manzini, Marinella Sciortino, and Guilherme P. Telles. Inducing the Lyndon Array. In Nieves R. Brisaboa and Simon J. Puglisi, editors, String Processing and Information Retrieval, pages 138–151, Cham, 2019. Springer International Publishing. doi:10.1007/978-3-030-32686-9_10.
- [32] Sabrina Mantaci, Antonio Restivo, G. Rosone, and Marinella Sciortino. An Extension of the Burrows Wheeler Transform and Applications to Sequence Comparison and Data Compression. In Alberto Apostolico, Maxime Crochemore, and Kunsoo Park, editors, 16th Annual Symposium on Combinatorial Pattern Matching, pages 178–189. Springer, 2005. doi:10.1007/11496656_16.
- [33] Sabrina Mantaci, Antonio Restivo, Giovanna Rosone, and Marinella Sciortino. Sorting Suffixes of a Text via its Lyndon Factorization. In Jan Holub and Jan Zdárek, editors, Prague Stringology Conference, pages 119–127, 2013. URL: http://www.stringology.org/event/2013/p11.html.
- [34] Sabrina Mantaci, Antonio Restivo, Giovanna Rosone, and Marinella Sciortino. Suffix array and Lyndon factorization of a text. Journal of Discrete Algorithms, 28:2–8, 2014. doi:10.1016/j.jda.2014.06.001.
- [35] Francesco Masillo. Matching Statistics Speed up BWT Construction. In Inge Li Gørtz, Martin Farach-Colton, Simon J. Puglisi, and Grzegorz Herman, editors, 31st Annual European Symposium on Algorithms, volume 274 of Leibniz International Proceedings in Informatics, pages 83:1–83:15, Dagstuhl, Germany, 2023. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. doi:10.4230/LIPIcs.ESA.2023.83.
- [36] Lucas Mercier and Philippe Chassaing. The height of the Lyndon tree. Discrete Mathematics & Theoretical Computer Science, DMTCS Proceedings vol. AS, 25th International Conference on Formal Power Series and Algebraic Combinatorics (FPSAC 2013), January 2013. doi:10.46298/dmtcs.2357.
- [37] Ge Nong, Sen Zhang, and Wai Hong Chan. Linear Suffix Array Construction by Almost Pure Induced-Sorting. In 2009 Data Compression Conference, pages 193–202, 2009. doi:10.1109/dcc.2009.42.
- [38] Jannik Olbrich. lg. Software, swhId: swh:1:dir:0274cae4abb893b49bc035ef3a57986a418afe40 (visited on 2025-09-04). URL: https://gitlab.com/qwerzuiop/lyndongrammar, doi:10.4230/artifacts.24673.
- [39] Jannik Olbrich, Enno Ohlebusch, and Thomas Büchler. On the Optimisation of the GSACA Suffix Array Construction Algorithm. In Diego Arroyuelo and Barbara Poblete, editors, String Processing and Information Retrieval, pages 99–113, Cham, 2022. Springer International Publishing. doi:10.1007/978-3-031-20643-6_8.
- [40] Jannik Olbrich, Enno Ohlebusch, and Thomas Büchler. Generic Non-recursive Suffix Array Construction. ACM Transactions on Algorithms, 20(2):18, 2024. doi:10.1145/3641854.
- [41] Marco Oliva, Travis Gagie, and Christina Boucher. Recursive prefix-free parsing for building big BWTs. In 2023 data compression conference, pages 62–70. IEEE, 2023. doi:10.1109/DCC55655.2023.00014.
- [42] Yoshimasa Takabatake, Tomohiro I, and Hiroshi Sakamoto. A Space-Optimal Grammar Compression. In Kirk Pruhs and Christian Sohler, editors, 25th Annual European Symposium on Algorithms, volume 87 of Leibniz International Proceedings in Informatics (LIPIcs), pages 67:1–67:15, Dagstuhl, Germany, 2017. Schloss Dagstuhl – Leibniz-Zentrum für Informatik. doi:10.4230/LIPIcs.ESA.2017.67.
