01 - Pancake Sorting, Lecture notes of Computer science

01 - Pancake sorting - Great T

Typology: Lecture notes

2025/2026

Uploaded on 05/02/2026

qwartz-brinz
qwartz-brinz 🇶🇦

20 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PANCAKE SORTING
THE PANCAKE-SORTING PUZZLE
The chef at our place is sloppy:
When he cooks pancakes,
they all come out at different sizes.
When the waiter delivers them
to a customer, he rearranges them
so that smallest is on top, and so on,
down to the largest at the bottom.
For this, he grabs several pancakes from
the top and flips them over; he keeps
repeating this until the full stack is sorted.
z
es.
m
e
d.
3
4
E
Example
1
.
.
Sort this stack. How many flips needed?
Answer.
Two flips are sufficient.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download 01 - Pancake Sorting and more Lecture notes Computer science in PDF only on Docsity!

PANCAKE SORTING T HE PANCAKE^ -S^ ORTING

PUZZLE

The chef at our place is sloppy:When he cooks pancakes,they all come out at different sizes.When the waiter delivers themto a customer, he rearranges themso that smallest is on top, and so on,down to the largest at the bottom.For this, he grabs several pancakes fromthe top and flips them over; he keepsrepeating this until the full stack is sorted.

zes.^ m^ ed.^ EExample 1..^ Sort this stack. How many flips needed? Answer.^ Two flips are sufficient.

5

EExample 2..^ Sort this stack. How many flips needed?Before we answer, let’s simplify our representation: replace each pancake by a number denoting its size

Answer.^ Four flips are

sufficient. Question.^ But are 4 flips also

5 2 3 4 1^ necessary?

(^142233324411555) (^143422 ) Question.^ But are 4 flips also

necessary? I.e., could there be a 3-flip way?AAnswer. No. Why? Suppose 3 are enough.Clearly, some flip must send 5 to bottom.st^ This must be the 1^ flip. (O.w., 1

st^ flip sends 5 somewhere inside; then 2

nd^ flip must send 5 to top, so that 3

rd^ flip can send it to bottom – result is not sorted.)st^ So, 1flip produces the stack

Now, some flip must send 4 on 5.This is possible only if some earlier flipsends 4 to top. So, 2

nd^ flip must send 4 to rd^ top, so that 3 flip can send 4 on 5.But stack still unsorted. Contradiction.

(^5213444311254132223514)^5^6

QQuestion.^ But are 4 flips also

necessary? I.e., could there be a 3-flip way? Answer. No. Why? Suppose 3 sufficient.Clearly, some flip must send 5 to bottom.st^ This must be the 1^ flip. (O.w., 1

st^ flip sends 5 somewhere inside; then 2

nd^ flip must send 5 to top, so that 3

rd^ flip can send it to bottom – result is not sorted.)st^ So, 1flip produces the stack

Now, some flip must send 4 on 5.This is possible only if some earlier flipsends 4 to top. So, 2

nd^ flip must send 4 to rd^ top, so that 3 flip can send 4 on 5.But stack still unsorted. Contradiction.

(^5213444311254132223514)^5^78 We have just proved our first theorem! Theorem.^ Four flips are

sufficient^ and necessary^ for sorting the pancake stack

Or, in a slightly more organized way: Theorem.^ Ifܺ^ is the smallest number offlips for sorting the pancake stack

, then: 4 ൑ ܺ൑^4

13

QQuestion.^ What is the value of

Theorem.ܲ^ = 5ହ^

.^ (We won’t prove it.) Question.^ How to prove such a claim?Answer.^ By a two-part proof: •^ We proveܲ^ ହ^

൑^5 , by describing a generic methodthat can sort any 5-stack with

൑^5 flips.

-^ We proveܲ^ ହ^

൒^5 , by finding a specific 5-stack

and arguing that no method sorts

ܵ in^ < 5^ flips. (Note: We have already shown

ܲ ൒^4 .)ହ^

14

As example of such proof, let’s see a simpler one: Theorem.ܲ^ = 3ଷ^

Proof.^ Because of the following: • ܲ^ ൑^3 .ଷ^ Consider this method for sorting a 3-stack:^ ƒ^ By^ ൑^2 flips, send largest pancake to bottom.^ ƒ^ By^ ൑^1 flip, send smallest pancake to top.Clearly, this uses

൑^3 flips.

  • ܲ^ ൒^3 .ଷ^ The stack^

needs 3 flips:Any method must send 3 to bottom.This needs 2 flips and results in

which is still unordered.

1.^ If largest not already at top,flip once to send it to top.2.^ Now flip once entire stackto send largest to bottom.1.^ If smallest not already at top,flip once to send it to top. same asܲ^ , but for 3-stacksହ d

More generally: Definition.^ For

th^ ݊ ൒ 1 , the ࢔ Pancake Number

is the number of flips sufficient and necessary forsorting a “hardest” stack of

݊ pancakes: QQuestion.^ Good upper/lower bounds for

ܲ maxmin௡ؔ^ ௌ^ ஺^

#flips when sorting

ܵ by^ ܣ

?^ ܲ൑^ ൑^ ?௡^ BOUNDING^ ࡼ^ ࢔

an upper bound Here is a simple algorithm for sorting an

݊ -stack:

  1. Find the largest pancake

ܯ^ in the stack.

  1. Flip right under it, to send

ܯ^ to top.

  1. Flip entire stack, to send

ܯ^ to bottom.

  1. Recurse on the

(݊^ െ^ 1)-stack above

ܯ ܯ ܯ

Question. 18 How many flips?Letܶ (݊ ) be the max # flips on an

݊ -stack. Then: So:݊ܶ^ = 2 +

݊ܶ െ^1 = 2 + 2 +݊ܶ^ െ^2 = 2 + 2 + 2 +^ ڮ^ + 2 +

= 2 + 2 + 2 +^

ڮ^ + 2 + 1

= 2݊^ െ^2 + 1 =

െ ݊ʹ^3.

AAnswer.^ The # flips is always

൑ ʹ݊ െ^3.

݊ܶ^ = 1 + 1 +

ܶ 2 = 1^ +^0

We just proved: Theorem.ܲ^ ൑ ʹ݊ െ௡^

3 , for all݊^ ൒^

Proof.^ By the recursive

“bring-to-top algorithm” and the calculations on the last two pages. Corollary.ܲ^ ൑ଷ^

So, this agrees with our earlier result that

ܲ = 3.ଷ^

Corollary.ܲ^ ൑ହ^

So, for݊^ = 5, our upper bound is

loose^ (asܲ^ = 5ହ^

It is still correct, of course; it is just not

tight.

So, we have proved: Theorem.݊^ ܲ൑

൑^2 ݊^ െ^3 ௡^

(݊^ ൒^4 ).

which does not nail it yet: upper bound isroughly twice as large as lower bound. •^ People have been trying to find

ܲ for௡^ specific݊^. Success up to

-^ We knowܲ^ ଶ଴

is either^23 or^24

, but nobody knows which! • Studying exhaustively all

20 -stacks would take for ever:^ 20! = 20^ ή^19 ή^18 ή ڮ ή

ଵ଼ 2 ή 1 = 2.43 ή 10 such stacks!

݊^ ࡼ࢔ (^4 45 56 77 88 99 1010 1111 1312 1413 1514 1615 1716 1817 1918 2019 22) ૛૙^?^25

WAIT. A^ RE^ YOU

SURE^ THIS^ IS

CS?

people who studied this puzzle Problem introduced in:^ ‰^ Harry Dweighter.

Elementary problemE2569. The American MathematicalMonthly 82(10):1010, 1975. Progress reported in:^ ‰^ W. Gates and C. Papadimitriou.

Bounds for sorting by prefix reversal

.^ Discrete Mathematics^ 27(1):47–57, 1979.were it was proved that:^ ଵ଻ܲ൑^ ௡^ ݊ଵ଺^

ହହ൑ +^ ݊ଷ^ ଷ

So, rather safe to call this CS!

s -

29

Current status of the original problem:^ ‰^ M. Heydari and H. Sudborough.

On the diameter of the pancake network.

Journal of Algorithms^ 25(1):67-94, 1997.^ ଵହ݊ଵସ^

‰^ Chitturi, Fahle, Meng, Morales, Shields, Sudborough,and Voit.^ An (18/11)n upper bound for sorting byprefix reversals.

Theoretical Computer Science 410(36):3372–3390, 2009.

30

A variation introduced by:^ ‰^ W. Gates and C. Papadimitriou.

Bounds for sorting by prefix reversal

.^ Discrete Mathematics^ 27(1):47–57, 1979.They proved:^ ଷെ^1 ܲܤ ൑^ ݊ଶ^

൑^2 ݊^ + 3௡

Improved to:^ ଷܲܤ ൑݊ଶ^

൑^2 ݊^ െ^2 ௡^ ‰ D. Cohen and M. Blum.^ On the problem ofsorting burnt pancakes.^ Discrete AppliedMathematics^ 61(2):105–120, 1995.

s

an application Sayܰ^ processors must connect, so that:^ •^ each connects to equally many others;^ •^ every two unconnected processorscan communicate via intermediaries;^ •^ this holds even if many processors crash;^ •^ total # of connections is small;^ •^ number of intermediaries is always small. Question.^ How to do this?

37

CCorollary.^ The^

diameter^ of a pancake network of sizeܰ^ isܱ^ (log

ܰ / log logܰ^ ). Proof.^ We know diameter is

ܲ൑^ ௡

ఱ and thatܲ ൑ (^) ௡ ݊య ఱ+ , where݊^ is య the smallest # such that

ܰ ݊൑^ !.

Hence, diameter is

ܱ (݊^ ) =ܱ^ (logܰ^

/ log logܰ^ ). ܰ^ =# processors^ rocessors݊^ = # pancakes/stack^ ancakes/stack

lessons

A couple of things we learned today: •^ Simple problems may be really hard to solve. •^ Viewed in the right way, simple problems mayhave far-reaching applications. •^ By studying pancakes, you can be a billionaire. •^ Computer Science is not just about computersand programming, it is about modeling the worldmathematically and finding better and betterways to solve problems. 38

“Computer science isno more about computers thanastronomy is about telescopes.”– Edsger W. Dijkstra