sorting with cranes mike atkinson university of otago joint work with michael albert

34
Sorting with Cranes Sorting with Cranes Mike Atkinson University of Otago Joint work with Michael Albert

Upload: gwen-york

Post on 27-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Sorting with CranesSorting with Cranes

Mike Atkinson

University of Otago

Joint work with Michael Albert

QuestionQuestion

• 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.

Enter the forkstackEnter 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.

Moving from input to Moving from input to outputoutput

Input Forkstack Output

QuestionsQuestions

• Which rearrangements of an input sequence are possible?

• Alternatively, which input sequences can be sorted?

Sorting 236415Sorting 236415

1

2

5

3

6

4

Input OutputForkstack

Sorting 236415Sorting 236415

1 2

5 3

6

4

Input OutputForkstack

Sorting 236415Sorting 236415

1 2

5 3

64

Input OutputForkstack

Sorting 236415Sorting 236415

1 2

5 3 6

4

Input OutputForkstack

Sorting 236415Sorting 236415

1

2

5 3 6

4

Input OutputForkstack

Sorting 236415Sorting 236415

1

2

5

3 6

4

Input OutputForkstack

Sorting 236415Sorting 236415

1

2 5

3 6

4

Input OutputForkstack

Sorting 236415Sorting 236415

1

2 5

3 6

4

Input OutputForkstack

Sorting 236415Sorting 2364151

2

5

3

6

4

Input OutputForkstack

Sorting 236415Sorting 2364151

2

5

3

6

4

Input Output

“Sorted” means “increasing from top to bottom”

Forkstack

Which sequences are Which sequences are sortable?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 …

The dreaded 13The 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

Or is it?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.

Sorting 236415Sorting 236415

1

2

5

3

6

4

• 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)

Sorting 326415Sorting 326415

1

2

5

3

6

4

• 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.

Almost decreasing?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.

The algorithmThe 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

ModificationsModifications

• 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.

Being obstructiveBeing 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.

CountingCounting

• 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.

The forkstack in the The forkstack in the kitchenkitchen

• The awful truth

• Sometimes the kitchen has no automatic dishwasher

• Washing dishes has to be done by people!

Washing, drying, and Washing, drying, and stowingstowing

n - 2

n - 1

n

Dirty dishes Drying stack Orderly cupboard

wash stow

Forkstack with one-element pushes, multi-element pops

Washing, drying, and Washing, drying, and stowingstowing

1

2

Other dishes

Other dishes

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 -1Other dishes

Maximal t

Washing, drying, and Washing, drying, and stowingstowing

1

2

Sortable segment t

t

t -1

Sortable segment t-1

Maximal t

Sortable segment 1

Sortable segment 0

0 < 1 < … t

Washing, drying, and Washing, drying, and stowingstowing

f = 1 + xf + (f - 1)xtftt = 0

8

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

f = f(x) = cnxnn = 0

8Then

Geometric series

Employ weaker stowers?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?

Employ weaker stowers?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.

Other parametersOther 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.412n sequences of length n (so s=t=2 is more powerful than s=1, t= )

• If s= , t= we can sort at most 9n sequences of length n

Open questionOpen question

• Solve the counting problem for the case s = , t =