






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
01 - Pancake sorting - Great T
Typology: Lecture notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







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
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.
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^ ܣ
an upper bound Here is a simple algorithm for sorting an
݊ -stack:
ܯ^ in the stack.
ܯ^ to top.
ܯ^ to bottom.
(݊^ െ^ 1)-stack above
ܯ ܯ ܯ
Question. 18 How many flips?Letܶ (݊ ) be the max # flips on an
݊ -stack. Then: So:݊ܶ^ = 2 +
AAnswer.^ The # flips is always
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
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.݊^ ܲ
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
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 ܲܤ ^ ݊ଶ^
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