Object grammars and random generation I. Dutour LaBRI, Université Bordeaux I, Unité associée au C.N.R.S. no 1304, 351 cours de la Libération, 33405 Talence Cedex, France. J.M. Fédouy I3S-ESSI, Univesité de Nice, Unité associée au C.N.R.S. no 1376, 650 Route des Colles, B.P. 145, 06903 Sophia-Antipolis Cedex, France. February 28, 1997 Abstract
This article presents a new systematic approach for the uniform random generation of combinatorial objects. The method is based on the notion of object grammars which are recursive descriptions of objects generalizing context-free grammars. The application of particular valuations to these grammars allows to reach to enumeration and random generation of objects according to non algebraic parameters.
1 Introduction Every recursive description of objects dened by means of terminal objects and operations applied to objects (mappings) is called an object grammar. It is most often described with drawings. For instance, the standard decomposition of complete binary trees is an object grammar (Figure 1). The formalism given here for object grammars [7, 8] generalizes the one for context-free grammars. It is akin to the work of Flajolet & al. [10, 11] allowing for the specication of structures by grammars involving set, sequence and cycle constructions. One can also categorize object grammars as belonging to the domain of Universal Algebra and Magmas [9, 15, 1]. Finally, our approach is related to the Theory of Species [3, 14] which gives a general approach to the description and construction of combinatorial structures on nite sets, as well as the various kind of enumeration of these structures. This paper outlines a systematic method using object grammars for generating combinatorial objects uniformly at random. This work lies in the recursive method framework ; this technique allows to recursively generate the random objects by interpreting the enumeration recurrence relations. This generation e-mail y e-mail
: :
[email protected] [email protected] 1
Figure 1: Complete binary trees. process have been formalized by Nijenhuis and Wilf [16], then by Hickey and Cohen in the case of context-free languages [13] and by Greene within the framework of the labelled formal languages [12]. Recently, Flajolet, Zimmermann and Van Cutsem have given a systematic approach for this method concerning their specications of structures [11]. The methods that they have examined enable to start from any hight level specication of decomposable class and compile automatically procedures that solve the corresponding random generation problem. They have presented two closely related groups of methods : the sequentiel algorithms (linear search) which have worst case time complexity O(n2), when applied to objects of size n, and the boustrophedonic algorithms (bidirectional fashion) which have worst case time complexity O(n log n). Object grammars allows to widen the eld of structures which can be generating using the recursive method. They are useful for generating objects unformly at random according severals parameters. The method which we are going to examine enables to consider not only algebraic parameters (i.e. which lead to algebraic generating functions), but also parameters which lead to generating functions satisfying q -equations. The other methods have rarely treated with these latest parameters. Given an unambigous object grammar and an associated q-linear valuation (then the generating function satises a q-equation), our method allows to construct automatically the enumeration and uniform generation procedures according to the valuation. The input is the object grammar and the valuation, the output is the set of enumeration and generation procedures. These procedures use sequential algorithms and have worst case time complexity O(kl(k + l)), when applied to objects of valuation xk ql , and supposing that the enumeration tables have been computed once for all in O(k2l2) time (see [7] for the general case). If one only considers an algebraic parameter (xk ), the complexity is the same as in [11] (for sequential algorithms). The path taken here is eminently praticable. This method have been implemented in the Maple language (package named qAlGO). The section 5 gives some results obtained with this program. It concerns the uniform random generation of convex polyominoes according to the aera. Last, the interest of the approach developed lies in its generality and simplicity. Time complexity are computable and, at the same time, one gains access to the random generation of arbitrarily complex objects according severals parameters, algebraic or not.
2
Figure 2: A parallelogram polyomino. φ2
φ1
φ3 ,
Figure 3: Object operations on parallelogram polyominoes.
2 Object grammars
2.1 Denitions, properties and notations
Let E be a family of sets of objects. An object operation in E is a mapping : E1 : : : Ek ?! E , where E 2 E and Ei 2 E for i in [1; k]. It simply describes the way of obtaining an object of E from k objects of E1; : : :; Ek respectively. The domain of is denoted by dom(), the codomain by codom() and the image by Im(). The i-th projection Ei of dom() is called a component. Example 2.1 : A parallelogram polyomino can be dened by a pair of paths only made with north and east steps and such that the paths are disjoint, except at their common ending points (see Figure 2). Let Epp be the set of parallelogram polyominoes. The mappings 1 , 2 and 3 described graphically in Figure 3 are object operations in E = fEppg. 1 and 2 are operations of arity 1 (Epp ?! Epp). 1 takes one polyomino as argument and glues a new cell at left (west) of the lowest cell of the rst column. 2 adds a new cell at the bottom of each column of the polyomino taken as argument. 3 is an operation of arity 2 (Epp Epp ?! Epp) ; it takes two polyominoes as argument, applies 2 to the rst and glues them by one cell: the top-cell of the last right column of the rst polyomino facing the bottom-cell of the rst left column of the second.
Denition 2.1 An object grammar is a 4-tuple < E ; T ; P ; S > where :
3
φ1 φ3 φ2
φ3
φ1
φ3
Figure 4: A derivation tree in G2.
E = fEigi2I
is a nite family of sets of objects. (I nite part of IN) T = fTEi gi2I is a nite family of nite subsets of elements of E , TEi Ei, whose elements are called terminal objects. P is a set of object operations in E . S is a xed element of E called the axiom of the grammar.
The dimension of an object grammar is the number of sets of objects in E . Remark : Sometimes a 3-tuple < E ; T ; P > is called also object grammar. The axiom is chosen later in E . The derivation trees of an object grammar represent very well the natural hiearchical structure of the applying of object operations for constructing objects from the terminals : internal nodes are labelled with object operations and leaves with objects. These derivation trees are comparable to the abstract trees within the theory of Compiling. Let G =< E ; T ; P > be an object grammar and E 2 E a set of objects. An object o is said to be generated in G by E , if there is a derivation tree of G on E (codomain of root = E ) whose evaluation is o. The set of generated objects in G by E is denoted by OG (E ). If S of E is xed as axiom of G, OG (S ) is then called the set of objects generated by G. Example 2.2 : Let's note 2 the unit cell of a polyomino. Here are two examples for object grammars:
G1 = < fEppg; ff2gg; f1; 2g; Epp > and G2 = < fEppg; ff2gg; f1; 2; 3g; Epp > . The parallelogram polyomino in Figure 2 belongs to OG2 (Epp), its derivation tree in G2 is given in Figure 4. OG1 (Epp) is the set of Ferrers diagrams which is a subset of parallelogram polyominoes, and OG2 (Epp) is the whole set of parallelogram polyominoes. 4
Figure 5: Schematic object grammar of parallelogram polyominoes. By analogy to context-free grammars, an object grammar G is unambiguous if every object in OG (S ) has exactly one derivation tree. Unambiguity is an important property in the eld of bijection buildings. One can also dene several normal forms for object grammars: reduced, form 1-2 or complete. The forms reduced and form 1-2 extend usual normal forms for context-free grammars: reduced and Chomsky normal form. An object grammar is said to be in form 1-2 if its all object operations are uniquely operations of arity 1 or arity 2. The complete form is specic for object grammars. An object grammar is said to be complete if OG (E ) = E for every set of objects E in G (generally OG (E ) E ). For example, the grammar G2 previously dened is complete while G1 is not. The details of transformations of object grammars in normal forms are given in [7].
Notations
An object grammar G =< E ; T ; P ; S > complete and unambiguous can be described as a system of equations (sets of objects, objects, object operations), or also a system of graphic equations. Every equation gives the decomposition of a set of objects in a disjoint union of terminal objects and image of object operations: 8
6= 0g is nite, is called an object valuation on E . X Consequently, the generating function associeted to E , VE (e), is a formal e2E
power series which lies in K [[X ]]. It will be denoted by VE (E ).
Theorem 2.2 Let G =< E ; T ; P > an object grammar unambiguous and com-
plete, and = fVE : E ?! K [X ]; E 2 Eg an associeted set of object valuations. For all E in E , from its equation in G
E =
X
e2TE
e +
X
codom()=E
(E1 ; : : :; Ek ) ,
(1)
one can directly obtain the following equation :
VE (E ) = VE (TE ) +
X
codom()=E
VE ((E1 ; : : :; Ek )) .
(2)
Proof :
The object grammar G is unambiguous and complete, then equation (1) implies an equality on sets with disjointed unions and equation (2) is obvious.
2
The objective is to obtain a system of equations for the generating functions of the sets of the grammar. Then, one have to express VE ((E1 ; : : :; Ek )) in terms of VE1 (E1 ), . . . , VEk (Ek ). This depends on the nature of object valuations that one considers. Example 2.3 : Let Epp bet the set of parallelogram polyominoes and the following valuation : Vla : Epp ?! IN[x; q]
7?! xwidth e qarea e
e
( )
( )
It is well-known that the generating function Vla (Epp), denoted here by fla (x; q), satises the q-equation (see for example [5])
fla(x; q) = xq + xqfla (x; q) + fla (xq; q) + fla (xq; q)fla(x; q) . Th object valuation Vla is called q -linear. The general denition of such
a valutaion is detailed below.
q-linear object valuations Let X = fx1 ; : : :; xn g and Q = fq1 ; : : :; qr g two disjointed sets of variables. Notations : x denotes the n-tuple (x1; : : :; xn ) and q the r-tuple (q1 ; : : :; qr ). If A is a matrix n r with coecients in IN (A = (aij ), 1 i n, 1 j r),
then
xqA = (x q a11 : : :qr a1r ; : : :; xnq an1 : : :qr anr ), 1 1
1
6
for f (x; q) 2 K [[X; Q]], f (x; q)jx xqA = f (xqA; q) . Let E; E ; : : :; Ek be sets of objects, VE ; VE1 ; : : :; VEk object valuations on E; E ; : : :; Ek respectively, and an object operation with codom() = E and dom() = E : : : Ek . VE is called q-linear with respect to if it exists a polynom in K [X; Q] and matrices Ai for i 2 [1; k] such that 1
1
1
VE ((e1 ; : : :; ek )) =
k
Y
i=1
VEi (ei)jx
xqAi
, for every (e1 ; : : :; ek ) 2 dom():
If is injective, then :
VE ((E1; : : :; Ek )) =
k
Y
i=1
VEi (Ei)jx
xqAi :
Corollary 2.3 If all the object valuations of are q-linear, equation (2) in theorem 2.2 becomes : VE (E ) = VE (TE ) +
X
codom()=E
k
Y
i=1
VEi (Ei)jx
xqAi .
(3)
Then, one obtains a system of q -equations. The unknowns are the generating functions for the sets E in E , VE (E ). Example 2.4 : The object valuation Vla is q -linear with respect to object operations 1, 2 and 3. Then the eqaution
Vla(Epp) = Vla(2) + Vla (1(Epp)) + Vla(2 (Epp)) + Vla (3(Epp; Epp)) becomes the q -equation seen before (fla (x; q ) = Vla(Epp))
fla(x; q) = xq + xqfla (x; q) + fla (xq; q) + fla (xq; q)fla(x; q) .
Particular case : linear object valuations
The linear object valuations are q -linear object valuations such that, for every object operation and for all i, Ai = (0). These linear valuations are exactly in the DSV methodology framework [4, 18], they yield algebraic generating functions.
3 Form 1-2 and object valuations The automatic method of random generation presented in the next section uses the form 1-2 of object grammars.
Denition 3.1 An object grammar G =< E ; T ; P > is called in expanded form 1-2 if, for every E in E , the equation that denes it has one of the forms 7
E = e ; E = E1 + E2 ; E = (E1 ) ; E = (E1; E2).
Proposition 3.2 Every object grammar G has an equivalent form 1-2. More-
over, if is a set of q -linear object valuations associated to G, it is possible to construct an equivalent set of object valuations associated to its form 1-2. Proof :
In order to transform an object grammar into an expanded form 1-2, it suces to cut all the sums and domains of the object operations having arity > 2 by adding sets of objects and identity object operations of arity 2. Thus, the equation E = (E1; : : :; Ek ) is replaced by the set of equations E = E1 (E1; FE2 ), FE2 = E2 (E2; FE3 ), . . . , FEk?1 = Ek?1 (Ek?1; Ek). Concerning the object valuations, if we have
VE ((E1; : : :; Ek )) =
k
Y
i=1
VEi (Ei )jx
;
xqAi
then we dene : 8 VE (E1 (E1; FE2 )) = VE1 (E1)jx xqA1 : VFE2 (FE2 ) > > > > > > i = 2 : : :k ? 2; > > > > > V = VFEi (Ei (Ei; FEi+1 )) > FEi (FEi ) > < = VEi (Ei)j Ai : VFEi+1 (FEi+1 ) > > > > > > > > > > > > > :
x xq
VFEk?1 (FEk?1 )
= VFEk?1 (Ek?1 (Ek?1; Ek )) = VEk?1 (Ek?1 ) Ak?1 : VEk (Ek )j x xq
x xqAk
2
4 Enumeration and random generation procedures All the object grammars G and all the possible associated sets of functions do not lead to random generation. The couples (G; ) considered here are wellfounded, i.e. each set of objects generates at least one object, and it generates a nite number of objects having the same value of valuation (it is the denition of an object valuation). An algorithm performing this test is detailed in [7]. It is inspired by works of Zimmermann [19]. In the following, the study is limited to the case of q -linear object valuations having values in the set of monomials denoted by Mon[X; Q]. Moreover, X and Q contain one variable : X = fxg and Q = fqg. The complete case is detailed in [7].
8
4.1 Enumeration procedures
Let G =< E ; T ; P > be an object grammar in expanded form 1-2 and = fVE : E ?! Mon[x; q]; E 2 Eg a set of q-linear valuations associated to G, such that the couple (G; ) is well-founded. The generating function of a set of objects E is denoted by
VE (E ) =
1
X
K;L=0
cE (K; L) xK q L :
Theorem 4.1 (i) The coecients cE (K; L) are given by the following formulas :
E = e, then if VE (e) = xK q L then cE (K; L) = 1 else 0;
E = E + E , then 1
2
cE (K; L) = cE1 (K; L) + cE2 (K; L);
E = (E ) with VE ((E )) = xk0 ql0 VE1 (E )jx xqa1 , then cE (K; L) = cE1 (K ? k ; L ? l ? (K ? k ):a ); 1
1
1
0
0
0
E = (E ; E ) with VE ((E ; E )) = xk0 ql0 VE1 (E )jx then
1
cE (K; L) =
2
K L
XX
i=0 j =0
1
1
2
1
xqa1 VE2 (E2)jx xqa2 ,
cE1 (i; j ):cE2 (K ?k0 ?i; L?l0 ?i:a1 ?(K ?k0 ?i):a2 ?j ):
(ii) The computation of all the coecients until the value xk q l needs O(k2l2) arithmetic operations. Proof :
The details of the algorithm allowing the computation of the coecients in O(k2l2 ) time are given in [7]. 2
4.2 Generation procedures
To each set of objects E of the grammar is associated a procedure gE having parameters k and l, and generating (uniformly at random) an object of E having the valuation xk q l . More precisely, this procedure constructs the derivation tree in the grammar. It depends on the form of the equation which denes E in the object grammar:
E = e. The procedure gE is trivial : 9
gE :=Proc(k,l) If VE (e) = xk ql Then Return e
End Proc
E = E + E . The procedure gE must generate an object belonging either to E or to E . The probability that this object belongs to E is equal to cE1 (k; l)=cE(k; l) : 1
2
1
2
1
gE :=Proc(k,l) U := Uniform([0; 1]); If U < cE1 (k; l)=cE(k; l) Then Return gE1 (k; l) Else Return gE2 (k; l)
End Proc
E = (E ), with VE ((E )) = xk0 ql0 VE1 (E )jx xqa1 . Then the procedure gE is very simple. It returns an object of E obtained by from an object of E having the valuation xk?k0 q l?l0 ? k?k0 :a1 : 1
1
1
(
1
)
gE :=Proc(k,l) kk := k ? k0; ll := l ? l0; Return ( gE1 (kk; ll ? kk:a1 ) )
End Proc
E = (E ; E ), with VE ((E ; E )) = xk0 ql0 VE1 (E )jx xqa1 VE2 (E )jx xqa2 . The procedure gE must generate an object of E obtained by from an object of E and an object of E which respect the denition of the valuations. The probability for the object of E having the valuation xK q L and those of E having the valuation xk?k0 ?K q l?l0 ?K:a1 ? k?k0 ?K :a2 ?L is cE1 (K; L):cE2 (k ? k ? K; l ? l ? K:a ? (k ? k ? K ):a ? L)=cE (k; l). 1
2
1
1
2
1
2
2
1
(
2
The procedure is :
0
0
1
0
2
gE :=Proc(k,l) kk := k ? k0; ll := l ? l0; S := cE1 (0; 0):cE2 (kk; ll ? kk:a2)=cE (k; l); K := 0; U := Uniforme([0; 1]); While U > S Do K := K + 1; T := ll ? K:a1 ? (kk ? K ):a2; L := 0; While U > S And L l Do L := L + 1; S := S + cE1 (k; l):cE2 (kk ? K; T ? L)=cE (k; l);
End While End While Return ( gE1 (k; l); gE2 (kk ? K; T ? L) ) End Proc 10
)
Random generation algorithm :
Input : a couple (G; ). Output : procedures for generating the objects generated by G at random.
? Transform G into form 1-2 =) (G0; 0 ). ? Verify that (G0; 0) is well-founded, else error. ? For each set of objects E in G0, create the enumeration procedure cE , then compute all the coecients until the rank (k; l). ? For each set of objects E in G0, create the generation procedures gE as indicated above.
Figure 6: A random generation procedure.
Theorem 4.2 The worst case time complexity of the generation procedures is of O(kl(k + l)) arithmetic operations. Proof :
A random generation procedure consists in constructing recursively a derivation tree in G. This tree is binary because G is in form 1-2. The size of the derivation tree of an object having the valuation xk q l is proportionnal to k + l. The generation of a vertex of the tree has a maximal cost of O(kl) (the loops of the procedure). Thus, the complexity of the generation of the derivation tree in the worst case is O(kl(k + l)). 2
4.3 Algorithm for uniform random generation
One can now describe an uniform random generation procedure for the objects of an object grammar according to a set of q -linear object valuations (Figure 6). The obtained generation procedures give the derivation trees of objects in G0, but not directly in G. A simple transformation (linear cost in O(k + l)) gives the derivation trees in G. This postprocessing does not aect the conclusions of the complexity studies. Futhermore, at the expense of some programming eort, it can be eected on the y.
5 Maple package qAlGO The program qAlGO (in Maple language)implements the method developed in the previous sections. The package qAlGO builds automatically the enumeration and generation procedures from a unambiguous object grammar and a set of associated q -linear valuations. The automatic nature of the software qAlGO gives a very useful tool that makes the experimental study of various statistics on combinatorial objects easier.
11
Convex polyominoes
We present here an example of experimental studies using qAlGO. It concerns the random generation, according to the area, of the dierent classes of convex polyominoes : parallelogram polyominoes, directed and convex polyominoes and convex polyominoes. Take the example of the parallelogram polyominoes. It suces to give in input to qAlGO an object grammar that generates them, as well as the associated object valuations. Thus one obtains the enumeration and the uniform generation according to the width (x) and the area (q ). > with(qalgo); [compile, countgo, drawgo, drawgoall] # definition of the object grammar and valuations > paralgo := { P = cell + phi1(P) + phi2(P) + phi3(P,P) }: > paralval := [[cell, 1, 1], [phi1, 1, 1, [0]], [phi2, 0, 0, [1]], [phi3, 0, 0, [1, 0]]]: # construction of the procedures > compile( paralgo, paralval, qlinear, Identity):
Then we are able to generate these objects at random. More precisely, qAlGO returns the derivation tree of a random parallelogram polyomino. For example, # generation of a polyomino having area 10 > drawgo( paralgo, paralval, qlinear, P, 10); phi3(phi1(phi2(phi2(phi2(cellp)))), phi1(phi2(cellp)))
Then it suces to evaluate this tree according to the object operations, and to write the polyomino under a form understood by the interface XAnimal of CalICo1 [6, 17] ; So we can visualize the below polyomino.
Such experiments showed us how the random convex polyominoes according to area is thin. According to the perimeter, they look more thick. Examples of such polyominoes are given Figures 7 and 8. Remark : Convex polyominoes having no directed growth, their orientation according to the area is either north-east, or north-west (as Figure 8), with the same probability one over two. In order to understand the thin allure of such random polyominoes according to area, we computed experimentally the average value of two parameters : the CalICo oers a software environment for manipulations and visualizations of combinatorial objects ; it allows to make communicate graphical interfaces and computer algebra software such as Maple. 1
12
Figure 7: Random parallelogram and convex polyominoes of perimeter 100.
Figure 8: Random parallelogram and convex polyominoes of aera 100. height of a column and the gluing number between two adjacent columns, which is the number of cells by which two adjacent columns are in contact. After generating 1000 convex polyominoes having area 100 and 1000 parallelogram polyominoes having area 200, we obtain the following average values :
2; 37 for the height of the columns, 1; 37 for the gluing number between two adjacent columns. Remark : The result for the average height of a column (2:37) coincides exactly
with that Bender [2] has obtained using asymptotic analysis methods. The dierence of 1 between these two average parameters can be explained very simply by noticing that this dierence for a polyomino has limit equal to the quotient between the height and the width of the polyomino, which is 1 by symetry.
6 Conclusions and perspectives Our method is automatic and generic and can be used for all the objects having recursive denitions even for non algebraic parameters. In the precedent section, we showed how it is possible to use the random generation in order to get conjectures on some parameters of objects. A lot of studies can be done for other objects as paths, trees . . . 13
In the case of specications, Flajolet, Zimmermann and Van Cutsem [11] have shown that the generation of a structure of size n is in O(n log n) by using a boustrophedonic algorithms instead of O(n2). It would be interesting to make a similar analysis to see if the application of the boustrophedonic principle can improve the complexity of our algorithm of generation in the same way.
References [1] A. Arnold and M. Dauchet. Une relation d'équivalence décidable sur la classe des forêts reconnaissables. Math. Sytems Theory, 12:103128, 1978. [2] E.A. Bender. Convex n-ominoes. Discrete Math., 8:219226, 1974. [3] F. Bergeron, G. Labelle, and P. Leroux. Theory of species and tree like structures. Cambridge University Press, To appear in 1997. [4] M. Delest. Langages algébriques : à la frontière entre la Combinatoire et l'Informatique. In Actes du 6eme Colloque Séries Formelles et Combinatoire Algébrique, pages 6978, DIMACS, Université de Rutgers, USA, 1994. [5] M. Delest and J.M. Fédou. Attribute grammars are useful for combinatorics. Theoretical Computer Science, 98:6576, 1992. [6] M. Delest, J.M. Fédou, and N. Rouillon. Human Interaction for Symbolic Computation, chapter Computation and Images in Combinatorics, To appear. Texts and Monographs in Symbolic Computation. Springer-Verlag, 1996. [7] I. Dutour. Grammaires d'objets : énumération, bijections et génération aléatoire. PhD thesis, Université Bordeaux I, Janvier 1996. [8] I. Dutour and J.M. Fédou. Object grammars and bijections. Submitted to ICALP'97. [9] S. Eilenberg and J.B. Wright. Automata in general algebra. Information and control, 11:452470, 1967. [10] P. Flajolet, B. Salvy, and P. Zimmermann. Automatic average-case analysis of algorithms. Theoretical Computer Science, 79(1):37109, 1991. [11] P. Flajolet, P. Zimmermann, and B. Van Cutsem. A calculus for the random generation of combinatorial structures. Theoretical Computer Science, 132:135, 1994. [12] D.H. Greene. Labelled formal languages and their uses. PhD thesis, Stanford University, June 1983. [13] T. Hickey and J. Cohen. Uniform random generation of strings in a context-free language. SIAM. J. Comput., 12(4):645655, 1983. [14] A. Joyal. Une théorie combinatoire des séries formelles. Advances in Math., 42(1):182, 1981. [15] J. Mezei and J.B. Wright. Algebraic automata and context-free sets. Information and control, 11:329, 1967. [16] A. Nijenhuis and H.S. Wilf. Combinatorial algorithms. Academic Press Inc., 1979. [17] N. Rouillon. Calcul et Image en Combinatoire. PhD thesis, Université Bordeaux I, Décembre 1994.
14
[18] X.G. Viennot. Enumerative combinatorics and algebraic languanges. In L. Budach, editor, Proceedings FCT'85, pages 450464, 1985. Lecture Notes in Computer Science 199, 1985. [19] P. Zimmermann. Séries génératrices et analyse automatique d'algorithmes. PhD thesis, Ecole Polytechnique de Palaiseau, Mars 1991.
15