Abstract

We present a really simple linear-time algorithm constructing a context-free grammar of size \(\mathcal{O}(g log (N/g))\) for the input string, where N is the size of the input string and g the size of the optimal grammar generating this string. The algorithm works for arbitrary size alphabets, but the running time is linear when the alphabet Σ of the input string can be identified with numbers from {1,…, N }. Algorithms with such an approximation guarantee and running time are known, however all of them were non-trivial and their analyses involved. The here presented algorithm computes the LZ77 factorisation (of size l) and transforms it in phases to a grammar. In each phase it maintains an LZ77-like factorisation of the word with at most l factors as well as additional \(\mathcal{O}(l)\) letters. In one phase in a greedy way (by a left-to-right sweep) we choose a set of pairs of consecutive letters to be replaced with new symbols, i.e. nonterminals of the constructed grammar. We choose at least 2/3 of the letters in the word and there are \(\mathcal{O}(l)\) many different pairs among them. Hence there are \(\mathcal{O}(log N)\) phases, each introduces \(\mathcal{O}(l)\) nonterminals. A more precise analysis yields a bound \(\mathcal{O}(l log(N/l))\). As l ≤ g, this yields \(\mathcal{O}(g log(N/g))\).

Talk to us

Join us for a 30 min session where you can share your feedback and ask us any queries you have

Schedule a call

Disclaimer: All third-party content on this website/platform is and will remain the property of their respective owners and is provided on "as is" basis without any warranties, express or implied. Use of third-party content does not indicate any affiliation, sponsorship with or endorsement by them. Any references to third-party content is to identify the corresponding services and shall be considered fair use under The CopyrightLaw.