Title: Sorting with Cranes
1Sorting with Cranes
- Mike Atkinson
- University of Otago
- Joint work with Michael Albert
2Question
- Why is a stack limited to pushing or popping one
item at a time?
- Algorithmically to implement last in-first out
behaviour. - Implementationally no reason at all.
- In fact, block pops may be more efficient than
single element pops in a linked list
implementation.
3Enter the forkstack
- Storage as for a stack
- Push a sequence of arbitrary length (but first
element winds up on top) - Pop a sequence of arbitrary length.
- Mental image a stack of boxes being manipulated
by a forklift or a crane.
4Moving from input to output
Input
Forkstack
Output
5Questions
- Which rearrangements of an input sequence are
possible? - Alternatively, which input sequences can be
sorted?
6Sorting 236415
Input
Output
Forkstack
7Sorting 236415
Input
Output
Forkstack
8Sorting 236415
Input
Output
Forkstack
9Sorting 236415
Input
Output
Forkstack
10Sorting 236415
Input
Output
Forkstack
11Sorting 236415
Input
Output
Forkstack
12Sorting 236415
Input
Output
Forkstack
13Sorting 236415
Input
Output
Forkstack
14Sorting 236415
Input
Output
Forkstack
15Sorting 236415
Sorted means increasing from top to bottom
Input
Output
Forkstack
16Which sequences are sortable?
- Concrete approach seek an algorithm that sorts
input sequences whenever this is possible. - Abstract approach seek a characterisation of
obstructions to sortability (bad configurations) - Or in between
17The dreaded 13
- If, in the stack, an element sits directly on top
of a larger, but not next larger, element, then
we cannot recover. - First abstract characterisation
- A sequence is unsortable if any schedule of
moves that does not produce bad output,
eventually produces the dreaded 13. - Not tremendously useful!
18Or is it?
- Use the idea of avoiding 13s at all costs as the
primary component of an algorithm for sorting. - Only extra ingredient doing output when you can
is never harmful.
19Sorting 236415
- The 23 at the top need never be split.
- In fact, they should never be split.
- We cant move it with the 6 because of the
dreaded 13 (36 in this case)
20Sorting 326415
- The 32 at the stop still need to be moved before
the 6. - But they should be moved one element at a time,
to reduce the chances of a later dreaded 13.
21Almost decreasing?
- A sequence is almost decreasing if it is
decreasing, except for some steps of 1. - Eg 14 12 8 9 5 2 3 4 1
- To avoid the dreaded 13 the maximal initial
almost decreasing subsequence of the input needs
to be moved before the next element. - As a block if non-consecutive.
- To make it increasing if consecutive.
22The algorithm
- repeat
- As much output as possible
- Move the a.d. head as above
- until input is empty
- if stack is empty
- Hurray!
- else
- Waah!
Linear time test for forkstack sortability
23Modifications
- We may wish to consider the case where either the
push, the pop, or both operations are limited in
size (dishwasher and stower) - No real need to change the algorithm, it may just
fail earlier when a move is required which
overloads an operations capacity.
24Being obstructive
- Any unsortable sequence contains a minimal
unsortable subsequence. - The algorithm implies that there are (to within
renaming) a finite number of such - examine the stack just before a crash
- determine what went wrong and why
- In fact, they are 35142, together with 45 of
length six, and 6 of length 7.
25Counting
- We wish to count the number of sortable
permutations of each length. - This allows us to compare the powers of various
forkstack models, - And to compare the sorting power of forkstacks
with other data structures.
26The forkstack in the kitchen
- The awful truth
- Sometimes the kitchen has no automatic dishwasher
- Washing dishes has to be done by people!
27Washing, drying, and stowing
Forkstack with one-element pushes, multi-element
pops
28Washing, drying, and stowing
When dish t is washed the drying stack is empty
When dish t-1 is washed the drying stack contains
t only
2
1
The intermediate dish segments are sortable
segments on ranges of values
29Washing, drying, and stowing
a0 lt a1 lt at
30Washing, drying, and stowing
This recursive decomposition allows the counting
problem to be solved in this case. Let cn be the
number of sortable permutations if the push
operation is of single items and the pop
operation is unlimited. Define
Geometric series
Then
31Employ weaker stowers?
- If the stower can move just one dish at a time,
thats sorting on an ordinary stack. - Powerful stowers discussed above.
- In between? The two cases above correspond to the
first term, and entire sum of the geometric
series. The intermediate cases impose a limit on
the size of t and are exactly the partial sums. - Should we pay for a powerful stower?
32Employ weaker stowers?
- The equations tell us that a stower who can lift
up to k dishes allows about akn dish sequences of
length n to be stowed in order - a14 a24.730 a34.919 a44.976
- a54.993 a64.998 .. a? 5
- Local improvements in the manipulation of data
rapidly lose their effectiveness.
33Other parameters
- We know about the sorting power of a forkstack if
pushes of s1 items and pops of any length t are
allowed. - What about other values of s and t?
- The only solved case is s2, t2. Here we can
sort about 5.412n sequences of length n (so st2
is more powerful than s1, t ? ) - If s ?, t ? we can sort at most 9n sequences of
length n
34Open question
- Solve the counting problem for the case s ?, t
?