Download presentation
Presentation is loading. Please wait.
Published byGwen York Modified over 9 years ago
1
Sorting with Cranes Mike Atkinson University of Otago Joint work with Michael Albert
2
Question 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.
3
Enter 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.
4
Moving from input to output InputForkstackOutput
5
Questions Which rearrangements of an input sequence are possible? Alternatively, which input sequences can be sorted?
6
Sorting 236415 125364 InputOutputForkstack
7
Sorting 236415 125364 InputOutputForkstack
8
Sorting 236415 125364 InputOutputForkstack
9
Sorting 236415 125364 InputOutputForkstack
10
Sorting 236415 125364 InputOutputForkstack
11
Sorting 236415 125364 InputOutputForkstack
12
Sorting 236415 125364 InputOutputForkstack
13
Sorting 236415 125364 InputOutputForkstack
14
Sorting 236415 125364 InputOutputForkstack
15
Sorting 236415 125364 InputOutput “Sorted” means “increasing from top to bottom” Forkstack
16
Which 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 …
17
The 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! 1 3
18
Or is it? Use the idea of avoiding 13’s at all costs as the primary component of an algorithm for sorting. Only extra ingredient: doing output when you can is never harmful.
19
Sorting 236415 125364 The 23 at the top need never be split. In fact, they should never be split. We can’t move it with the 6 because of the dreaded 13 (36 in this case)
20
Sorting 326415 125364 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.
21
Almost 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.
22
The 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
23
Modifications 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 operation’s capacity.
24
Being 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.
25
Counting 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.
26
The forkstack in the kitchen The awful truth Sometimes the kitchen has no automatic dishwasher Washing dishes has to be done by people!
27
Washing, drying, and stowing n - 2 n - 1 n Dirty dishesDrying stackOrderly cupboard washstow Forkstack with one-element pushes, multi-element pops
28
Washing, drying, and stowing 1 2 Other dishes When dish t is washed the drying stack is empty When dish t-1 is washed the drying stack contains t only The intermediate dish segments are sortable segments on ranges of values t t -1 Other dishes Maximal t
29
Washing, drying, and stowing 1 2 Sortable segment t t t -1 Sortable segment t-1 Maximal t Sortable segment 1 Sortable segment 0 0 < 1 < … t
30
Washing, drying, and stowing f = 1 + xf + (f - 1) x t f t t = 0 8 This recursive decomposition allows the counting problem to be solved in this case. Let c n be the number of sortable permutations if the push operation is of single items and the pop operation is unlimited. Define f = f(x) = c n x n n = 0 8 Then Geometric series
31
Employ weaker stowers? If the stower can move just one dish at a time, that’s 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?
32
Employ weaker stowers? The equations tell us that a stower who can lift up to k dishes allows about k n dish sequences of length n to be stowed in order 1 =4 2 =4.730 3 =4.919 4 =4.976 5 =4.993 6 =4.998 ….. =5 Local improvements in the manipulation of data rapidly lose their effectiveness.
33
Other parameters We know about the sorting power of a forkstack if pushes of s=1 items and pops of any length t are allowed. What about other values of s and t? The only solved case is s=2, t=2. Here we can sort about 5.412 n sequences of length n (so s=t=2 is more powerful than s=1, t= ) If s= , t= we can sort at most 9 n sequences of length n
34
Open question Solve the counting problem for the case s = , t =
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.