<s>
In	O
mathematics	O
and	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
stack-sortable	B-Algorithm
permutation	I-Algorithm
(	O
also	O
called	O
a	O
tree	O
permutation	B-Algorithm
)	O
is	O
a	O
permutation	B-Algorithm
whose	O
elements	O
may	O
be	O
sorted	B-Algorithm
by	O
an	O
algorithm	O
whose	O
internal	O
storage	O
is	O
limited	O
to	O
a	O
single	O
stack	B-Application
data	I-Application
structure	I-Application
.	O
</s>
<s>
The	O
stack-sortable	B-Algorithm
permutations	I-Algorithm
are	O
exactly	O
the	O
permutations	B-Algorithm
that	O
do	O
not	O
contain	O
the	O
permutation	B-Algorithm
pattern	I-Algorithm
231	O
;	O
they	O
are	O
counted	O
by	O
the	O
Catalan	O
numbers	O
,	O
and	O
may	O
be	O
placed	O
in	O
bijection	B-Algorithm
with	O
many	O
other	O
combinatorial	O
objects	O
with	O
the	O
same	O
counting	O
function	O
including	O
Dyck	O
paths	O
and	O
binary	O
trees	O
.	O
</s>
<s>
The	O
problem	O
of	O
sorting	B-Algorithm
an	O
input	O
sequence	O
using	O
a	O
stack	B-Application
was	O
first	O
posed	O
by	O
,	O
who	O
gave	O
the	O
following	O
linear	O
time	O
algorithm	O
(	O
closely	O
related	O
to	O
algorithms	O
for	O
the	O
later	O
all	B-Operating_System
nearest	I-Operating_System
smaller	I-Operating_System
values	I-Operating_System
problem	O
)	O
:	O
</s>
<s>
For	O
instance	O
,	O
the	O
sequence	O
3	O
,	O
2	O
,	O
1	O
is	O
correctly	O
sorted	B-Algorithm
:	O
the	O
three	O
elements	O
are	O
all	O
pushed	O
onto	O
the	O
stack	B-Application
,	O
and	O
then	O
popped	O
in	O
the	O
order	O
1	O
,	O
2	O
,	O
3	O
.	O
</s>
<s>
However	O
,	O
the	O
sequence	O
2	O
,	O
3	O
,	O
1	O
is	O
not	O
correctly	O
sorted	B-Algorithm
:	O
the	O
algorithm	O
first	O
pushes	O
2	O
,	O
and	O
pops	O
it	O
when	O
it	O
sees	O
the	O
larger	O
input	O
value	O
3	O
,	O
causing	O
2	O
to	O
be	O
output	O
before	O
1	O
rather	O
than	O
after	O
it	O
.	O
</s>
<s>
Because	O
this	O
algorithm	O
is	O
a	O
comparison	B-Algorithm
sort	I-Algorithm
,	O
its	O
success	O
or	O
failure	O
does	O
not	O
depend	O
on	O
the	O
numerical	O
values	O
of	O
the	O
input	O
sequence	O
,	O
but	O
only	O
on	O
their	O
relative	O
order	O
;	O
that	O
is	O
,	O
an	O
input	O
may	O
be	O
described	O
by	O
the	O
permutation	B-Algorithm
needed	O
to	O
form	O
that	O
input	O
from	O
a	O
sorted	B-Algorithm
sequence	O
of	O
the	O
same	O
length	O
.	O
</s>
<s>
Knuth	O
characterized	O
the	O
permutations	B-Algorithm
that	O
this	O
algorithm	O
correctly	O
sorts	O
as	O
being	O
exactly	O
the	O
permutations	B-Algorithm
that	O
do	O
not	O
contain	O
the	O
permutation	B-Algorithm
pattern	I-Algorithm
231	O
:	O
three	O
elements	O
x	O
,	O
y	O
,	O
and	O
z	O
,	O
appearing	O
in	O
the	O
input	O
in	O
that	O
respective	O
order	O
,	O
with	O
z	O
<	O
x	O
<	O
y	O
.	O
</s>
<s>
Moreover	O
,	O
he	O
observed	O
that	O
,	O
if	O
the	O
algorithm	O
fails	O
to	O
sort	O
an	O
input	O
,	O
then	O
that	O
input	O
cannot	O
be	O
sorted	B-Algorithm
with	O
a	O
single	O
stack	B-Application
.	O
</s>
<s>
As	O
well	O
as	O
inspiring	O
much	O
subsequent	O
work	O
on	O
sorting	B-Algorithm
using	O
more	O
complicated	O
systems	O
of	O
stacks	B-Application
and	O
related	O
data	O
structures	O
,	O
Knuth	O
's	O
research	O
kicked	O
off	O
the	O
study	O
of	O
permutation	B-Algorithm
patterns	I-Algorithm
and	O
of	O
permutation	B-Algorithm
classes	O
defined	O
by	O
forbidden	O
patterns	O
.	O
</s>
<s>
The	O
sequence	O
of	O
pushes	O
and	O
pops	O
performed	O
by	O
Knuth	O
's	O
sorting	B-Algorithm
algorithm	I-Algorithm
as	O
it	O
sorts	O
a	O
stack-sortable	B-Algorithm
permutation	I-Algorithm
form	O
a	O
Dyck	O
language	O
:	O
reinterpreting	O
a	O
push	O
as	O
a	O
left	O
parenthesis	O
and	O
a	O
pop	O
as	O
a	O
right	O
parenthesis	O
produces	O
a	O
string	O
of	O
balanced	O
parentheses	O
.	O
</s>
<s>
Moreover	O
,	O
every	O
Dyck	O
string	O
comes	O
from	O
a	O
stack-sortable	B-Algorithm
permutation	I-Algorithm
in	O
this	O
way	O
,	O
and	O
every	O
two	O
different	O
stack-sortable	B-Algorithm
permutations	I-Algorithm
produce	O
different	O
Dyck	O
strings	O
.	O
</s>
<s>
Stack-sortable	B-Algorithm
permutations	I-Algorithm
may	O
also	O
be	O
translated	O
directly	O
to	O
and	O
from	O
(	O
unlabeled	O
)	O
binary	O
trees	O
,	O
another	O
combinatorial	O
class	O
whose	O
counting	O
function	O
is	O
the	O
sequence	O
of	O
Catalan	O
numbers	O
.	O
</s>
<s>
A	O
binary	O
tree	O
may	O
be	O
transformed	O
into	O
a	O
stack-sortable	B-Algorithm
permutation	I-Algorithm
by	O
numbering	O
its	O
nodes	O
in	O
left-to-right	B-Algorithm
order	I-Algorithm
,	O
and	O
then	O
listing	O
these	O
numbers	O
in	O
the	O
order	O
they	O
would	O
be	O
visited	O
by	O
a	O
preorder	B-Algorithm
traversal	I-Algorithm
of	O
the	O
tree	O
:	O
the	O
root	O
first	O
,	O
then	O
the	O
left	O
subtree	O
,	O
then	O
the	O
right	O
subtree	O
,	O
continuing	O
recursively	O
within	O
each	O
subtree	O
.	O
</s>
<s>
In	O
the	O
reverse	O
direction	O
,	O
a	O
stack-sortable	B-Algorithm
permutation	I-Algorithm
may	O
be	O
decoded	O
into	O
a	O
tree	O
in	O
which	O
the	O
first	O
value	O
x	O
of	O
the	O
permutation	B-Algorithm
corresponds	O
to	O
the	O
root	O
of	O
the	O
tree	O
,	O
the	O
next	O
x1	O
values	O
are	O
decoded	O
recursively	O
to	O
give	O
the	O
left	O
child	O
of	O
the	O
root	O
,	O
and	O
the	O
remaining	O
values	O
are	O
again	O
decoded	O
recursively	O
to	O
give	O
the	O
right	O
child	O
.	O
</s>
<s>
Several	O
other	O
classes	O
of	O
permutations	B-Algorithm
may	O
also	O
be	O
placed	O
in	O
bijection	B-Algorithm
with	O
the	O
stack-sortable	B-Algorithm
permutations	I-Algorithm
.	O
</s>
<s>
For	O
instance	O
,	O
the	O
permutations	B-Algorithm
that	O
avoid	O
the	O
patterns	O
132	O
,	O
213	O
,	O
and	O
312	O
may	O
be	O
formed	O
respectively	O
from	O
the	O
stack-sortable	O
(	O
231-avoiding	O
)	O
permutations	B-Algorithm
by	O
reversing	O
the	O
permutation	B-Algorithm
,	O
replacing	O
each	O
value	O
x	O
in	O
the	O
permutation	B-Algorithm
by	O
n+1x	O
,	O
or	O
both	O
operations	O
combined	O
.	O
</s>
<s>
The	O
312-avoiding	O
permutations	B-Algorithm
are	O
also	O
the	O
inverses	O
of	O
the	O
231-avoiding	O
permutations	B-Algorithm
,	O
and	O
have	O
been	O
called	O
the	O
stack-realizable	O
permutations	B-Algorithm
as	O
they	O
are	O
the	O
permutations	B-Algorithm
that	O
can	O
be	O
formed	O
from	O
the	O
identity	O
permutation	B-Algorithm
by	O
a	O
sequence	O
of	O
push-from-input	O
and	O
pop-to-output	O
operations	O
on	O
a	O
stack	B-Application
.	O
</s>
<s>
As	O
noted	O
,	O
the	O
123-avoiding	O
and	O
321-avoiding	O
permutations	B-Algorithm
also	O
have	O
the	O
same	O
counting	O
function	O
despite	O
being	O
less	O
directly	O
related	O
to	O
the	O
stack-sortable	B-Algorithm
permutations	I-Algorithm
.	O
</s>
<s>
investigates	O
the	O
properties	O
of	O
stack-sortable	B-Algorithm
permutations	I-Algorithm
chosen	O
uniformly	O
at	O
random	O
among	O
all	O
such	O
permutations	B-Algorithm
of	O
a	O
given	O
length	O
.	O
</s>
<s>
The	O
expected	O
length	O
of	O
the	O
longest	O
descending	O
subsequence	O
in	O
such	O
a	O
permutation	B-Algorithm
is	O
,	O
differing	O
by	O
a	O
constant	O
factor	O
from	O
unconstrained	O
random	O
permutations	B-Algorithm
(	O
for	O
which	O
the	O
expected	O
length	O
is	O
approximately	O
)	O
.	O
</s>
<s>
The	O
expected	O
length	O
of	O
the	O
longest	O
ascending	O
sequence	O
differs	O
even	O
more	O
strongly	O
from	O
unconstrained	O
permutations	B-Algorithm
:	O
it	O
is	O
.	O
</s>
<s>
The	O
expected	O
number	O
of	O
values	O
within	O
the	O
permutation	B-Algorithm
that	O
are	O
larger	O
than	O
all	O
previous	O
values	O
is	O
only	O
,	O
smaller	O
than	O
its	O
logarithmic	O
value	O
for	O
unconstrained	O
permutations	B-Algorithm
.	O
</s>
<s>
And	O
the	O
expected	O
number	O
of	O
inversions	B-Algorithm
is	O
,	O
in	O
contrast	O
to	O
its	O
value	O
of	O
for	O
unconstrained	O
permutations	B-Algorithm
.	O
</s>
<s>
Every	O
permutation	B-Algorithm
defines	O
a	O
permutation	B-Algorithm
graph	O
,	O
a	O
graph	O
whose	O
vertices	O
are	O
the	O
elements	O
of	O
the	O
permutation	B-Algorithm
and	O
whose	O
edges	O
connect	O
pairs	O
of	O
elements	O
that	O
are	O
inverted	B-Algorithm
by	O
the	O
permutation	B-Algorithm
.	O
</s>
<s>
The	O
permutation	B-Algorithm
graphs	O
of	O
stack-sortable	B-Algorithm
permutations	I-Algorithm
are	O
trivially	O
perfect	O
.	O
</s>
<s>
For	O
each	O
element	O
i	O
of	O
a	O
permutation	B-Algorithm
p	O
,	O
define	O
bi	O
to	O
be	O
the	O
number	O
of	O
other	O
elements	O
that	O
are	O
to	O
the	O
left	O
of	O
and	O
greater	O
than	O
i	O
.	O
</s>
<s>
Then	O
p	O
is	O
stack-sortable	O
if	O
and	O
only	O
if	O
,	O
for	O
all	O
i	O
,	O
bibi	O
+	O
1≤1	O
.	O
</s>
<s>
uses	O
the	O
bijection	B-Algorithm
between	O
stack-sortable	B-Algorithm
permutations	I-Algorithm
and	O
binary	O
trees	O
to	O
define	O
a	O
numerical	O
rank	O
for	O
each	O
binary	O
tree	O
,	O
and	O
to	O
construct	O
efficient	O
algorithms	O
for	O
computing	O
the	O
rank	O
of	O
a	O
tree	O
(	O
"	O
ranking	O
"	O
)	O
and	O
for	O
computing	O
the	O
tree	O
with	O
a	O
given	O
rank	O
(	O
"	O
unranking	O
"	O
)	O
.	O
</s>
<s>
defined	O
two	O
edit	O
operations	O
on	O
permutations	B-Algorithm
:	O
deletion	O
(	O
making	O
a	O
permutation	B-Algorithm
pattern	I-Algorithm
)	O
and	O
its	O
inverse	O
.	O
</s>
<s>
Using	O
the	O
same	O
correspondence	O
between	O
trees	O
and	O
permutations	B-Algorithm
,	O
they	O
observed	O
that	O
these	O
operations	O
correspond	O
to	O
edge	O
contraction	O
in	O
a	O
tree	O
and	O
its	O
inverse	O
.	O
</s>
<s>
By	O
applying	O
a	O
polynomial	O
time	O
dynamic	B-Algorithm
programming	I-Algorithm
algorithm	O
for	O
edit	O
distance	O
in	O
trees	O
,	O
they	O
showed	O
that	O
the	O
edit	O
distance	O
between	O
two	O
stack-sortable	B-Algorithm
permutations	I-Algorithm
(	O
and	O
hence	O
also	O
the	O
longest	O
common	O
pattern	O
)	O
can	O
be	O
found	O
in	O
polynomial	O
time	O
.	O
</s>
<s>
This	O
technique	O
was	O
later	O
generalized	O
to	O
algorithms	O
for	O
finding	O
longest	O
common	O
patterns	O
of	O
separable	B-Algorithm
permutations	I-Algorithm
;	O
however	O
,	O
the	O
longest	O
common	O
pattern	O
problem	O
is	O
NP-complete	O
for	O
arbitrary	O
permutations	B-Algorithm
.	O
</s>
