Document not found! Please try again

Pop-stacks in parallel - Science Direct

7 downloads 0 Views 70KB Size Report
PUSH operation transfers the next item of an input stream into the stack and the POP operation transfers the most recently pushed item from the stack to an.
Information Processing Letters 70 (1999) 63–67

Pop-stacks in parallel M.D. Atkinson a , J.-R. Sack b,∗ a School of Mathematical and Computational Sciences, North Haugh, St. Andrews, Fife, KY16 9SS, UK b Carleton University, School of Computer Science, 1125 Colonel By Drive, Ottawa, Ontario, Canada K1S 5B6

Received 1 January 1999; received in revised form 1 March 1999 Communicated by S.G. Akl

Abstract Pop-stacks are a stack-like container data type but the POP operation removes all the items on the stack. The permutations that can be sorted by a system of k pop-stacks in parallel are shown to be characterizable by a finite set of forbidden patterns and, for k = 2, a recurrence relation is found for their enumeration.  1999 Elsevier Science B.V. All rights reserved. Keywords: Algorithms; Computational complexity; Data structures; Parallel algorithms; Stacks

1. Introduction A stack may be viewed as a machine with two instructions POP and PUSH that transforms an input sequence of data items into an output sequence. The PUSH operation transfers the next item of an input stream into the stack and the POP operation transfers the most recently pushed item from the stack to an output stream. When the entire input stream has been transferred, via the stack, to the output stream, the output sequence will then be a permutation of the input sequence. The set of possible permutations which arise in this way characterizes the effect of a stack on its data and was first studied by Knuth [4]. Shortly afterwards Tarjan [7], Even and Itai [3], and Pratt [5] studied the effect of having several stacks either in series or in parallel. Since the actual values of the data items are unimportant (only that they be distinct) it is usual to ∗ Corresponding author. Email: [email protected]. The work of the second author was supported by the National Science and Engineering Research Council of Canada.

denote them by 1, 2, . . . , although occasionally other data sets need to be considered. It is convenient to formulate the central questions that arise in terms of sorting: (1) Which permutations can be sorted into increasing order? (2) Can the sortable permutations be characterized by a finite forbidden set of permutations? (3) How many permutations of each length n can be sorted? In the case of a single stack answers to these questions have been known for many years. Stack sortable permutations can be recognized in linear time, they are characterized by the single forbidden permutation [2,3,1], and there are 2n n /(n+1) sortable permutations of each length n. The case of k stacks in series is very much harder and no satisfactory answers are known for any of the three questions above for k > 2. In response to this difficulty Avis and Newborn [1] defined a less powerful object which they called a ‘pop-stack’. In a pop-stack PUSH operations are as usual but the

0020-0190/99/$ – see front matter  1999 Elsevier Science B.V. All rights reserved. PII: S 0 0 2 0 - 0 1 9 0 ( 9 9 ) 0 0 0 4 9 - 6

64

M.D. Atkinson, J.-R. Sack / Information Processing Letters 70 (1999) 63–67

POP operation unloads the entire stack (in the lastin, first-out manner). They introduced the respective terms feasible and m-feasible for those permutations that could be sorted by an unlimited number of pop-stacks in series and by a fixed number m of pop-stacks in series. Their main results comprised rather complex summation formulae to answer the enumeration problem for both the feasible and the mfeasible permutations. Very recently, in work which was not targeted at stacks, Bose et al. [2] introduced a class of permutations which they called separable. A permutation σ is separable if either (1) σ is the permutation [1], or (2) σ can be written as a concatenation αβ where either α is a separable permutation of the symbols 1, 2, . . . , m for some 1 6 m < n and β is a separable permutation of m + 1, . . . , n or β is a separable permutation of the symbols 1, 2, . . . , m for some 1 6 m < n and α is a separable permutation of m + 1, . . . , n. By comparing [1] and [2] it is easy to see that the separable permutations are precisely the feasible permutations and one can conclude that these permutations are characterized by the forbidden permutations [2, 4, 1, 3] and [3, 1, 4, 2]. It then follows from [6] that the feasible permutations are enumerated by the Schröder numbers. These numbers were originally defined as the number of ways of inserting nonoverlapping chords into a convex polygon but they also have various other interpretations (for more background on these numbers which occur in other generalizations of the stack structure see [4]). For the corresponding problem of k stacks in parallel the situation is rather different. Thanks to work of Unger [8,9] it is known that the sortable permutations can be recognized in time O(n log n) if k 6 3 but that recognition is NP-complete for larger values of k. In addition, from [7,3], it is known that, if k > 2, no finite set of forbidden permutations can characterize the sortable ones. Moreover the enumeration problem is unsolved for k > 2. Motivated by these results we shall consider k popstacks in parallel (see Fig. 1). We observe that, for any k, the recognition problem requires only linear time, and we prove, also for any k, that the sortable permutations are characterized by a finite forbidden set. Finally, we give an enumeration result for k = 2.

Fig. 1. Pop-stacks in parallel.

For clarity we use the term “MPOP” (multiple POP) for the operation of unloading an entire pop-stack. It is also notationally convenient to consider permutations which can be sorted into decreasing order. This, of course, produces an equivalent problem in which the symbols 1, 2, . . . , n are simply renamed n, n − 1, . . . , 1. So, from now on, we consider a system of k pop-stacks in parallel receiving data from an input permutation. At each stage of the sorting process the next input symbol is placed on one of the pop-stacks or the entire content of one of the pop-stacks is transferred to the output. The k-sortable permutations are those for which an appropriate sequence of PUSH and POP operations results in an output n, n − 1, . . . , 1. We begin by observing that if a permutation σ is k-sortable then there is a canonical series of PUSH and MPOP operations that carries out the sorting. In this canonical series a stack is always popped if we can thereby extend the output sequence legally; otherwise the next input symbol q is pushed either onto the unique stack whose top element is q − 1 or, if there is no such stack, onto an empty stack. To ascertain whether σ is k-sortable we simply carry out this algorithm. If it succeeds we have proved that σ is k-sortable; otherwise it will reach a state where no MPOP or PUSH is valid and we can conclude that σ is not k-sortable. Thus we have Theorem 1. The k-sortable permutations of length n can be recognized in linear time.

2. Forbidden sets In this section we prove

M.D. Atkinson, J.-R. Sack / Information Processing Letters 70 (1999) 63–67

Theorem 2. There exists a finite set Fk of permutations with the property that a permutation σ is ksortable if and only if no permutation of Fk appears as a pattern in σ . Proof. We shall examine the conditions that must hold if σ cannot be sorted by a canonical series of PUSH and MPOP operations. We shall see that σ must contain a bounded set of symbols that are ordered in one of a certain number of ways; and, conversely, any set of symbols ordered in one of these ways cannot be sorted. Suppose that the canonical series of PUSH and MPOP operations reaches a point where no further PUSH or MPOP can occur but has not terminated. All k stacks must be non-empty and we may number the stacks so that the ith stack has content bi . . . ti where (1) bi and ti are, respectively, the bottom and top elements of the ith stack; and bi . . . ti is a sequence of consecutive increasing integers; (2) ti−1 < bi for each i = 2, . . . , k. We let m denote the symbol which should next be output; note that m is not present in any of the stacks and so each bi must precede m in the input. When bi was stacked it was placed in an empty stack and therefore bi − 1 was not present in any of the stacks. Therefore bi must have preceded bi − 1 in the input (unless i = 1 and b1 = 1). Note that any input sequence on the set {b1 , . . . , bk , b1 − 1, . . . , bk − 1, m} (where the symbols are not necessarily distinct) with the property that b1 < b2 < · · · < bk , each bi precedes m, and, for i > 1, each bi precedes bi − 1 would lead to a situation where, before m was stacked, all stacks would contain exactly one of the bi . Now let q denote the next input symbol. Since m is not one of the top of stack symbols (i.e., m 6= tk ) m is either the symbol q or comes after q in the input. In addition, since q cannot be stacked, either q must precede q − 1 or q = 1. Conversely, if these conditions hold and none of the stacks is empty then no further operation is possible. We have proved that if a permutation σ is not ksortable then it contains a bounded set of symbols satisfying certain sequencing and ordering constraints; and conversely, if σ has a set of symbols with these constraints then it is not k-sortable. The (necessarily finite) set of permutations of these symbols that fulfill these constraints therefore has the property that one of

65

them occurs within a permutation σ if and only if σ is not k-sortable. 2 This proof is constructive and, in principle, allows the forbidden sets to be computed. For example, for k = 2, the minimal forbidden set is  [3, 2, 1, 4], [2, 1, 4, 3], [2, 4, 1, 3, 5], [4, 1, 3, 5, 2], [1, 4, 3, 5, 2], [1, 3, 5, 4, 2], [1, 3, 5, 2, 4] . 3. Enumeration In this section fn denotes the number of k-sortable permutations of length n. We shall mainly focus on the case k = 2 and therefore suppress notational references to k. However, we begin with a result, Lemma 3, that holds for every value of k. We define a permutation to be reduced if it does not contain any segment (i.e., a subsequence of consecutively placed symbols) of the form i, i + 1 and we let gn denote the number of reduced k-sortable permutations. Furthermore, if a1 . . . an is any sequence of distinct integers we define its normalized form to be the permutation which arises by relabelling the items with the elements of {1, . . . , n} so as to preserve order. For example, the normalized form of [4, 1, 6, 3] is [3, 1, 4, 2]. Lemma 3. fn =

n−1  m=1 m−1 gm .

Pn

Proof. Every sortable permutation of length n may be written as σ = [α1 , . . . , αm ] where each αi is a maximal segment of consecutive increasing integers of length `i . The permutation σ is uniquely determined by `1 , . . . , `m together with the permutation σ¯ that is the normalized form of any [a1, . . . , am ] where ai ∈ αi (the normalized form is independent of the choice of ai ∈ αi ). Note that σ¯ is reduced and that n−1  for a fixed reduced σ¯ there are m−1 permutations σ = [α1 , . . . , αm ] as the `i are allowed to vary subject to `1 + · · · + `m = n. The required equation now follows by allowing the length of σ¯ to vary over all possibilities. Lemma 4. If k = 2 the sequence (gn ) is defined by the conditions g1 = 1, g2 = 1, g3 = 3 and the recurrence gn = 3gn−1 − gn−2 + gn−3 .

66

M.D. Atkinson, J.-R. Sack / Information Processing Letters 70 (1999) 63–67

Proof. In the algorithm that sorts a reduced permutation σ , the first symbol to be output is n and just before this occurs both stacks will contain a sequence of consecutive integers increasing from bottom to top. One stack will contain n − s + 1, . . . , n for some s and the other x1 , x2 , . . . . These two sequences must be interleaved in some way to form an initial segment of σ and, since σ is reduced, the interleaving must be alternate. It follows that σ has an initial segment of one of the following forms: (A) σ = [x1 , n − s + 1, x2 , . . . , xs , n, xs+1 , y . . .] with s > 1, (B) σ = [n − s + 1, x2 , . . . , xs , n, xs+1 , y, . . .] with s > 1, (C) σ = [x1 , n − s + 1, x2, . . . , xs , n, y, . . .] with s > 1, (D) σ = [n − s + 1, x2 , . . . , xs,n, y, . . .] with s > 2. In all the above forms the element y denotes an integer that does not extend the increasing sequence of consecutive integers x1 , x2 , . . . . We now compute, for each fixed value of s, the numbers ans , bns , cns , dns of permutations of each of these forms. (A) For s > 1 the processes of inserting and removing the first two symbols give a one-to-one correspondence between permutations of this type and permutations [x2 , n − s + 2, x3 , . . . , xs , n, xs+1 , y, . . .] of the set {1, . . . , n} \ {x1 , n − s + 1}. A permutation of the latter type, when normalized, has the form [x1 , n − s, x2 , . . . , xs−1, n − 2, xs , y, . . .] and is reduced. This shows that ans = an−2,s−1 and therefore, by induction, ans = an−2s+2,1 . On the other hand, an1 enumerates permutations of the type [x1 , n, x2 , y, . . .] and these are in oneto-one correspondence with permutations [x1 , y, . . .] on the set {1, . . . , n} \ {n, x2 } (here the oneto-one correspondence is obtained by inserting and removing the second and third symbols). The latter permutations are again sortable, and reduced upon normalization; therefore there are gn−2 such permutations. Hence ans = an−2s+2,1 = gn−2s . (B) There is a one-to-one correspondence between permutations of type A and permutations [n − s +

1, x2 , . . . , xs , n, xs+1 , y, . . .] of the set {1, . . . , n}\ {x1 }. These permutations, when normalized, are of type B, but of length n − 1. Therefore ans = bn−1,s and hence bns = gn−2s+1 . (C) Suppose first that s > 2. Then there is a one-toone correspondence between permutations of this type and reduced sortable permutations [x2 , n − s + 2, x3 , . . . , xs , n, y, . . .] of the set {1, . . . , n} \ {x1 , n − s + 1} (these permutations do remain reduced when normalized since x2 < xs < n − s + 2). This proves that cns = cn−2,s−1 whenever s > 2 and therefore cns = cn−2s+4,2 . However cn2 enumerates permutations of the form [x1 , n − 1, x2 , n, y, . . .] and these are in oneto-one correspondence with permutations [x1 , y, . . .] of {1, . . . , n} \ {x2 , n − 1, n} which are also sortable and reduced when normalized. Thus cn2 = gn−3 and, for all s > 2, cns = gn−2s+1 . The above argument breaks down when s = 1 because, for the permutations [x1 , n, y, . . .] enumerated by cn1 , we have the extra condition that x1 6= n − 1. Thus these sequences are in one-toone correspondences with the reduced sortable permutations [x1 , y, . . .] of length n − 1 where x1 6= n − 1. Since the permutations which do have x1 = n − 1 are in one-to-one correspondence with reduced sortable sequences of length n − 2 it follows that cn1 = gn−1 − gn−2 . (D) The permutations of this type are in one-to-one correspondence with permutations [n − s + 2, x3 , . . . , xs , n, y, . . .] on {1, . . . , n} \ {n − s + 1, x2 } (so long as s > 2) and these are sortable and, on normalization, reduced. Hence dns = dn−2,s−1 = dn−2s+4,2 . However the permutations enumerated by dn2 are of the form [n − 1, x2 , n, y, . . .] which correspond to permutations [x2 , y, . . .] of length n − 2 and there are gn−2 of these. Thus dns = gn−2s+2 . We can now complete the proof. We have X gn = (ans + bns + cns + dn,s+1 ) s>1

=

X

(gn−2s + gn−2s+1 + gn−2s+1 + gn−2s )

s>1

+ gn−2 + gn−1 + gn−1 − gn−2 + gn−2

=2

n−1 X r=1

gn−r − gn−2 .

M.D. Atkinson, J.-R. Sack / Information Processing Letters 70 (1999) 63–67

By differencing we now obtain gn − gn−1 = 2gn−1 − gn−2 + gn−3 as required. 2 Theorem 5. If k = 2 the sequence (fn ) is defined by the conditions f1 = 1, f2 = 2, f3 = 6 and the recurrence fn = 6fn−1 − 10fn−2 + 6fn−3 . P Proof. By the previous lemma gm = i ai rim where r1 , r2 , r3 are the roots of x 3 − 3x 2 + x − 1 = 0 and a1 , a2 , a3 are constants. By Lemma 3  n  X n−1 X m ai ri fn = m−1 i m=1  n  X X n−1 m = ai r m−1 i i m=1 X = ai ri (1 + ri )n−1 i

(by interchanging the order of summations and applying the binomial theorem). The numbers 1 + ri satisfy the equation 0 = (x − 1)3 − 3(x − 1)2 + (x − 1) − 1 = x 3 − 6x 2 + 10x − 6 and the theorem follows. 2 Obtaining a similar enumeration theorem for fn in the case of 3 or more pop-stacks seems to be difficult since the form of the reduced permutations is more

67

complex. Nevertheless, we conjecture that, for any fixed k, fn satisfies a linear recurrence with constant coefficients. References [1] D. Avis, M. Newborn, On pop-stacks in series, Utilitas Math. 19 (1981) 129–140. [2] P. Bose, J.F. Buss, A. Lubiw, Pattern matching for permutations, Inform. Process. Lett. 65 (1998) 277–283. [3] S. Even, A. Itai, Queues, stacks and graphs, in: Z. Kohavi, A. Paz (Eds.), Theory of Machines and Computations, Proc. Internat. Symp. on the Theory of Machines and Computations, Technion – Israel Inst. of Technol., Haifa, Israel, August 1971, Academic Press, New York, 1971, pp. 71–86. [4] D.E. Knuth, Fundamental Algorithms, The Art of Computer Programming, Vol. 1, 2nd edn., Addison-Wesley, Reading, MA, 1973. [5] V.R. Pratt, Computing permutations with double-ended queues, parallel stacks and parallel queues, Proc. ACM Symp. Theory of Computing 5 (1973) 268–277. [6] L. Shapiro, A.B. Stephens, Bootstrap percolation, the Schröder number, and the N -kings problem, SIAM J. Discrete Math. 2 (1991) 275–280. [7] R.E. Tarjan, Sorting using networks of queues and stacks, J. ACM 19 (1972) 341–346. [8] W. Unger, The complexity of colouring circle graphs, in: Proc. 9th Annual Symposium on Theoretical Aspects of Computer Science, 1992, Springer Lecture Notes in Computer Sci., Vol. 577, Springer, Berlin, 1992, pp. 389–400. [9] W. Unger, On the k-colouring of circle graphs, in: Proc. 5th Annual Symposium on Theoretical Aspects of Computer Science, 1988, Lecture Notes in Computer Sci., Vol. 294, Springer, Berlin, 1988, pp. 61–72.

Suggest Documents