generating all circular shifts by context-free grammars in chomsky

0 downloads 0 Views 206KB Size Report
Nov 21, 2006 - Keywords: context-free grammar, Chomsky normal form, ... In [2] we considered context-free grammars Gn (n ≥ 1) in Chomsky normal form.
Journal of Automata, Languages and Combinatorics 11 (2006) 2, 147–159 c Otto-von-Guericke-Universit¨

at Magdeburg

GENERATING ALL CIRCULAR SHIFTS BY CONTEXT-FREE GRAMMARS IN CHOMSKY NORMAL FORM

Peter R. J. Asveld Department of Computer Science, Twente University of Technology P.O. Box 217, 7500 AE Enschede, the Netherlands e-mail: [email protected]

ABSTRACT Let {a1 , a2 , . . . , an } be an alphabet of n symbols and let Cn be the language of circular or cyclic shifts of the word a1 a2 · · · an ; so Cn = {a1 a2 · · · an−1 an , a2 a3 · · · an a1 , . . . , an a1 · · · an−2 an−1 }. We discuss a few families of context-free grammars Gn (n ≥ 1) in Chomsky normal form such that Gn generates Cn . The grammars in these families are investigated with respect to their descriptional complexity, i.e., we determine the number of nonterminal symbols ν(n) and the number of rules π(n) of Gn as functions of n. These ν and π happen to be functions bounded by low-degree polynomials, particularly when we focus our attention to unambiguous grammars. Finally, we introduce a family of minimal unambiguous grammars for which ν and π are linear. Keywords: context-free grammar, Chomsky normal form, permutation, circular shift, cyclic shift, descriptional complexity, unambiguous grammar.

1. Introduction In [2] we considered context-free grammars Gn (n ≥ 1) in Chomsky normal form that generate the finite language Ln consisting of all permutations over the alphabet Σn = {a1 , a2 , . . . , an }. A few families {Gn }n≥1 of such grammars for {Ln }n≥1 have been studied in [2] with respect to their grammatical complexity; more precisely, the number of nonterminal symbols ν(n) and the number of production rules π(n) of Gn have been determined as functions of n. In this paper we restrict ourselves to a subset of specific permutations over Σn , viz. to the so-called circular or cyclic shifts. Assuming a linear order on Σn , e.g., a1 < a2 < · · · < an , the set Cn of circular shifts (also called cyclic shifts) over Σn is Cn = {a1 a2 · · · an−1 an , a2 a3 · · · an a1 , a3 a4 · · · a1 a2 , . . . , an a1 · · · an−2 an−1 }. So Cn is obtained from the word a1 a2 · · · an by repetitively moving the first symbol to the right-hand end of the string. Clearly, Cn contains n strings, which is much less than the n! elements of Ln [7, 12]. Considered as a set of permutations of a1 a2 · · · an the set Cn forms a cyclic subgroup of order n of the symmetric group Sn over Σn

148

P. R. J. ASVELD

(i. e., the group of all permutations over the alphabet Σn ). And there are (n − 1)! of such isomorphic subgroups of order n of Sn , each of which is induced by a linear order a1 < ap(2) < ap(3) < · · · < ap(n) on Σn , where p is a permutation of 2, 3, . . . , n; cf. also the notion of circular successor in Section 2 below, and §6.1 in [7]. As in [2] we will discuss some families of grammars {Gn }n≥1 in Chomsky normal form for {Cn }n≥1 and investigate the descriptional complexity of these Gn ’s in terms of the complexity measures ν(n) and π(n). Contrary to the families in [2] where the complexity measures ν(n) and π(n) were bounded by exponential functions, in this paper these measures are bounded by polynomial functions of low degree. The structure of this paper resembles the one of [2] to some extent. So firstly, in Section 2 we consider some preliminaries on notation and terminology. Section 3 is devoted to some elementary properties of grammars Gn in Chomsky normal form that generate Cn . Section 4 consists of a direct approach based on the set of (circularly) ordered strings over the terminal alphabet Σn of Gn . In Section 5 we consider a slight modification of this family which results in a family of context-free grammars in Chomsky normal form with less productions than the ones in Section 4 (provided n ≥ 3). Section 6 is devoted to unambiguous grammars (in Chomsky normal form) for C n ; these grammars turn out to be rather simple in terms of the descriptional complexity measures ν(n) and π(n). In Section 7 we consider a family of unambiguous grammars in Chomsky normal form that is minimal – with respect to the descriptional complexity measures ν(n) and π(n) – in an obvious sense. For this minimal family of grammars, these complexity measures turn out to be linear functions in n. Finally, Section 8 contains some concluding remarks. 2. Preliminaries We assume the reader to be familiar with the rudiments of discrete mathematics, especially with combinatorics; for standard texts on these subjects we refer to [7, 12]. For a finite set X, we denote the cardinality (the number of elements) of X by #X. Basic concepts, terminology and notation from formal language theory can be found in e. g. [9]. We will denote the empty word by λ. As usual, the length of the word x will be denoted by |x|. For each word w over Σ, we denote by A(w) the set of all symbols from Σ that really do occur in w. Formally, A(λ) = ∅, and A(ax) = {a} ∪ A(x) for each Sa ∈ Σ and x ∈ Σ⋆ . This mapping is extended to languages L over Σ by A(L) = w∈L A(w). Recall that a λ-free context-free grammar G = (V, Σ, P, S) is in Chomsky normal form if P ⊆ N ×(N −{S})2 ∪N ×Σ where N = V −Σ. For each context-free grammar G = (V, Σ, P, S), let L(G, A) be the language over Σ defined by L(G, A) = {w ∈ Σ⋆ | A ⇒⋆ w}. Thus for the language L(G) generated by G, we have L(G) = L(G, S). Note that, if G is in Chomsky normal form, then L(G, A) is a nonempty language for each A in N . Throughout this paper, we use Σn to denote an alphabet of n different symbols a1 , a2 , . . . , an (n ≥ 1) provided with the linear order a1 < a2 < · · · < an .

Generating All Circular Shifts by Context-Free Grammars in CNF

149

In [2] subsets of Σn and the power set of Σn played a principal part in generating all permutations by context-free grammars in Chomsky normal form. However, in generating all circular shifts we need (sets of) subwords of a1 a2 . . . an a1 . . . an−1 . Intuitively, Fkn is the set of all subwords of length k (1 ≤ k ≤ n) that obey the circular succession relation ≺ on Σn defined by: ai ≺ aj if and only if either (i) i < n and j = i + 1 or (ii) i = n and j = 1. Note that ≺ is not a transitive relation; as its name suggests it is rather a kind of successor relation. Returning to the definition of F kn , we formally define for each k (1 ≤ k ≤ n), the set Fkn by Fkn = {x ∈ Σ⋆n | ∃u, v ∈ Σ⋆n : uxv = a1 a2 . . . an a1 a2 . . . ak−1 ; |x| = k} and the partial unions Qnm by Qnm =

m [

Fkn

(1 ≤ m ≤ n).

k=1

Then we obviously have Cn = Fnn = Qnn − Qnn−1 , #Fkn = n (1 ≤ k ≤ n), #Qnm = mn and #Cn = #Fnn = n; note that #Qnn − #Qnn−1 = n2 − (n − 1)n = n as well. Example 2.1 For n = 3, we have Σ3 = {a1 , a2 , a3 }, F13 = Σ3 , F23 = {a1 a2 , a2 a3 , a3 a1 } and F33 = C3 = {a1 a2 a3 , a2 a3 a1 , a3 a1 a2 }. Clearly, L3 − C3 = {a1 a3 a2 , a2 a1 a3 , a3 a2 a1 }, where L3 is the language of all permutations over Σ3 . 2 Finally, we remark that Cn can also be characterized in terms of the circular closure operator c on languages which is defined by c(L) = {w2 w1 | w1 w2 ∈ L} for each language L [6]. Then Cn can be written as Cn = c({a1 a2 . . . an }). 3. Elementary Properties In this section we first recall some properties of grammars that generate Ln (the finite language of all permutations over Σn ). From [2] we quote the following results. Proposition 3.1 For n ≥ 1, let Gn = (Vn , Σn , Pn , Sn ) be a context-free grammar in Chomsky normal form that generates Ln , and let Nn be defined by Nn = Vn − Σn . (1) For each A in Nn , the language L(Gn , A) is a nonempty subset of an isomorphic copy Mk of the language Lk for some k (1 ≤ k ≤ n). Consequently, each string z in L(Gn , A) has length k, z consists of k different symbols, and A(z) = A(L(Gn , A)). (2) Let A and B be nonterminal symbols in Nn . If L(Gn , A) ∩ L(Gn , B) 6= ∅, then A(L(Gn , A)) = A(L(Gn , B)). (3) If A → BC is a rule in Gn , then A(L(Gn , B)) ∩ A(L(Gn , C)) = ∅ and A(L(Gn , B)) ∪ A(L(Gn , C)) = A(L(Gn , A)). 2 Proposition 3.1 (2) suggested the following equivalence relation on Nn : A and B from Nn are called equivalent if |x| = |y| for some x ∈ L(Gn , A) and some y ∈ L(Gn , B). The corresponding equivalence classes are denoted by {En,k }nk=1 . Thus

150

P. R. J. ASVELD

Nn is the union of the n equivalence classes En,1 , En,2 , . . . , En,n with En,i ∩ En,j = ∅ for i 6= j. The number of elements #En,k of the equivalence class En,k will be denoted by D(n, k) (1 ≤ k ≤ n) in the sequel. Example 3.2 Consider Σ3 = {a1 , a2 , a3 } and the grammar GP 3 = (V3 , Σ3 , P3 , A123 ) with N3 = {A123 , A12 , A13 , A23 , A1 , A2 , A3 } and P3 consists of A123 → A1 A23 | A2 A13 | A3 A12 , A12 → A1 A2 | A2 A1 , A13 → A1 A3 | A3 A1 , A23 → A2 A3 | A3 A2 , A1 → a1 , A2 → a2 and A3 → a3 . Then L(GP 3 ) = L3 , E3,3 = {A123 }, E3,2 = {A12 , A13 , A23 }, E3,1 = {A1 , A2 , A3 }, and hence D(3, 3) = 1 and D(3, 2) = D(3, 1) = 3. 2 In the proof of Proposition 3.1 we only used the fact that each word in L(Gn ) is a permutation [2]. So if we require that Gn generates Cn rather than Ln , Proposition 3.1 still applies. Next we will see what happens when we restrict our attention to circular shifts rather than to arbitrary permutations over Σn ; cf. Proposition 3.4 for which we need the following auxiliary result. Lemma 3.3 Let X be a nonempty proper subalphabet of Σn with #X = l (1 ≤ l < n). Then there exists at most one word x with A(x) = X such that x satisfies the circular succession relation. And if X = {b1 , b2 , . . . , bl }, then x = bp(1) bp(2) . . . bp(l) provided there exists a permutation p of {1, 2, . . . , l} such that bp(1) ≺ bp(2) ≺ · · · ≺ bp(l) . Proof. If X = {b}, then x = b and the statement holds trivially. When l ≥ 2 we check whether there is a permutation p of {1, 2, . . . , l} such that bp(1) ≺ bp(2) ≺ · · · ≺ bp(l) . Then x = bp(1) bp(2) . . . bp(l) is the only word that satisfies the circular succession relation, since l < n. If such a p does not exist, then there is no such x. 2 Proposition 3.4 Let Gn = (Vn , Σn , Pn , Sn ) be a context-free grammar in Chomsky normal form that generates Cn , and let Nn be defined by Nn = Vn − Σn . (1) For each A in Nn − {Sn }, the language L(Gn , A) is a singleton. (2) Let A and B be nonterminal symbols in Nn −{Sn }. If L(Gn , A)∩L(Gn , B) 6= ∅, then L(Gn , A) = L(Gn , B). (3) If A → BC is in Pn , with L(Gn , B) = {b1 b2 . . . bk } and L(Gn , C) = {c1 c2 . . . cl }, then bk ≺ c1 . Hence, if L(Gn , B) ⊆ Fkn and L(Gn , C) ⊆ Fln , then L(Gn , A) ⊆ n Fk+l . Proof. (1) Since Gn is in Chomsky normal form, each nonterminal A is useful: so there is a derivation Sn ⇒⋆ ϕAψ ⇒+ ϕxA ψ ⇒+ x where x is a circular shift, ϕψ 6= λ, A(x) = Σn , and 1 ≤ #A(xA ) < n. Hence, by Lemma 3.3, L(Gn , A) contains at most one word over Σn , and as L(Gn , A) is nonempty, L(Gn , A) is a singleton. (2) Since L(Gn , A) and L(Gn , B) are singletons by (1), L(Gn , A) ∩ L(Gn , B) 6= ∅ implies that they are equal. Finally, (3) easily follows from the fact that L(Gn , A) ⊆ Qnn . 2

Generating All Circular Shifts by Context-Free Grammars in CNF

151

Example 3.5 Consider the context-free grammar GU 3 = (V3 , Σ3 , P3 , S3 ) with N3 = {S3 , A12 , A23 , A31 , A1 , A2 , A3 } and P3 consists of S3 → A12 A3 | A23 A1 | A31 A2 , A12 → A1 A2 , A23 → A2 A3 , A31 → A3 A1 , A1 → a1 , A2 → a2 and A3 → a3 . Then L(GU 3 ) = C3 , E3,3 = {S3 }, E3,2 = {A12 , A23 , A31 }, E3,1 = {A1 , A2 , A3 }, and hence D(3, 3) = 1 and D(3, 2) = D(3, 1) = 3. As S3 → A23 A1 is in P3 , L(G3 , A23 ) = {a2 a3 }, L(G3 , A1 ) = {a1 } and a3 ≺ a1 . 2 For a context-free grammar Gn from a family {Gn }n≥1 , we use as descriptional complexity measures the number ν(n) of nonterminal symbols and the number π(n) of production rules of Gn ; thus ν(n) = #Nn and π(n) = #Pn . As in [2] we will view ν and π as functions of n. For a more general and thorough treatment of descriptional complexity for context-free grammars and their languages we refer to [8, 10, 11, 5, 3, 1, 4]. In [2] we introduced another descriptional complexity measure, viz. the number of left-most derivations δ(n), i. e., δ(n) = #{Sn ⇒⋆L x | x ∈ L(Gn )}, where ⇒L denotes the leftmost derivation relation. Clearly, this measure makes sense when we generate a finite language by means of a grammar with bounded ambiguity. As these measures depend on n as well as on the family under consideration, we use να (n), πα (n) and δα (n) in case of a family {Gα n }n≥1 . Example 3.6 (1) For the grammar GP 3 of Example 3.2, we have νP (3) = 7, πP (3) = 12 and δP (3) = 3! = 6, since GP 3 is unambiguous [2]. (2) Similarly, for the unambiguous grammar GU 3 of Example 3.5, we have νU (3) = 7, πU (3) = 9 and δU (3) = 3. 2 When we consider a family of grammars {Gα n }n≥1 for {Cn }n≥1 in the sequel, we = (V always assume that the first element Gα 1 , Σ1 , P1 , S1 ) satisfies N1 = {S1 } and 1 P = {S → a }. Then ν (1) = π (1) = δ (1) = 1 whereas for n ≥ 2, να (n) = 1 1 α α α P1n D(n, k) ≥ n + 1, π (n) ≥ n + 2 and δ (n) ≥ n. α α k=1 4. A Straightforward Approach According to the previous section an obvious way to generate the language C n is to provide Gn with nonterminal symbols Ax such that if |x| = k < n, then L(Gn , Ax ) = {x} with x ∈ Qnn−1 and Ax ∈ En,k . The words from Cn = Qnn − Qnn−1 are treated differently: they should be derived from Sn . So there are productions Sn → Ax Ay for all nonempty words x and y with xy ∈ Cn , while En,n = {Sn }. Definition 4.1 The family {G0n }n≥1 is given by {(Vn , Σn , Pn , Sn )}n≥1 with for n ≥ 2, Nn = Vn − Σn = {Sn } ∪ {Ax | x ∈ Qnn−1 }, Pn = {Sn → Ax Ay | xy ∈ Cn ; x, y ∈ Σ+ n } ∪ {Aa → a | a ∈ Σn } n ∪ {Axy → Ax Ay | xy ∈ Qn−1 ; x, y ∈ Σ+ n }.

2

152

P. R. J. ASVELD

Of course, Sn is the only nonterminal from G0n that generates more than a single string provided n ≥ 2. Viz. #L(G0n , Sn ) = #Cn = n and #L(G0n , A) = 1 for each A in Nn − {Sn }; cf. Proposition 3.4. In the sequel we will slightly change our notation in order to reduce the number of subscript levels: if x = aj . . . ak , we will write Aj...k for Ax instead of Aaj ...ak ; cf. Examples 3.2 and 3.5 above. In this way the set of indices {1, 2, . . . , n} inherits the linear order of Σn ; a similar remark applies with respect to the ≺-relation. Example 4.2 For n = 3, Definition 4.1 yields G03 = (V3 , Σ3 , P3 , S3 ) with N3 = {S3 , A12 , A23 , A31 , A1 , A2 , A3 } and P3 = {S3 → A1 A23 | A12 A3 | A2 A31 | A23 A1 | A3 A12 | A31 A2 , A12 → A1 A2 , A23 → A2 A3 , A31 → A3 A1 , A1 → a1 , A2 → a2 , A3 → a3 }. Now E3,3 = {S3 }, E3,2 = {A12 , A23 , A31 }, E3,1 = {A1 , A2 , A3 }, D(3, 3) = 1 and D(3, 2) = D(3, 1) = 3. Thus ν0 (3) = 7, π0 (3) = 12 and δ0 (3) = 6. 2 Proposition 4.3 For the family {G0n }n≥1 of Definition 4.1 we have (1) (2) (3) (4)

D(n, n) = 1 and D(n, k) = n with 1 ≤ k < n, ν0 (n) = n2 − n + 1, π0 (n) = 12 n3 − 21 n2 + n, δ0 (1) = 1 and for n ≥ 2, δ0 (n) = n2 − n.

Proof. The definitions of Nn and Pn in 4.1 imply that for n ≥ 2, ν0 (n) = #Nn = 1 + #Qnn−1 = 1 + (n − 1)n = n2 − n + 1, while π0 (n) = h0 (n) + h1 (n) + h2 (n) with h0 (n) = #{Sn → Ax Ay | xy ∈ Cn ; x, y ∈ Σ+ n }, h1 (n) = #{Axy → Ax Ay | xy ∈ Qnn−1 ; x, y ∈ Σ+ n }, and h2 (n) = #{Aa → a | a ∈ Σn }.

Pn−1 Clearly, h0 (n) = n(n − 1) and h2 (n) = n, whereas h1 (n) = k=2 (k − 1) · n = 1 1 1 3 1 2 2 n(n−1)(n−2). Consequently, π0 (n) = n(n−1)+ 2 n(n−1)(n−2)+n = 2 n − 2 n +n. 0 Gn generates n strings, each of which can be obtained by a left-most derivation in n − 1 ways provided n ≥ 2; hence δ0 (n) = n(n − 1). 2 5. An Improvement The family {G1n }n≥1 to be introduced in this section, is obtained from {G0n }n≥1 by reducing the number of rules. Viz. if A → BC is a production of G1n , then we restrict the form of this rule by requiring that L(G1n , B) = {x} with |x| = 1. This idea originates from generating Ln by regular grammars; cf. [2] for details and examples. Definition 5.1 The family {G1n }n≥1 is given by {(Vn , Σn , Pn , Sn )}n≥1 with for n ≥ 2, Nn = Vn − Σn = {Sn } ∪ {Ax | x ∈ Qnn−1 }, Pn = {Sn → Ax Ay | xy ∈ Cn ; x ∈ Σn } ∪ {Aa → a | a ∈ Σn } ∪ {Axy → Ax Ay | xy ∈ Qnn−1 ; x ∈ Σn , y ∈ Σ+ n }.

2

Generating All Circular Shifts by Context-Free Grammars in CNF

153

Example 5.2 In case n = 3 we obtain from Definition 5.1 G13 = (V3 , Σ3 , P3 , S3 ) with N3 = {S3 , A12 , A23 , A31 , A1 , A2 , A3 } and P3 = {S3 → A1 A23 | A2 A31 | A3 A12 , A12 → A1 A2 , A23 → A2 A3 , A31 → A3 A1 , A1 → a1 , A2 → a2 , A3 → a3 }. Again we have E3,3 = {S3 }, E3,2 = {A12 , A23 , A31 }, E3,1 = {A1 , A2 , A3 }, D(3, 3) = 1, D(3, 2) = D(3, 1) = 3 and ν1 (3) = 7. But now π1 (3) = 9 and δ1 (3) = 3. 2 Proposition 5.3 For the family {G1n }n≥1 of Definition 5.1 we have (1) D(n, n) = 1 and D(n, k) = n with 1 ≤ k < n, (2) ν1 (n) = n2 − n + 1, (3) π1 (n) = n2 , (4) δ1 (n) = n. Proof. As in the proof of Proposition 4.3 we still have for n ≥ 2, ν1 (n) = n2 − n + 1. Again we write π1 (n) for n ≥ 2 as π1 (n) = h0 (n) + h1 (n) + h2 (n) with h0 (n) = #{Sn → Ax Ay | xy ∈ Cn ; x ∈ Σn }, h1 (n) = #{Axy → Ax Ay | xy ∈ Qnn−1 ; x ∈ Σn , y ∈ Σ+ n }, and h2 (n) = #{Aa → a | a ∈ Σn }. Then still we have h2 (n) = n, but now h0 (n) = n and h1 (n) = #(Qnn−1 − F1n ) = #Qnn−1 − #F1n = n(n − 1) − n = n2 − 2n. Hence π1 (n) = n + n2 − 2n + n = n2 . Again, the grammars G1n generate n strings, but now there is only a single (leftmost) derivation for each word in L(G1n ); therefore we have δ1 (n) = n. 2 Although the number of nonterminal symbols did not change (viz. ν1 (n) = ν0 (n)), we have improved upon the number of production rules. In this regard the G1n ’s are superior to the G0n ’s of Definition 4.1, because for n ≥ 3, we have the strict inequality π1 (n) = n2 < 21 n3 − 21 n2 + n = π0 (n). 6. Families of Unambiguous Grammars In Section 5 we saw that the family {G1n }n≥1 is a slight improvement upon {G0n }n≥1 ; so it might be considered as a first step toward a family of minimal grammars. When we discuss minimality of grammars, the first question is: With respect to which measure are the Gn ’s minimal? Obvious candidates are, of course, the measures ν, π or δ, but also combined measures as the sum or the product of ν and π (or any monotonically increasing function in ν and π) may be considered as well. Comparing Definitions 4.1 and 5.1 together with Propositions 4.3 and 5.3, suggests that the first step toward a family of minimal grammars is to avoid ambiguity or, equivalently, to require that δ(n) = n. Proposition 6.1 Let {Gn }n≥1 be a family of grammars in Chomsky normal form that generates {Cn }n≥1 . Then δ(n) = n if and only if π(n) = ν(n) + n − 1.

154

P. R. J. ASVELD

Proof. Suppose δ(n) = n, i. e., Gn is unambiguous. As Gn is in Chomsky normal form, each nonterminal is useful. Thus for each A in Nn − {Sn } there is at least one production. By Proposition 3.4(1), we have #L(Gn , A) = #{xA } = 1. So assuming that there is more than one rule for A in Pn yields two or more left-most derivations for xA . Since these derivations of xA are used as left-most subderivations of some x in Cn , there are two or more left-most derivations of x; hence δ(n) > n. Consequently, for each A in Nn − {Sn }, there is exactly one rule in Pn . For Sn we have that n rules are necessary and sufficient to generate the n words of Cn = L(Gn , Sn ) because δ(n) = n. Hence π(n) = ν(n) − 1 + n. Conversely, suppose that π(n) = ν(n) + n − 1. For each A ∈ Nn − {Sn }, there is at least one rule; so the number r of Sn -rules satisfies r ≤ n. If r < n, there is a production Sn → AB which yields two unequal terminal strings in Cn . But this is impossible by Proposition 3.4(1); hence r = n. Thus for each A ∈ Nn − {Sn }, there is exactly one rule. Now it is straightforward to show that Gn is unambiguous, since each Sn -rule uniquely derives a single word of Cn in a left-most way. 2 Note that this result applies to {G1n }n≥1 but not to {G0n }n≥1 ; cf. Propositions 4.3 and 5.3. However, {G1n }n≥1 is not the only family satisfying this equality; viz. Definition 6.2 The family {G2n }n≥1 is given by {(Vn , Σn , Pn , Sn )}n≥1 with N2 = V2 − Σ2 = {S2 , A1 , A2 }, P2 = {S2 → A1 A2 | A2 A1 , A1 → a1 , A2 → a2 }, and for n ≥ 3, Nn = Vn − Σn = {Sn } ∪ {Aa | a ∈ Σn } ∪ {Ax | x ∈ F2n } ∪ Mn with for m ≥ 2, 2m 2m M2m = {Ax | x ∈ F2m−2 ∪ F2m−4 ∪ · · · ∪ F42m }, and 2m−1 2m−1 M2m−1 = {Ax | x ∈ F2m−3 ∪ F2m−5 ∪ · · · ∪ F32m−1 },

Pn = {Sn → Ax Ay | xy ∈ Cn ; x ∈ F2n } ∪ {Axy → Ax Ay | Axy ∈ Mn , x ∈ F2n } ∪ {Axy → Ax Ay | xy ∈ F2n , x, y ∈ Σn } ∪ {Aa → a | a ∈ Σn }. 2 If A → BC is a rule in G1n , then L(G1n , B) consists of a single string of length one; in G2n we have L(G2n , B) = {wB } with |wB | = 2. Example 6.3 For the case n = 5 we obtain from Definition 6.2 the grammar G25 = (V5 , Σ5 , P5 , S5 ) with N5 = {S5 , A1 , A2 , A3 , A4 , A5 , A12 , A23 , A34 , A45 , A51 } ∪ M5 with M5 = {A123 , A234 , A345 , A451 , A512 }, and P5 = {S5 → A12 A345 | A23 A451 | A34 A512 | A45 A123 | A51 A234 , A123 → A12 A3 , A234 → A23 A4 , A345 → A34 A5 , A451 → A45 A1 , A512 → A51 A2 , A12 → A1 A2 , A23 → A2 A3 , A34 → A3 A4 , A45 → A4 A5 , A51 → A5 A1 , A1 → a1 , A2 → a2 , A3 → a3 , A4 → a4 , A5 → a5 }. The equivalence classes are E5,5 = {S5 }, E5,4 = ∅, E5,3 = {A123 , A234 , A345 , A451 , A512 }, E5,2 = {A12 , A23 , A34 , A45 , A51 } and E5,1 = {A1 , A2 , A3 , A4 , A5 }. Now we have ν2 (5) = 16 < 21 = ν1 (5) and π2 (5) = 20 < 25 = π1 (5) which indicates 2 that {G1n }n≥1 is not minimal.

Generating All Circular Shifts by Context-Free Grammars in CNF

155

In order to show the next results concisely we need a function that indicates whether its argument is even or odd: we will use the binary mod-operation. This operation should not be confused with the congruence relation a ≡ b (mod m). Proposition 6.4 For the family {G2n }n≥1 of Definition 6.2, we have (1) D(n, n) = 1, D(n, 1) = D(n, 2) = n, and for 3 ≤ k < n, D(n, k) = if k ≡ n (mod 2) then n else 0, (2) ν2 (1) = 1 and for n ≥ 2, ν2 (n) = 12 n2 + 12 n · (n mod 2) + 1, (3) π2 (1) = 1 and for n ≥ 2, π2 (n) = 12 n2 + n + 21 n · (n mod 2), (4) δ2 (n) = n. Proof. From Definition 6.2, Proposition 6.4(1) easily follows. Then for even n with Pn−2 n ≥ 3, we have ν2 (n) = 1 + 2n + k=4 D(n, k) = 1 + 2n + 12 (n − 4)n = 21 n2 + 1. For Pn−2 odd n with n ≥ 3, this yields ν2 (n) = 1 + 2n + k=3 D(n, k) = 1 + 2n + 12 (n − 3)n = 1 1 2 1 1 2 2 n + 2 n + 1. Combining these two cases results in ν2 (n) = 2 n + 2 n · (n mod 2) + 1 2 for n ≥ 3. For n = 2, we get from G2 that ν2 (2) = 3 and π2 (2) = 4, accordingly. Finally, Proposition 6.1 implies Proposition 6.4(3). 2 From Propositions 5.3 and 6.4 it follows that for n ≥ 4, we have ν1 (n) > ν2 (n) and, consequently, π1 (n) > π2 (n); for smaller values of n we have equality in both cases. Of course, we may proceed in this way by defining families {Gkn }n≥1 (k ≥ 3) with the property that if A → BC is a rule of Gkn , then L(Gkn , B) consists of a single word of length k. However, the definitions of such families become more complicated as k increases (cf. Definitions 6.2 and 5.1) and, in addition, we have to define the grammars Gk1 , Gk2 , . . . , Gkk explicitly (e. g., in terms of Gji for some j < k). And probably we are still left with νk (n) and πk (n) being functions in Θ(n2 ). 7. A Family of Minimal Grammars Our quest for a family {Gn }n≥1 of minimal grammars that generate {Cn }n≥1 is directed by the following two observations. Firstly, Proposition 6.1 implies that avoiding ambiguity is a major contribution. Then δ reaches its minimum value (i. e., δ(n) = n) and we may concentrate on minimizing ν(n): as a consequence π(n) will reach its minimum value as well. The second observation concerns the definition of “minimality” in this context. From the last remark in Section 3 it follows that ν(n), π(n), δ(n) ∈ Ω(n). So the following definition is an obvious choice. Definition 7.1 Let {Gn }n≥1 be a family of context-free grammars in Chomsky normal form that generates {Cn }n≥1 . Then the family {Gn }n≥1 is called minimal if (1) each Gn (n ≥ 1) is an unambiguous grammar, and (2) the number ν(n) of nonterminal symbols of Gn satisfies ν(n) ∈ Θ(n).

2

156

P. R. J. ASVELD

Next we will show that there exists a family that meets these conditions. Definition 7.2 The family {G⋄n }n≥1 is given by {(Vn , Σn , Pn , Sn )}n≥1 with for n ≥ 2, Nn = Vn − Σn = {Sn , A1 , A2 , . . . , An } ∪ {A12...k , Ak(k+1)...n | 1 < k < n}, Pn = {Sn → A1 A23...n } ∪ {Sn → Ak(k+1)...n A12...(k−1) | 1 < k ≤ n} ∪ {A12...k → A12...(k−1) Ak | 1 < k < n} ∪ {Ak(k+1)...n → Ak A(k+1)...n | 1 < k < n} ∪ {Ak → ak | 1 ≤ k ≤ n}. 2 Example 7.3 For n = 5, Definition 7.2 yields the grammar G⋄5 = (V5 , Σ5 , P5 , S5 ) with N5 = {S5 , A1 , A2 , A3 , A4 , A5 , A12 , A123 , A1234 , A45 , A345 , A2345 } with P5 = {S5 → A1 A2345 | A2345 A1 | A345 A12 | A45 A123 | A5 A1234 , A1234 → A123 A4 , A123 → A12 A3 , A12 → A1 A2 , A2345 → A2 A345 , A345 → A3 A45 , A45 → A4 A5 , A1 → a1 , A2 → a2 , A3 → a3 , A4 → a4 , A5 → a5 }. Now E5,5 = {S5 }, E5,4 = {A1234 , A2345 }, E5,3 = {A123 , A345 }, E5,2 = {A12 , A45 }, E5,1 = {A1 , A2 , A3 , A4 , A5 }, ν⋄ (5) = 12, π⋄ (5) = 16 and δ⋄ (5) = 5. 2 Proposition 7.4 For the family {G⋄n }n≥1 of Definition 7.2 we have (1) D(n, n) = 1, D(n, 1) = n, and D(n, k) = 2 with 1 < k < n, (2) ν⋄ (1) = 1 and for n ≥ 2, ν⋄ (n) = 3n − 3, (3) π⋄ (1) = 1 and for n ≥ 2, π⋄ (n) = 4n − 4, (4) δ⋄ (n) = n. Proof. It is a routine matter to verify that L(G⋄n ) = Cn and that G⋄n is unambiguous for n ≥ 1. Definition 7.2 implies ν⋄ (n) = #Nn = n + 1 + 2(n − 2) = 3n − 3 provided that n ≥ 2. The other statements follow from Proposition 6.1 and Definition 7.2. 2 Corollary 7.5 The family {G⋄n }n≥1 of Definition 7.2 is minimal.

2

Since both ν⋄ (n) and π⋄ (n) are linear in n, we have for i = 0, 1, 2 that ν⋄ (n) < νi (n) and π⋄ (n) < πi (n) for all n ≥ 5. 8. Concluding Remarks In the previous sections we discussed a few ways of generating the languages {C n }n≥1 of circular shifts by context-free grammars {Gn }n≥1 in Chomsky normal form. The family {G0n }n≥1 is as straightforward as Definition 4.1 in [2]; {G1n }n≥1 is inspired by Definition 5.4 in [2], whereas for {G2n }n≥1 there is no immediate analogue in [2]. On the other hand the family {G⋄n }n≥1 reflects the main idea of Definition 7.1 of [2]: in that Definition a grammatical transformation is used to define the family inductively. The interested reader may compare G⋄n and G⋄n+1 and infer a transformation to define {G⋄n }n≥1 inductively.

157

Generating All Circular Shifts by Context-Free Grammars in CNF ν(n) G0n

n2 − n + 1

G1n

n2 − n + 1

G2n

1 2 2n

G⋄n

3n − 3

π(n) 1 3 2n 2

− 12 n2 + n

1 2 2n

+ n + 12 n · (n mod 2)

n

+ 21 n · (n mod 2) + 1

δ(n) n2 − n n

4n − 4

n n

Table 1: Complexity measures

We compared these grammar families with respect to their descriptive complexity measures ν(n), π(n) and δ(n). These measures turned out to be bounded by lowdegree polynomial functions. In case of unambiguous grammars, i. e., when δ(n) = n, there is a simple relation between ν(n) and π(n); cf. Proposition 6.1. A summary of our results for n ≥ 2 is in Table 1. Going downwards in this table means an improvement of at least one complexity measure, provided n ≥ 5. The last family in this table {G⋄n }n≥1 is minimal in the sense of Definition 7.1; cf. Corollary 7.5. Whether it is also minimal in an absolute sense (i. e., does there exist a family of unambiguous grammars with ν(n) < 3n − 3 and, consequently, with π(n) < 4n − 4 for n large enough?) is a difficult open problem. First of all, the coefficient 3 is rather small, and any progress must be made by Sn−1 economizing in the 2n − 4 elements of k=2 En,k , since D(n, n) = 1 and D(n, 1) = n because En,n = {Sn } and En,1 = {A1 , A2 , . . . , An }, respectively. E.g., a straightforward divide-and-conquer approach (usually to be considered as very efficient; cf. Proposition 8.3 in [2]) yields a grammar family with for n = 2m (m ≥ 2), D(n, n) = 1, D(n, 2l ) = n, for 0 ≤ l < m = log2 n, and D(n, k) = 0, otherwise, which results in ν(n) = n⌈log2 n⌉ + 1. Of course, this is not an improvement with respect to ν⋄ (n) of Proposition 7.4. Secondly, and more importantly, determining the minimum number of contextfree productions (regardlessly whether they are in Chomsky normal form) needed to generate a finite language is a notoriously difficult problem. For discussions, examples and complicated (partial) results we refer to [5, 4, 3, 1] and in particular to [13]. In Section 1 we noticed that viewed as a set of permutations of a1 a2 . . . an the set Cn forms a cyclic subgroup of order n of the symmetric group Sn over Σn . We conclude this section with a few trivial subgroups of Sn : • In = {a1 a2 . . . an } – the identity permutation; • Rn = {a1 a2 . . . an−1 an , an an−1 . . . a2 a1 } – an element and its reversal; • Tn = {a1 a2 a3 . . . an , a2 a1 a3 . . . an } – an element and the result of transposing its first two symbols.

158

P. R. J. ASVELD

We leave it as an exercise to the interested reader to construct families of unambiguous grammars for these language families for which the following holds (n ≥ 2). Xn

νX (n)

πX (n)

δX (n)

νX (2)

νX (n + 1)

πX (2)

πX (n + 1)

In

2n − 1

2n − 1

1

3

νI (n) + 2

3

πI (n) + 2

Rn

3n − 3

3n − 2

2

3

νR (n) + 3

4

πR (n) + 3

Tn

2n − 1

2n

2

3

νT (n) + 2

4

πT (n) + 2

The last four columns in this table show the recurrence relations by which ν X (n) and πX (n) are defined. References [1] B. Alspach, P. Eades, G. Rose, A lower-bound for the number of productions for a certain class of languages. Discrete Appl. Math. 6 (1983), 109–115. [2] P. R. J. Asveld, Generating all permutations by context-free grammars in Chomsky normal form. Theoret. Comput. Sci. 354 (2006), 118–130. An earlier version appeared in: F. Spoto, G. S. Scollo, A. Nijholt (eds.), Proc. Algebraic Methods in Language Processing (AMiLP 2003), 3rd AMAST Workshop on Language Processing, Verona, Italy, August 2003, pp. 55–68; ISSN 0929-0672. [3] W. Bucher, A note on a problem in the theory of grammatical complexity. Theoret. Comput. Sci. 14 (1981), 337–344. [4] W. Bucher, H. A. Maurer, K. Culik II, Context-free complexity of finite languages. Theoret. Comput. Sci. 28 (1984), 277–285. [5] W. Bucher, H. A. Maurer, K. Culik II, D. Wotschke, Concise description of finite languages. Theoret. Comput. Sci. 14 (1981), 227–246. [6] J. Dassow, On the circular closure of languages. EIK 15 (1979) 87–94. [7] R. L. Graham, D. E. Knuth, O. Patashnik, Concrete Mathematics. AddisonWesley, Reading, MA, 1989. [8] J. Gruska, Some classifications of context-free languages. Inform. Contr. 14 (1969), 152–179. [9] M. A. Harrison, Introduction to Formal Language Theory. Addison-Wesley, Reading, MA, 1978. [10] V. A. Iljuˇ skin, The complexity of the grammatical description of context-free languages. Dokl. Akad. Nauk SSSR 203 (1972), 1244–1245 / Soviet Math. Dokl. 13 (1972), 533–535. ´, Complexity of normal form grammars. Theoret. Comput. Sci. [11] A. Kelemenova 28 (1984), 299–314. [12] C. L. Liu, Introduction to Combinatorial Mathematics. McGraw-Hill, New York, etc., 1968.

Generating All Circular Shifts by Context-Free Grammars in CNF

159

[13] Z. Tuza, On the context-free production complexity of finite languages. Discrete Appl. Math. 18 (1987), 293–304. (Received: May 11, 2005; revised: November 21, 2006)