# pp2 - 2. C OUNTING Now that we know which permutations can...

This preview shows pages 1–2. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2. C OUNTING Now that we know which permutations can sorted with our two simple sorting machines, how many (of some length n ) can be sorted? As we will see, surprisingly, both machines can sort the same number of permutations of any length. 2.1. S TACK W ORDS In the previous lecture, we proved that there is precisely one way to sort every stack-sortable permutation, given by the canonical stack sorting al- gorithm. We now create, for each stack-sortable permutation, a stack word which describes how to sort it. At each step of the canonical stack sorting algorithm, we write s if we perform a pu s h, and p if we perform a p op. As each entry must be both pushed and popped, the stack word for a permuta- tion of length n will always have length 2 n (unless the permutation cant be sorted, in which case it doesnt have a stack word). For example, the stack word for the permutation 2143 shown in Figure 1.1 is ssppsspp . Not all words 1 over the alphabet s , p describe stack sortings of a per- mutation. For example, spps does not describe a stack sorting, because there is nothing on the stack for the second p to pop. Generalizing this counterex- ample a bit, let us say that the stack word w is valid if it contains the same number of pushes ( s letters) and pops ( p letters) and for every i , the prefix w 1 w 2 w i contains at least as many pushes as pops. Our aim is to count stack-sortable permutations by counting valid stack words, so first we must verify the following. Proposition 2.1. Every valid stack word sorts some permutation. Proof. To prove this, we run everything backwards. Suppose that w is a valid stack word of length 2 n . We begin with the identity on the left of the stack (note that this means that the entry closest to the stack is n ), and we read w backwards. When we read a p , we push the rightmost entry from the output onto the stack. When we read an s , we pop the topmost entry on the stack onto the input. Because w is a valid stack word, every prefix w 1 w 2 w i contains at least as many pushes as pops, so since w taken altogether contains pre- cisely as many pushes as pops, this means that every suffix w i 1 w i 2 w 2 n contains at least as many pops as pushes. Therefore, in running the sorting backwards as described above, when we come to an s , there is 1 A word over an alphabet A is simply a finite sequence of members of A ....
View Full Document

## pp2 - 2. C OUNTING Now that we know which permutations can...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online