<?xml version="1.0" encoding="UTF-8"?>
<TEI xml:space="preserve" xmlns="http://www.tei-c.org/ns/1.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.tei-c.org/ns/1.0 https://raw.githubusercontent.com/kermitt2/grobid/master/grobid-home/schemas/xsd/Grobid.xsd"
 xmlns:xlink="http://www.w3.org/1999/xlink">
	<teiHeader xml:lang="en">
		<fileDesc>
			<titleStmt>
				<title level="a" type="main">Exhaustive generation of positive lattice paths</title>
			</titleStmt>
			<publicationStmt>
				<publisher/>
				<availability status="unknown"><licence/></availability>
			</publicationStmt>
			<sourceDesc>
				<biblStruct>
					<analytic>
						<author>
							<persName><forename type="first">Elena</forename><surname>Barcucci</surname></persName>
							<email>elena.barcucci@unifi.it</email>
							<affiliation key="aff0">
								<orgName type="department">Dipartimento di Matematica e Informatica &quot;U. Dini&quot;</orgName>
								<orgName type="institution">Università degli Studi di Firenze</orgName>
								<address>
									<addrLine>Viale G.B. Morgagni 65</addrLine>
									<postCode>50134</postCode>
									<settlement>Firenze</settlement>
									<country key="IT">Italy</country>
								</address>
							</affiliation>
						</author>
						<author>
							<persName><forename type="first">Antonio</forename><surname>Bernini</surname></persName>
							<email>antonio.bernini@unifi.it</email>
							<affiliation key="aff0">
								<orgName type="department">Dipartimento di Matematica e Informatica &quot;U. Dini&quot;</orgName>
								<orgName type="institution">Università degli Studi di Firenze</orgName>
								<address>
									<addrLine>Viale G.B. Morgagni 65</addrLine>
									<postCode>50134</postCode>
									<settlement>Firenze</settlement>
									<country key="IT">Italy</country>
								</address>
							</affiliation>
						</author>
						<author>
							<persName><forename type="first">Renzo</forename><surname>Pinzani</surname></persName>
							<email>renzo.pinzani@unifi.it</email>
							<affiliation key="aff0">
								<orgName type="department">Dipartimento di Matematica e Informatica &quot;U. Dini&quot;</orgName>
								<orgName type="institution">Università degli Studi di Firenze</orgName>
								<address>
									<addrLine>Viale G.B. Morgagni 65</addrLine>
									<postCode>50134</postCode>
									<settlement>Firenze</settlement>
									<country key="IT">Italy</country>
								</address>
							</affiliation>
						</author>
						<title level="a" type="main">Exhaustive generation of positive lattice paths</title>
					</analytic>
					<monogr>
						<imprint>
							<date/>
						</imprint>
					</monogr>
					<idno type="MD5">4DD037A182A84503390F178BF51EAB2D</idno>
				</biblStruct>
			</sourceDesc>
		</fileDesc>
		<encodingDesc>
			<appInfo>
				<application version="0.7.2" ident="GROBID" when="2023-03-24T05:45+0000">
					<desc>GROBID - A machine learning software for extracting information from scholarly documents</desc>
					<ref target="https://github.com/kermitt2/grobid"/>
				</application>
			</appInfo>
		</encodingDesc>
		<profileDesc>
			<abstract>
<div xmlns="http://www.tei-c.org/ns/1.0"><p>We refer to lattice positive paths as to paths in the discrete plane constituted by different kinds of steps (north-east, east and south-east), starting from the origin and never going under the x-axis. They have been deeply studied both from a combinatorial and an algorithmic point of view. We propose some algorithms for the exhaustive generation of positive paths which are prefixes of Dyck, Motzkin and q-coloured Motzkin paths, according to their length. For each kind of path we define a recursive version as well an iterative one, specifying which path follows a given one in the lexicographic order. Furthermore we study the complexity of these algorithms by using the relations between the number of paths of given size and the number of north-east steps appearing in the final rise.</p></div>
			</abstract>
		</profileDesc>
	</teiHeader>
	<text xml:lang="en">
		<body>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="1">Introduction</head><p>Random and exhaustive generation has a fundamental role in the study of parameters characterizing different classes of combinatorial objects as well as for testing the algorithms operating on them. Many classes of paths in the plane and other combinatorial objects have been studied from the point of view of random generation [AS95, BBHT17, BBJ17, BPS95, DFLS04]. In their books <ref type="bibr" target="#b21">[Rus03,</ref><ref type="bibr" target="#b20">Knu08]</ref>, F. Ruskey and D. Knuth propose many algorithms for the exhaustive generation of several combinatorial objects according to their size. These algorithms can be classified into two main types: the recursive algorithms and those based on the use of a (next) procedure able to generate the successor of any object according to a fixed (usually lexicographic) order. In this paper we present some algorithms of both the above mentioned kinds for the exhaustive generation of three classes of lattice paths in the discrete plane: prefixes of Dyck, Motzkin and q-coloured Motzkin paths, also called Dyck, Motzkin and q-coloured Motzkin positive paths. Furthermore we show that the proposed algorithms have the CAT (Constant Amortized Time) property <ref type="bibr" target="#b21">[Rus03]</ref>, that is the average number of operations needed to generate any object of a given size does not depend on the size itself but it is limited by a constant.</p><p>We consider three kinds of steps U = (1, 1), D = (1, −1), F = (1, 0), called rise, fall and flat steps. An n-length Dyck (Motzkin) path is a path in Z 2 made up of U and D steps (U , D and F steps) starting from (0, 0), ending in (n, 0) and never going under the x-axis. If the flat steps can be coloured in q ≥ 2 different ways (that is we have q different flat steps F 1 , F 2 , ..., F q ) we obtain q-coloured Motzkin paths (called bicoloured when q = 2). Positive Dyck (Motzkin, q-coloured Motzkin) paths are prefixes of Dyck (Motzkin, q-coloured Motzkin) paths, that is paths having the above mentioned properties but ending at a point (n, h), where h ≥ 0 is called the height of the path.</p><p>We can establish a total order among the paths in the same class in a very natural way. If p = p 1 p 2 ...p n and r = r 1 r 2 ...r n are two paths we say that p precedes r if there is an index k &gt; 0 such that p i = r i for i = 1, ..., k − 1 and the step p k lies under the step r k (for the flat steps of different colours we can choose an order whatever, so we have D &lt; F 1 &lt; F 2 &lt; ... &lt; F q &lt; U ). We will define some algorithms able to exhaustively generate the paths having the same length according to the above order.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="2">Dyck prefixes</head><p>Dyck paths have been widely studied, both from a combinatorial and an algorithmic point of view, directly or in connection with the numerous combinatorial structures in bijection with them and enumerated by Catalan numbers. Well-formed parenthesis, binary trees, parallelogram polyominoes, triangulations of a polygon, chords on a circle, pattern avoiding permutations are only a few examples. The book of Stanley <ref type="bibr" target="#b24">[Sta15]</ref> presents 214 different kinds of objects counted by Catalan numbers and many others enumerated by sequences related to them. In the literature there exist also many papers dealing with exhaustive generation and Gray codes for structures enumerated by Catalan numbers (see for instance <ref type="bibr" target="#b16">[BR98,</ref><ref type="bibr" target="#b21">Rus03,</ref><ref type="bibr" target="#b22">RW08,</ref><ref type="bibr" target="#b25">VW06,</ref><ref type="bibr" target="#b26">Wal98]</ref>).</p><p>Dyck prefixes correspond to ballot sequences and the number of n-length Dyck prefixes is</p><formula xml:id="formula_0">d n = n n 2</formula><p>(sequence A001405 in <ref type="bibr">[S]</ref>). From this we can easily obtain that</p><formula xml:id="formula_1">d 2n = 2d 2n−1</formula><p>and</p><formula xml:id="formula_2">d 2n+1 = 2n + 1 n + 1 d 2n &lt; 2d 2n</formula><p>These relations have a very simple interpretation. Any odd length path has a strictly positive height, so we can add at its end either a rise or a fall step. As a result from any (2n − 1)-length path we obtain two 2n-length different paths. The even length paths include also 0-height paths, that is Dyck paths enumerated by Catalan numbers (C n ), and only a rise step can be added at the end of these paths. As a results from the 2n-length paths we obtain 2d 2n − C n different (2n + 1)-length paths.</p><p>In order to exhaustively generate the n-length paths according to the natural order above defined, we represent them by means of n-length words on the alphabet {0, 1}, by associating 1 to each rise step and 0 to each fall step, and then generate the words in lexicographic order.</p><p>The first n-length word is f irst(n) = (10) n 2 if n is even and f irst(n) = (10) n 2 1 if n is odd. The last one is always 1 n . Furthermore we define f irst(n) = ε when n ≤ 0. We denote by h(w) the difference between the number of 1's and the number of 0's in the word w, that is the height of the corresponding path. Let w = v0, then the following word w is v1 and h(w ) = h(w) + 2. If w = v01 p then w = v10 k z where k is the maximum number of 0's that can be added after v1, that is</p><formula xml:id="formula_3">k = min{p, h(w) − p + 2} because h(v1) = h(w) − p + 2, and z = f irst(p − k). Furthermore, h(w ) = h(w) − 2p + 2 if p &lt; h(w) − p + 2 and h(w ) = h(z) otherwise.</formula><p>As a result, in order to obtain the successor of a word w it is necessary to scan w from right to left till the rightmost 0 is detected and then modify w from this position to the end as described above. This can be achieved by the procedure next() (described in Algorithm 1 in a Java style). We suppose that a is a global array containing w in the entries 1..n, a[0] = 0 is a flag for recognizing the last word, h is a global variable containing the height of the path represented in a and f inish a global boolean variable whose value becomes true when the procedure recognizes the last sequence. After initializing a with f irst(n), h with h(f irst(n)) and f inish with f alse, the procedure is used into a do-while cycle which stops when f inish becomes true.</p><p>In order to evaluate the complexity of the algorithm it is easy to see that the number of elements tested and modified for generating the next sequence from w = v01 p is equal to p + 1. Let t n be the total number of final 1's in all the n-length words (that is the number of 1's which take part in the 1 p suffixes). The following relation Algorithm 1 procedure next for Dyck prefixes</p><formula xml:id="formula_4">void next ( ) { int j , p=0; while ( a [ n−p]==1) p=p+1; i f ( p==n ) f i n i s h=true ; e l s e { a [ n−p ] = 1 ; h=h−p+2; j=n−p+1; while ( h&gt;0 &amp;&amp; j&lt;=n ) { a [ j ] = 0 ; j=j +1; h=h−1;} while ( j &lt;n ) { a [ j ] = 1 ; a [ j +1]=0; j=j +2;} i f ( j==n ) {a [ n ] = 1 ; h=h+1;} } } holds t n = t n−1 + d n−1 because</formula><p>an element 1 can be added at the end of any (n − 1)-length word and the final 1's are still final 1's. So the total number of operations necessary to generate all the n-length words is t n + d n and the average number for each word is</p><formula xml:id="formula_5">avg n = t n + d n d n = t n d n + 1</formula><p>So the quantity r n = tn dn must be evaluated.</p><formula xml:id="formula_6">r 2n = t 2n d 2n = t 2n−1 + d 2n−1 2d 2n−1 = 1 2 (r 2n−1 + 1) r 2n+1 = t 2n+1 d 2n+1 = t 2n + d 2n 2n+1 n+1 d 2n = t 2n−1 + d 2n−1 + 2d 2n−1 2 2n+1 n+1 d 2n−1 = n + 1 2(2n + 1) (r 2n−1 + 3) If r 2n−1 &lt; 2 then r 2n+1 &lt; n + 1 2(2n + 1) 5 = 5n + 5 4n + 2 &lt; 2</formula><p>for n &gt; 1.</p><p>Since r 1 &lt; 2 we can conclude that r n &lt; 2 for n ≥ 1 and avg n &lt; 3, so the algorithm has the CAT property.</p><p>A recursive version can also be defined. The array a and h have the same meaning and do not necessitate any initialization. The array a is filled from left to right: the first parameter of the procedure dyckR is the next position j to be defined and the second parameter indicates the height of the Dyck prefix represented in the first j − 1 entries of a. If the value of the first parameter in a call is greater than n, a new prefix is contained in a and it can be displayed (or elaborated). Otherwise the prefix is not yet completed: a fall step is added (a[j] = 0) and a recursive call with parameters j + 1 and h − 1 is generated only if h &gt; 0, while a rise step can always be added (a[j] = 1) followed by a recursive call with parameters j + 1 and h + 1. The initial call is dyckR(1, 0).</p><p>In order to evaluate the complexity we can consider the total number of recursive calls made for generating all the n-length words, because any call makes a constant number of operations. We can slightly modify the procedure in such a way that any call outputs a new word or generate exactly two recursive calls. In this way the recursion tree is a binary tree with d n leafs and d n − 1 internal nodes and, as a result, we have that the average number of calls for a word is less than 2 and the algorithm is CAT. When we add a fall step, the height of the prefix decreases by 1. So if the height was 1 it becomes 0 and at a successive call only a rise step can be added. So when the height is equal to 1 and we add a fall step in position j, if j &lt; n, we add also a rise step in position j + 1 and make a call with parameters j + 2 and 1. As a result, the only calls with h = 0 are those with j = n and in the recursion tree any internal node has exactly two sons. In this case the initial call should Algorithm 2 recursive procedure for Dyck prefixes void dyckR ( int j , int h ) { i f ( j &gt;n ) { p r i n t ( a ) ; } e l s e { i f ( h&gt;0) { a [ j ] = 0 ; dyckR ( j +1, h −1);} a [ j ] = 1 ; dyckR ( j +1, h +1); } } Algorithm 3 improved recursive procedure for Dyck prefixes void dyckRC ( int j , int h ) { i f ( j &gt;n ) { p r i n t ( a ) ; } e l s e { a [ j ] = 0 ; i f ( h==1 &amp;&amp; j &lt;n ) { a [ j +1]=1; dyckRC ( j +2, 1 ) ; } e l s e dyckRC ( j +1, h −1); a [ j ] = 1 ; dyckRC ( j +1, h +1); } } be dyckRC(2, 1), after initializing a[1] = 1.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="3">Motzkin prefixes</head><p>Motzkin numbers [Aig98, DS77] enumerate many combinatorial structures which are very close to those enumerated by Catalan numbers <ref type="bibr" target="#b24">[Sta15]</ref>. Motzkin prefixes have been deeply studied mainly due to their relation with directed animals and percolation <ref type="bibr" target="#b6">[BPS94,</ref><ref type="bibr" target="#b19">GV88]</ref>. They are enumerated by sequence A005773 in <ref type="bibr">[S]</ref>. There is no closed formula for the number m n of n-length Motzkin prefixes, but for our sakes the recurrence relation (18) <ref type="bibr" target="#b5">[BPS91]</ref>:</p><formula xml:id="formula_7">m n = 2m n−1 + 3 n − 1 n + 1 m n−2 ,</formula><p>with the conditions m 0 = 1 and m 1 = 2, is sufficient to state that</p><formula xml:id="formula_8">m n &gt; 2m n−1 .</formula><p>In order to exhaustively generate the n-length Motzkin prefixes we represent them by means of n-length words on the alphabet {0, 1, 2}, by associating 2 to each rise step, 1 to each flat step and 0 to each fall step. In this way the natural order for the paths defined in the introduction corresponds to the lexicographic order on the words.</p><p>The first n-length word is 1 n and the last one is 2 n . We denote by h(w) the height of the path coded by w, that is the difference between the number of 2's and the number of 0's in the word w.</p><p>Let w = v0 (w = v1), then the following word w is v1 (w = v2) and h(w ) = h(w) + 1. If w = v02 p then w = v10 k 1 p−k where k is the maximum number of 0's that can be added after v1, that is k = min{p, h(w)−p+1} because h(v1) = h(w) − p + 1. Furthermore, h(w ) = h(w) − p + 1 − k. In an analogous way, if w = v12 p then w = v20 k 1 p−k where k is the maximum number of 0's that can be added after v2, that is k = min{p, h(w)−p+1} because h(v2) = h(w) − p + 1. In this case too, h(w</p><formula xml:id="formula_9">) = h(w) − p + 1 − k.</formula><p>As a result, it is necessary to scan a word w from right to left till the rightmost element different from 2 is detected and then modify w from this position to the end as described above, in order to obtain its successor. This can be achieved by the procedure next() (described in Algorithm 4 in a Java style). We suppose that a is a global array containing w in the entries 1..n, a[0] = 0 is useful for recognizing the last word, h is a global variable containing the height of the path represented in a and f inish a global boolean variable whose value becomes true when the procedure recognizes the last sequence. After initializing a with 1 n , h with 0 and f inish with f alse, the procedure is used into a do-while cycle which stops when f inish becomes true. In order to evaluate the complexity of the algorithm we remark that the number of elements tested and modified for generating the next sequence from w = v02 p or w = v12 p is equal to p + 1. Let t n be the total number of final 2's in all the n-length words (that is the number of 2's which take part in the 2 p suffixes). The following relation holds t n = t n−1 + m n−1 because an element 2 can be added at the end of any (n − 1)-length word and the final 2's are still final 2's. So the total number of operations necessary to generate all the n-length words is t n + m n and the average number for each word is</p><formula xml:id="formula_10">avg n = t n + m n m n</formula><p>It is easy to verify that t n &lt; m n . This is true for n = 1, and if we assume that</p><formula xml:id="formula_11">t k &lt; m k for k ≤ n − 1, we obtain t n = t n−1 + m n−1 &lt; 2m n−1 &lt; m n .</formula><p>As a result</p><formula xml:id="formula_12">avg n = t n + m n m n &lt; m n + m n m n = 2.</formula><p>So this algorithm too has the CAT property.</p><p>A recursive version can be defined in this case too. The array a and h have the same meaning and do not necessitate any initialization. The array a is filled from left to right: the first parameter of the procedure motzR is the next position j to be defined and the second parameter indicates the height of the Motzkin prefix represented in the first j − 1 entries of a. If the value of the first parameter in a call is greater than n, a new prefix is contained in a and it can be displayed (or elaborated). Otherwise the prefix is not yet completed: a fall step is added (a[j] = 0) and a recursive call with parameters j + 1 and h − 1 is generated only if h &gt; 0, while a flat and a rise step can always be added (a[j] = 1 and a[j] = 2) followed by a recursive call with parameters j + 1 and h for the flat step and parameters j +1 and h+1 for the rise step, respectively. The initial call is motzR(1, 0).</p><p>In order to evaluate the complexity we can consider the total number of recursive calls made for generating all the n-length words, because any call makes a constant number of operations. We remark that any call outputs a new word or generate at least two recursive calls. The recursion tree is a tree with m n leafs and a number of internal nodes less than m n − 1 and, as a result, the average number of calls for a word is less than 2 and the algorithm is CAT.</p><p>Algorithm 5 recursive procedure for Motzkin prefixes void motzR ( int j , int h ) { i f ( j &gt;n ) { p r i n t ( a ) ; } e l s e { i f ( h&gt;0) {a [ j ] = 0 ; motzR ( j +1, h −1);} a [ j ] = 1 ; motzR ( j +1, h ) ; a [ j ] = 2 ; motzR ( j +1, h +1); } } 4 q-coloured Motzkin prefixes q-coloured Motzkin paths <ref type="bibr" target="#b8">[BDPP95]</ref> are a natural generalization of Motzkin paths obtained by allowing q different colours be used for flat steps. When q = 2, bicoloured Motzkin paths are enumerated by Catalan numbers, while their prefixes are counted by the binomial coefficients d n = 2n+1 n+1 (sequence A001700 in <ref type="bibr">[S]</ref>). The discussion relative to Motzkin prefixes can be easily extended to the prefixes of q-coloured Motzkin paths. They can be represented by words on the alphabet Σ q = {0, 1, ..., q, q + 1} where q + 1 corresponds to a rise step, 0 to a fall step and 1, 2 ,..., q to the different flat steps. The first n-length word is 1 n and the last one (q + 1) n . If w = vx(q + 1) p , where x ∈ Σ q and x &lt; q + 1, then the successive word is w = vx 0 k 1 p−k , where x = x + 1 and k is the maximum number of 0's that can be added after vx . We have that h(vx ) = h(vx) = h(w) − p if 0 &lt; x &lt; q + 1, because we only change the colour of a flat step, and h(vx ) = h(vx) + 1 = h(w) − p + 1 otherwise, because we transform a fall step in a flat step or a flat step in a rise step. As a result, k = min{p, h(vx )}.</p><p>In order to obtain w it is again necessary to scan w from right to left till the rightmost element different from q + 1 is detected and then modify w starting from this position as described above. This is achieved by the procedure next() (Algorithm 6). The variables, their meaning and initialization and the use context of the procedure are the same as for Motzkin prefixes. As far as the complexity of the algorithm is concerned, we remark that the number of elements tested and modified for generating the next sequence from w = vx2 p , where x &lt; q + 1, is equal to p + 1. Let c n,q be the number of n-length q-coloured Motzkin prefixes. We have that c n,q &gt; (q + 1) • c n−1,q because a rise or any flat step can always be added to an (n − 1)-length q-coloured prefix.</p><p>The total number t n,q of final (q +1)'s in all the n-length words again satisfies the relation t n,q = t n−1,q +c n−1,q because an element (q + 1) can be added at the end of any (n − 1)-length word and the final (q + 1)'s are still final (q + 1)'s. So the total number of operations necessary to generate all the n-length words is t n,q + c n,q .</p><p>In this case too, we can verify that t n,q &lt; c n,q . This is true for n = 1, as a matter of fact t 1,q = 1 and c 1,q = q + 1. By assuming that t k,q &lt; c k,q for k ≤ n − 1, we obtain t n,q = t n−1,q + c n−1,q &lt; 2c n−1,q &lt; c n,q .</p><p>As a result the average number of elements tested and modified in to generate each word is avg n = t n,q + c n,q c n,q &lt; c n,q + c n,q c n,q = 2 and this algorithm too has the CAT property.</p><p>We can also define a recursive procedure with the same parameters used for Motzkin prefixes. This procedure (Algorithm 7) adds in position j, corresponding to the first parameter, a fall step (if the height h of the prefix so long defined is positive), a flat step of any colour, and a rise step. Obviously the recursion stops when j &gt; n.</p><p>Algorithm 7 recursive procedure for q-coloured Motzkin prefixes void qmotzR ( int j , int h ) { int i ; i f ( j &gt;n ) { p r i n t ( a ) ; } e l s e { i f ( h&gt;0) { a [ j ] = 0 ; qmotzR ( j +1, h −1);} f o r ( i =1; i&lt;=q ; i=i +1) { a [ j ]= i ; qmotzR ( j +1, h ) ; } a [ j ]=q+1; qmotzR ( j +1, h +1); } } Any call of such procedure produces a new sequence or generate at least q + 1 recursive calls. As a result this version too has the CAT property.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="5">Further developments</head><p>Recently binary words avoiding one or more patterns <ref type="bibr" target="#b13">[BMPP12]</ref> have been employed in order to construct sets of words constituting a non-overlapping code <ref type="bibr" target="#b15">[Bla15]</ref>, in particular Dyck words and Motzkin words were used in <ref type="bibr" target="#b14">[BPP12]</ref> and <ref type="bibr" target="#b4">[BBPPS16]</ref>. Their exhaustive generation and Gray code were tackled in [BBPSV14, BBPSV15, BBPV15, BBPV17]. It would be interesting to establish similar procedures for prefixes of words avoiding those patterns.</p></div><figure xmlns="http://www.tei-c.org/ns/1.0" xml:id="fig_0"><head>Algorithm 4</head><label>4</label><figDesc>procedure next for Motzkin prefixes void next ( ) { int j , p=0; while ( a [ n−p]==2) p=p+1; i f ( p==n ) f i n i s h=true ; e l s e { a [ n−p]=a [ n−p ] + 1 ; h=h−p+1; j=n−p+1; while ( h&gt;0 &amp;&amp; j&lt;=n ) { a [ j ] = 0 ; h=h−1; j=j +1;} while ( j&lt;=n ) {a [ j ] = 1 ; j=j +1;} } }</figDesc></figure>
<figure xmlns="http://www.tei-c.org/ns/1.0" xml:id="fig_1"><head>Algorithm 6</head><label>6</label><figDesc>procedure next for q-coloured Motzkin prefixes void next ( ) { int j , p=0; while ( a [ n−p]==q+1) p=p+1; i f ( p==n ) f i n i s h=true ; e l s e { a [ n−p]=a [ n−p ] + 1 ; h=h−p ; i f ( a [ n−p ] = = 1 | | a [ n−p]==q+1) h=h+1; j=n−p+1; while ( h&gt;0 &amp;&amp; j&lt;=n ) { a [ j ] = 0 ; h=h−1; j=j +1;} while ( j&lt;=n ) {a [ j ] = 1 ; j=j +1;} } }</figDesc></figure>
		</body>
		<back>

			<div type="acknowledgement">
<div xmlns="http://www.tei-c.org/ns/1.0"><head>Acknowledgements</head><p>This work was partially supported by the GNCS 2018 project "Proprietà combinatorie e rilevamento di pattern in strutture discrete lineari e bidimensionali".</p></div>
			</div>

			<div type="references">

				<listBibl>

<biblStruct xml:id="b0">
	<analytic>
		<title level="a" type="main">Motzkin numbers</title>
		<author>
			<persName><forename type="first">M</forename><surname>Aigner</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">European Journal of Combinatorics</title>
		<imprint>
			<biblScope unit="volume">19</biblScope>
			<biblScope unit="page" from="663" to="675" />
			<date type="published" when="1998">1998</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b1">
	<monogr>
		<title level="m" type="main">Random generation of trees -random generators in computer science</title>
		<author>
			<persName><forename type="first">L</forename><surname>Alonso</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Schott</surname></persName>
		</author>
		<imprint>
			<date type="published" when="1995">1995</date>
			<publisher>Kluwer</publisher>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b2">
	<analytic>
		<title level="a" type="main">Generating Random Permutations by Coin Tossing: Classical Algorithms, New Analysis, and Modern Implementation</title>
		<author>
			<persName><forename type="first">A</forename><surname>Bacher</surname></persName>
		</author>
		<author>
			<persName><forename type="first">O</forename><surname>Bodini</surname></persName>
		</author>
		<author>
			<persName><forename type="first">H.-K</forename><surname>Hwang</surname></persName>
		</author>
		<author>
			<persName><forename type="first">T.-H</forename><surname>Tsai</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">ACM Transactions on Algorithms</title>
		<imprint>
			<biblScope unit="volume">13</biblScope>
			<biblScope unit="page">43</biblScope>
			<date type="published" when="2017">2017</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b3">
	<analytic>
		<title level="a" type="main">Efficient random sampling of binary and unary-binary trees via holonomic equations</title>
		<author>
			<persName><forename type="first">A</forename><surname>Bacher</surname></persName>
		</author>
		<author>
			<persName><forename type="first">O</forename><surname>Bodini</surname></persName>
		</author>
		<author>
			<persName><forename type="first">A</forename><surname>Jacquot</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Theoretical Computer Science</title>
		<imprint>
			<biblScope unit="volume">695</biblScope>
			<biblScope unit="page" from="42" to="53" />
			<date type="published" when="2017">2017</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b4">
	<analytic>
		<title level="a" type="main">Cross-bifix-free sets generation via Motzkin paths</title>
		<author>
			<persName><forename type="first">E</forename><surname>Barcucci</surname></persName>
		</author>
		<author>
			<persName><forename type="first">S</forename><surname>Bilotta</surname></persName>
		</author>
		<author>
			<persName><forename type="first">E</forename><surname>Pergola</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
		<author>
			<persName><forename type="first">J</forename><surname>Succi</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">RAIRO -Theoretical Informatics and Applications</title>
		<imprint>
			<biblScope unit="volume">50</biblScope>
			<biblScope unit="page" from="81" to="91" />
			<date type="published" when="2016">2016</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b5">
	<analytic>
		<title level="a" type="main">The Motzkin family</title>
		<author>
			<persName><forename type="first">E</forename><surname>Barcucci</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Sprugnoli</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Pure Mathematics and Applications</title>
		<imprint>
			<biblScope unit="volume">2</biblScope>
			<biblScope unit="page" from="249" to="279" />
			<date type="published" when="1991">1991</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b6">
	<analytic>
		<title level="a" type="main">The random generation of directed animals</title>
		<author>
			<persName><forename type="first">E</forename><surname>Barcucci</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Sprugnoli</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Theoretetical Computer Science</title>
		<imprint>
			<biblScope unit="volume">127</biblScope>
			<biblScope unit="page" from="333" to="350" />
			<date type="published" when="1994">1994</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b7">
	<analytic>
		<title level="a" type="main">The random generation of underdiagonal walks</title>
		<author>
			<persName><forename type="first">E</forename><surname>Barcucci</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Sprugnoli</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Discrete Mathematics</title>
		<imprint>
			<biblScope unit="volume">139</biblScope>
			<biblScope unit="page" from="3" to="18" />
			<date type="published" when="1995">1995</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b8">
	<analytic>
		<title level="a" type="main">A Construction for Enumerating k-coloured Motzkin Paths</title>
		<author>
			<persName><forename type="first">E</forename><surname>Barcucci</surname></persName>
		</author>
		<author>
			<persName><forename type="first">A</forename><surname>Del Lungo</surname></persName>
		</author>
		<author>
			<persName><forename type="first">E</forename><surname>Pergola</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Lecture Notes in Computer Science</title>
		<imprint>
			<biblScope unit="volume">959</biblScope>
			<biblScope unit="page" from="254" to="263" />
			<date type="published" when="1995">1995</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b9">
	<analytic>
		<title level="a" type="main">Prefix partitioned Gray code for particular cross-bifix-free sets</title>
		<author>
			<persName><forename type="first">A</forename><surname>Bernini</surname></persName>
		</author>
		<author>
			<persName><forename type="first">S</forename><surname>Bilotta</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
		<author>
			<persName><forename type="first">A</forename><surname>Sabri</surname></persName>
		</author>
		<author>
			<persName><forename type="first">V</forename><surname>Vajnovszki</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Cryptography and Communications</title>
		<imprint>
			<biblScope unit="volume">6</biblScope>
			<biblScope unit="page" from="359" to="369" />
			<date type="published" when="2014">2014</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b10">
	<analytic>
		<title level="a" type="main">Gray code orders for q-ary words avoiding a given factor</title>
		<author>
			<persName><forename type="first">A</forename><surname>Bernini</surname></persName>
		</author>
		<author>
			<persName><forename type="first">S</forename><surname>Bilotta</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
		<author>
			<persName><forename type="first">A</forename><surname>Sabri</surname></persName>
		</author>
		<author>
			<persName><forename type="first">V</forename><surname>Vajnovszki</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Acta Informatica</title>
		<imprint>
			<biblScope unit="volume">52</biblScope>
			<biblScope unit="page" from="573" to="592" />
			<date type="published" when="2015">2015</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b11">
	<analytic>
		<title level="a" type="main">A trace partitioned Gray code for q-ary generalized Fibonacci strings</title>
		<author>
			<persName><forename type="first">A</forename><surname>Bernini</surname></persName>
		</author>
		<author>
			<persName><forename type="first">S</forename><surname>Bilotta</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
		<author>
			<persName><forename type="first">V</forename><surname>Vajnovszki</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Journal of Discrete Mathematical Sciences and Cryptography</title>
		<imprint>
			<biblScope unit="volume">18</biblScope>
			<biblScope unit="page" from="751" to="761" />
			<date type="published" when="2015">2015</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b12">
	<analytic>
		<title level="a" type="main">A Gray code for cross-bifix-free sets</title>
		<author>
			<persName><forename type="first">A</forename><surname>Bernini</surname></persName>
		</author>
		<author>
			<persName><forename type="first">S</forename><surname>Bilotta</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
		<author>
			<persName><forename type="first">V</forename><surname>Vajnovszki</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Mathematical Structures in Computer Science</title>
		<imprint>
			<biblScope unit="volume">27</biblScope>
			<biblScope unit="page" from="184" to="196" />
			<date type="published" when="2017">2017</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b13">
	<analytic>
		<title level="a" type="main">Pattern 1 j+1 0 j avoiding binary words</title>
		<author>
			<persName><forename type="first">S</forename><surname>Bilotta</surname></persName>
		</author>
		<author>
			<persName><forename type="first">D</forename><surname>Merlini</surname></persName>
		</author>
		<author>
			<persName><forename type="first">E</forename><surname>Pergola</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Fundamenta Informaticae</title>
		<imprint>
			<biblScope unit="volume">117</biblScope>
			<biblScope unit="page" from="35" to="55" />
			<date type="published" when="2012">2012</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b14">
	<analytic>
		<title level="a" type="main">A new approach to cross-bifix-free sets</title>
		<author>
			<persName><forename type="first">S</forename><surname>Bilotta</surname></persName>
		</author>
		<author>
			<persName><forename type="first">E</forename><surname>Pergola</surname></persName>
		</author>
		<author>
			<persName><forename type="first">R</forename><surname>Pinzani</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">IEEE Transactions on Information Theory</title>
		<imprint>
			<biblScope unit="volume">58</biblScope>
			<biblScope unit="page" from="4058" to="4063" />
			<date type="published" when="2012">2012</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b15">
	<analytic>
		<title level="a" type="main">Non-overlapping codes</title>
		<author>
			<persName><forename type="first">S</forename><surname>Blackburn</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">IEEE Transactions on Information Theory</title>
		<imprint>
			<biblScope unit="volume">61</biblScope>
			<biblScope unit="page" from="4890" to="4894" />
			<date type="published" when="2015">2015</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b16">
	<analytic>
		<title level="a" type="main">An Eades-McKay algorithm for well formed parantheses strings</title>
		<author>
			<persName><forename type="first">B</forename><surname>Bultena</surname></persName>
		</author>
		<author>
			<persName><forename type="first">F</forename><surname>Ruskey</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Information Processing Letters</title>
		<imprint>
			<biblScope unit="volume">68</biblScope>
			<biblScope unit="page" from="255" to="259" />
			<date type="published" when="1998">1998</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b17">
	<analytic>
		<title level="a" type="main">Motzkin numbers</title>
		<author>
			<persName><forename type="first">R</forename><surname>Donaghey</surname></persName>
		</author>
		<author>
			<persName><forename type="first">L</forename><surname>Shapiro</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Journal of Combinatorial Theory Series A</title>
		<imprint>
			<biblScope unit="volume">23</biblScope>
			<biblScope unit="page" from="291" to="301" />
			<date type="published" when="1977">1977</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b18">
	<analytic>
		<title level="a" type="main">Boltzmann samplers for the random generation of combinatorial structures</title>
		<author>
			<persName><forename type="first">P</forename><surname>Duchon</surname></persName>
		</author>
		<author>
			<persName><forename type="first">P</forename><surname>Flajolet</surname></persName>
		</author>
		<author>
			<persName><forename type="first">G</forename><surname>Louchard</surname></persName>
		</author>
		<author>
			<persName><forename type="first">G</forename><surname>Schaeffer</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Combinatorics Probability and Computing</title>
		<imprint>
			<biblScope unit="volume">13</biblScope>
			<biblScope unit="page" from="577" to="625" />
			<date type="published" when="2004">2004</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b19">
	<analytic>
		<title level="a" type="main">Equivalence of the two-dimensional directed animal problem to a one-dimensional path problem</title>
		<author>
			<persName><forename type="first">D</forename><surname>Gouyou-Beauchamps</surname></persName>
		</author>
		<author>
			<persName><forename type="first">G</forename><surname>Viennot</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Advances in Applied Mathematics</title>
		<imprint>
			<biblScope unit="volume">9</biblScope>
			<biblScope unit="page" from="334" to="357" />
			<date type="published" when="1988">1988</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b20">
	<monogr>
		<title level="m" type="main">The Art of Computer Programming</title>
		<author>
			<persName><forename type="first">D</forename><forename type="middle">E</forename><surname>Knuth</surname></persName>
		</author>
		<imprint>
			<date type="published" when="2008">2008</date>
			<publisher>Addison-Wesley</publisher>
			<biblScope unit="volume">4</biblScope>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b21">
	<monogr>
		<author>
			<persName><forename type="first">F</forename><surname>Ruskey</surname></persName>
		</author>
		<ptr target="http://www.1stworks.com/ref/RuskeyCombGen.pdf" />
		<title level="m">Combinatorial Generation</title>
				<imprint>
			<date type="published" when="2003">2003</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b22">
	<analytic>
		<title level="a" type="main">Generating Balanced Parentheses and Binary Trees by Prefix Shifts</title>
		<author>
			<persName><forename type="first">F</forename><surname>Ruskey</surname></persName>
		</author>
		<author>
			<persName><forename type="first">A</forename><surname>Williams</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="m">Proc. Fourteenth Computing: The Australasian Theory Symposium</title>
				<meeting>Fourteenth Computing: The Australasian Theory Symposium</meeting>
		<imprint>
			<date type="published" when="2008">2008</date>
			<biblScope unit="volume">77</biblScope>
			<biblScope unit="page" from="107" to="115" />
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b23">
	<monogr>
		<title level="m" type="main">The on-line encyclopedia of integer sequences</title>
		<author>
			<persName><forename type="first">N</forename><forename type="middle">J A</forename><surname>Sloane</surname></persName>
		</author>
		<ptr target="http://oeis.org" />
		<imprint/>
	</monogr>
</biblStruct>

<biblStruct xml:id="b24">
	<monogr>
		<title level="m" type="main">Catalan numbers</title>
		<author>
			<persName><forename type="first">R</forename><forename type="middle">P</forename><surname>Stanley</surname></persName>
		</author>
		<imprint>
			<date type="published" when="2015">2015</date>
			<publisher>Cambridge University Press</publisher>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b25">
	<analytic>
		<title level="a" type="main">A loop-free two-close algorithm for listing k-ary Dyck words</title>
		<author>
			<persName><forename type="first">V</forename><surname>Vajnovszki</surname></persName>
		</author>
		<author>
			<persName><forename type="first">T</forename><surname>Walsh</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Journal of Discrete Algorithms</title>
		<imprint>
			<biblScope unit="volume">4</biblScope>
			<biblScope unit="page" from="633" to="648" />
			<date type="published" when="2006">2006</date>
		</imprint>
	</monogr>
</biblStruct>

<biblStruct xml:id="b26">
	<analytic>
		<title level="a" type="main">Generation of well-formed parenthesis strings in constant worst-case time</title>
		<author>
			<persName><forename type="first">T</forename><surname>Walsh</surname></persName>
		</author>
	</analytic>
	<monogr>
		<title level="j">Journal of Algorithms</title>
		<imprint>
			<biblScope unit="volume">29</biblScope>
			<biblScope unit="page" from="165" to="173" />
			<date type="published" when="1998">1998</date>
		</imprint>
	</monogr>
</biblStruct>

				</listBibl>
			</div>
		</back>
	</text>
</TEI>
