Optimal Regular Expressions for Permutations
Antonio Molina Lovett, Jeffrey Shallit

TL;DR
This paper constructs an optimal regular expression for permutation languages, providing explicit bounds on its size and proving its minimality among all such expressions.
Contribution
It introduces a divide-and-conquer method to explicitly construct and prove the minimal size of regular expressions for permutation languages.
Findings
Constructed a regular expression $R_n$ for permutation language $P_n$
Derived explicit bounds on the size of $R_n$
Proved $R_n$ has minimal size among all regular expressions for $P_n$
Abstract
The permutation language consists of all words that are permutations of a fixed alphabet of size . Using divide-and-conquer, we construct a regular expression that specifies . We then give explicit bounds for the length of , which we find to be , and use these bounds to show that has minimum size over all regular expressions specifying .
| 1 | 1 |
| 2 | 4 |
| 3 | 15 |
| 4 | 48 |
| 5 | 190 |
| 6 | 600 |
| 7 | 2205 |
| 8 | 6720 |
| 9 | 29988 |
| 10 | 95760 |
Peer Reviews
No public reviews on file for this paper yet. If you reviewed it on a platform where reviews are public (OpenReview, ICLR, NeurIPS, ICML), you can paste yours below so the community can read it here.
Videos
No videos yet. Explain this paper in a talk, walkthrough, or lecture? Add one.
Optimal Regular Expressions for Permutations
Antonio Molina Lovett and Jeffrey Shallit
School of Computer Science
University of Waterloo
Waterloo, ON N2L 3G1
Canada
Abstract
The permutation language consists of all words that are permutations of a fixed alphabet of size . Using divide-and-conquer, we construct a regular expression that specifies . We then give explicit bounds for the length of , which we find to be , and use these bounds to show that has minimum size over all regular expressions specifying .
1 Introduction
Given a regular language defined in some way, it is a challenging problem to find good upper and lower bounds on the size of the smallest regular expression specifying . (In this paper, by a regular expression, we always mean one using the operations of union, concatenation, and Kleene closure only.) Indeed, as a computational problem, it is known that determining the shortest regular expression corresponding to an NFA is PSPACE-hard [10]. Jiang and Ravikumar proved the analogous result for DFAs [9]. For more recent results on inapproximability, see [7].
For nontrivial families of languages, only a handful of results are already known. For example, Ellul et al. [5] showed that the shortest regular expression for the language is of length . Here denotes the number of occurrences of the symbol in the word . (A simple divide-and-conquer strategy provides a matching upper bound.) Chistikov et al. [3] showed that the regular language
[TABLE]
can be specified by a regular expression of size exactly , and furthermore this bound is optimal. Mousavi [11] developed a general program for computing lower bounds on regular expression size for the binomial languages
[TABLE]
Let be a positive integer, and define . In this paper we study the finite language consisting of all permutations of . Thus, for example,
[TABLE]
We are interested in regular expressions that specify . In counting the length of regular expressions, we adopt the conventional measure of alphabetic length (see, for example, [4]): the length of a regular expression is the number of occurrences of symbols of the alphabet . Thus, other symbols, such as parentheses and +, are ignored.
A brute-force solution, which consists of listing all the members of and separating them by the union symbol +, evidently gives a regular expression for of alphabetic length . This can be improved to by tail recursion, where represents a regular expression for all permutations of the symbols of :
[TABLE]
For example, for this gives
1(2(34+43)+3(24+42)+4(23+32))+2(1(34+43)+3(14+41)+4(13+31))+
3(1(24+42)+2(14+41)+4(12+21))+4(1(23+32)+2(13+31)+3(12+21)).
Can we do better?
Ellul et al. [5] proved the following weak lower bound: every regular expression for has alphabetic length at least . In this note we derive an upper bound through divide-and-conquer. We then show that the regular expression this strategy produces is, in fact, actually optimal. This improves the result from [5]. The language is of particular interest because its complement has short regular expressions, as shown in [5]. For other results concerning context-free grammars for , see [5, 1, 2, 6].
2 Divide-and-conquer
Consider the following divide-and-conquer strategy. Let be an alphabet of cardinality . We consider all subsets of cardinality . For each subset we recursively determine a regular expression for the permutations of , a regular expression for the permutations of , and concatenate them together. This gives
[TABLE]
Finally, we define .
Thus, for example, we get
(12+21)(34+43)+(13+31)(24+42)+(23+32)(14+41)+
(14+41)(23+32)+(24+42)(13+31)+(34+43)(12+21)
for .
The alphabetic length of the resulting regular expression for all permutations of is then , where
[TABLE]
The first few values of are given in the table below.
It is sequence A320460 in the On-Line Encyclopedia of Integer Sequences [13].
It seems hard to determine a simple closed-form expression for . Nevertheless, we can roughly estimate it as follows, at least when is a power of :
[TABLE]
Substituting the Stirling approximation and simplifying, we get that is roughly equal to
[TABLE]
To make this precise, and make it work when is not a power of , however, takes more work.
The rest of the paper is organized as follows: in Section 3, we prove that our regular expression is in fact optimal, assuming one result that is proven at the end of the paper. In Section 4, we establish some inequalities related to Stirling’s formula. In Section 5, we connect these inequalities to and obtain the estimate mentioned in the abstract. Finally, in Section 6 we use our obtained bounds on to provide the missing piece in our optimality proof.
3 Optimality
In order to show that our regular expression has minimum possible length, we use the following property of that we prove in Section 6:
Lemma 1**.**
If , then every integer satisfies . Equality occurs if and only if or .
For and , define to be the minimum alphabetic length of a regular expression specifying a subset of , where the subset has cardinality at least .
Lemma 2**.**
If and , then .
Proof.
We prove this by induction over the lexicographical ordering of pairs . This is easy for our base case , as the best regular expression is a single character. We thus suppose .
Consider a regular expression for a subset of of cardinality at least that has minimum alphabetic length. Clearly no such expression will involve or . We now consider the possibilities for the last (outermost) operation in the regular expression. Clearly the only relevant possibilities are union and concatenation.
If the last operation is a union, then it is the union of two subsets of of cardinalities where , and by minimality. Then we get
[TABLE]
If the last operation is a concatenation, then it is the concatenation of two regular expressions for subsets of and of cardinalities and respectively (possibly after changing alphabets) where and . By minimality, we have all positive, so . We now obtain
[TABLE]
In both cases, we get the desired inequalities for these choices of and , completing our induction. ∎
Theorem 3**.**
Let . Over all regular expressions for the permutation language , the regular expression given by our divide-and-conquer strategy achieves the minimum alphabetic length.
Proof.
By our construction from Section 2, the regular expression specifies the entirety of and has alphabetic length . We thus get the upper bound . By Lemma 2, we have the matching lower bound . Thus, has minimum possible alphabetic length for a regular expression specifying . ∎
4 Analysis
In what follows we use to denote the natural logarithm, and to denote logarithms to the base .
Define to be the usual Stirling approximation [12]:
[TABLE]
Lemma 4**.**
For every , we have the bounds
[TABLE]
Proof.
The first two derivatives of are
[TABLE]
and so we see that is convex (that is, its derivative is increasing) for all . Thus, by Jensen’s inequality [8] and exponentiating, we obtain the lower bound
[TABLE]
for all .
Now, using the mean value theorem twice, we get that
[TABLE]
where
[TABLE]
Adding the inequalities (2) and (3), we get
[TABLE]
This gives us the inequality
[TABLE]
for all . ∎
Next, for , define the function by
[TABLE]
Our goal is to show that can be approximated by for some choice of .
Lemma 5**.**
Let . Then for every we have
[TABLE]
Proof.
We again compute the logarithmic derivative:
[TABLE]
For , this derivative is at most , so by the mean value theorem,
[TABLE]
Exponentiating, we get
[TABLE]
Next, we note that the derivative of exceeds that of for and is less for . So, since , we have for all . Hence
[TABLE]
for all . Thus, by the mean value theorem and exponentiating again, we get
[TABLE]
We can now combine the inequalities (4) and (5) for to get
[TABLE]
which gives us both desired bounds. ∎
We now show an identity relating and .
Lemma 6**.**
Suppose and . If , then
[TABLE]
Proof.
We have
[TABLE]
5 Bounds on
In this section we obtain an estimate for , the size of the optimal regular expression for .
Theorem 7**.**
For all we have
[TABLE]
Further, when is a power of two, we get the following upper bound, matching the general lower bound.
[TABLE]
Proof.
Recall the Stirling approximation
[TABLE]
see [12]. Now suppose that and , where , for some non-decreasing function . Then by combining Lemma 4, Lemma 5, and equation (6), we get
[TABLE]
and
[TABLE]
For the case where is a power of two only, we use and , we set
[TABLE]
so and . Now Lemma 6 gives us the identity . Then by induction we have for all , where is any constant that satisfies this bound for . In particular, works, so we have for all that are powers of two.
Next, for general , we use and , we set
[TABLE]
so and . Now Lemma 6 gives us the identity . For , we get
[TABLE]
Easily, we also get . Thus, by induction we have for all , where is chosen to make this work for . In particular, works again. Further, since we have for all , we also have for all . Finally, we check manually that this last inequality holds for too, and thus for all .
All that remains is the lower bound. We get similar recurrences, supposing and , where for some non-increasing . Then by a similar argument as for the upper bounds, we have
[TABLE]
and
[TABLE]
This time, we set with (indeed non-increasing), and , noting . Now for , we have . Since the right-hand side of this inequality is non-increasing in , the bound holds for all . This implies
[TABLE]
Further, , so by induction we have for all , where is chosen to satisfy this bound for . In particular, works. Since for all , we also have for all . Finally, we check manually that this works for all too, and thus for all . ∎
6 Optimality revisited
We now give a simple lower bound on the growth of .
Lemma 8**.**
We have for all .
Proof.
We prove this by induction on . It is easy to verify the base case . Otherwise . Suppose the desired inequality holds for all smaller values of . If is odd, then let satisfy . Then
[TABLE]
Otherwise, if is even, then let satisfy . We note that , so . We then have
[TABLE]
Armed with this inequality and the bounds given by Theorem 7 of Section 5, we are ready to complete our proof of the optimality of . We recall Lemma 1, which is what we have left to show: See 1
Proof.
We easily check the cases by hand. Let be arbitrary. It suffices to consider the cases where , as those where are symmetric. Equality for the case is given by the definition of .
Suppose that . Then , so . Hence we get
[TABLE]
and so
[TABLE]
Also, from we get , and so
[TABLE]
Then
[TABLE]
Next, suppose . Then and so . Then if ,
[TABLE]
We note also that , so we have for all . We note from our proof of Lemma 5 that the derivative of is at most for . In particular, for , this derivative is at most for all , and so here (as ). Thus
[TABLE]
The reference list from the paper itself. Each links out to its DOI / PubMed record.
- 1[1] P. R. J. Asveld, Generating all permutations by context-free grammars in Chomsky normal form, Theoret. Comput. Sci. 354 (2006), 118–130.
- 2[2] P. R. J. Asveld, Generating all permutations by context-free grammars in Greibach normal form, Theoret. Comput. Sci. 409 (2008), 565–577.
- 3[3] D. Chistikov, S. Ivan, A. Lubiw, and J. Shallit, Fractional coverings, greedy coverings, and rectifier networks. In H. Vollmer and B. Vallée, editors, 34th Symposium on Theoretical Aspects of Computer Science (STACS 2017) , Vol. 66 of LIP Ics , pp. 23:1–23:14. Schloss Dagstuhl — Leibniz-Zentrum für Informatik, 2017.
- 4[4] A. Ehrenfeucht and P. Zeiger, Complexity measures for regular expressions, J. Comput. System Sci. 12 (1976), 134–146.
- 5[5] K. Ellul, B. Krawetz, J. Shallit, and M.-w. Wang, Regular expressions: new results and open problems, J. Autom. Lang. Combin. 10 (2005), 407–437.
- 6[6] Y. Filmus, Lower bounds for context-free grammars, Info. Proc. Letters 111 (2011), 895–898.
- 7[7] G. Gramlich and G. Schnitger, Minimizing nfa’s and regular expressions, J. Comput. System Sci. 73 (2007), 908–923.
- 8[8] J. L. W. V. Jensen, Sur les fonctions convexes et les inégalités entre les valeurs moyennes, Acta Math. 30 (1906), 175–193.
