Pancake sorting

{{short description|Mathematics problem}}

{{Use mdy dates|date=October 2014}}

File:Pancake sort operation.png

Pancake sorting is the mathematical problem of sorting a disordered stack of pancakes in order of size when a spatula can be inserted at any point in the stack and used to flip all pancakes above it. A pancake number is the minimum number of flips required for a given number of pancakes. In this form, the problem was first discussed by American geometer Jacob E. Goodman.{{cite news|first=Simon |last=Singh |authorlink=Simon Singh | title=Flipping pancakes with mathematics|newspaper=The Guardian| date=November 14, 2013| url=https://www.theguardian.com/science/blog/2013/nov/14/flipping-pancakes-mathematics-jacob-goodman| access-date=March 25, 2014}} A variant of the problem is concerned with burnt pancakes, where each pancake has a burnt side and all pancakes must, in addition, end up with the burnt side on bottom.

All sorting methods require pairs of elements to be compared. For the traditional sorting problem, the usual problem studied is to minimize the number of comparisons required to sort a list. The number of actual operations, such as swapping two elements, is then irrelevant. For pancake sorting problems, in contrast, the aim is to minimize the number of operations, where the only allowed operations are reversals of the elements of some prefix of the sequence. Now, the number of comparisons is irrelevant.

The pancake problems

=The original pancake problem=

The minimum number of flips required to sort any stack of {{math|n}} pancakes has been shown to lie between {{math|{{sfrac|15|14}}n}} and {{math|{{sfrac|18|11}}n}} (approximately 1.07n and 1.64n), but the exact value is not known.{{Cite book|last1=Fertin|first1=G.|last2=Labarre|first2=A.|last3=Rusu|first3=I.|last4=Tannier|first4=E.|last5=Vialette|first5=S. |title=Combinatorics of Genome Rearrangements|publisher= The MIT Press|year= 2009|isbn=9780262062824}}

The simplest pancake sorting algorithm performs at most {{math|2n − 3}} flips. In this algorithm, a kind of selection sort, we bring the largest pancake not yet sorted to the top with one flip; take it down to its final position with one more flip; and repeat this process for the remaining pancakes.

In 1979, Bill Gates and Christos Papadimitriou gave a lower bound of {{math|{{sfrac|17|16}}n}} (approximately 1.06n) flips and an upper bound of {{math|{{sfrac|(5n+5)|3}}}}. The upper bound was improved, thirty years later, to {{math|{{sfrac|18|11}}n}} by a team of researchers at the University of Texas at Dallas, led by Founders Professor Hal Sudborough.{{cite web|title=Team Bests Young Bill Gates With Improved Answer to So-Called Pancake Problem in Mathematics|publisher=University of Texas at Dallas News Center|date=September 17, 2008|url=http://www.utdallas.edu/news/2008/09/17-002.php?WT.mc_id=NewsEmails&WT.mc_ev=EmailOpen|access-date=November 10, 2008|quote=A team of UT Dallas computer science students and their faculty adviser have improved upon a longstanding solution to a mathematical conundrum known as the pancake problem. The previous best solution, which stood for almost 30 years, was devised by Bill Gates and one of his Harvard instructors, Christos Papadimitriou, several years before Microsoft was established.|archive-url=https://web.archive.org/web/20120214070902/http://www.utdallas.edu/news/2008/09/17-002.php?WT.mc_id=NewsEmails&WT.mc_ev=EmailOpen|archive-date=February 14, 2012|url-status=dead}}{{Cite journal|last1=Chitturi|first1=B.|last2=Fahle|first2=W.|last3=Meng|first3=Z.|last4=Morales|first4=L.|last5=Shields|first5=C. O.|last6=Sudborough|first6=I. H.|last7=Voit|first7=W.|date=2009-08-31|title=An (18/11)n upper bound for sorting by prefix reversals|journal=Theoretical Computer Science|series=Graphs, Games and Computation: Dedicated to Professor Burkhard Monien on the Occasion of his 65th Birthday|volume=410|issue=36|pages=3372–3390|doi=10.1016/j.tcs.2008.04.045|doi-access=free}}

In 2011, Laurent Bulteau, Guillaume Fertin, and Irena Rusu{{Cite journal|journal=Journal of Computer and System Sciences|doi=10.1016/j.jcss.2015.02.003|title=Pancake Flipping Is Hard|last1=Bulteau|first1=Laurent|last2=Fertin|first2=Guillaume|last3=Rusu|first3=Irena|volume=81|number=8|pages=1556–1574|arxiv=1111.0434|year=2015}} proved that the problem of finding the shortest sequence of flips for a given stack of pancakes is NP-hard, thereby answering a question that had been open for over three decades.

=The burnt pancake problem=

In a variation called the burnt pancake problem, the bottom of each pancake in the pile is burnt, and the sort must be completed with the burnt side of every pancake down. It is a signed permutation, and if a pancake i is "burnt side up" a negative element i` is put in place of i in the permutation. In 2008, a group of undergraduates built a bacterial computer that can solve a simple example of the burnt pancake problem by programming E. coli to flip segments of DNA which are analogous to burnt pancakes. DNA has an orientation (5' and 3') and an order (promoter before coding). Even though the processing power expressed by DNA flips is low, the high number of bacteria in a culture provides a large parallel computing platform. The bacteria report when they have solved the problem by becoming antibiotic resistant.{{Cite journal|doi=10.1186/1754-1611-2-8|title=Engineering bacteria to solve the Burnt Pancake Problem|year=2008|last1=Haynes|first1=Karmella A|last2=Broderick|first2=Marian L|last3=Brown|first3=Adam D|last4=Butner|first4=Trevor L|last5=Dickson|first5=James O|last6=Harden|first6=W Lance|last7=Heard|first7=Lane H|last8=Jessen|first8=Eric L|last9=Malloy|first9=Kelly J|journal=Journal of Biological Engineering|volume=2|pages=8|pmid=18492232|pmc=2427008|last10=Ogden|first10=Brad J|last11=Rosemond|first11=Sabriya|last12=Simpson|first12=Samantha|last13=Zwack|first13=Erin|last14=Campbell|first14=A Malcolm|last15=Eckdahl|first15=Todd T|last16=Heyer|first16=Laurie J|author16-link= Laurie Heyer |last17=Poet|first17=Jeffrey L |doi-access=free }}

The pancake problem on strings

The discussion above presumes that each pancake is unique, that is, the sequence on which the prefix reversals are performed is a permutation. However, "strings" are sequences in which a symbol can repeat, and this repetition may reduce the number of prefix reversals required to sort. Chitturi and Sudborough (2010) and Hurkens et al. (2007) independently showed that the complexity of transforming a compatible string into another with the minimum number of prefix reversals is NP-complete. They also gave bounds for the same. Hurkens et al. gave an exact algorithm to sort binary and ternary strings. Chitturi{{cite journal|journal=Discrete Mathematics, Algorithms and Applications|volume= 03| issue = 3|pages=269–286|doi=10.1142/S1793830911001206|year=2011|last1=Chitturi|first1=Bhadrachalam|title=A Note on Complexity of Genetic Mutations }} (2011) proved that the complexity of transforming a compatible signed string into another with the minimum number of signed prefix reversals—the burnt pancake problem on strings—is NP-complete.

History

The pancake sorting problem was first posed by Jacob E. Goodman, writing under the pseudonym "Harry Dweighter" ("harried waiter").{{citation

|first1=Harry|last1=Dweighter

|title= Elementary Problem E2569

|journal = Amer. Math. Monthly

|volume= 82

|issue=10

|pages=1009–1010

|year=1975

|doi=10.2307/2318260|jstor=2318260

}}

Although seen more often as an educational device, pancake sorting also appears in applications in parallel processor networks, in which it can provide an effective routing algorithm between processors.{{Cite journal| last1 = Gargano | first1 = L.

| last2 = Vaccaro | first2 = U.

| last3 = Vozella | first3 = A.

| doi = 10.1016/0020-0190(93)90043-9

| issue = 6

| journal = Information Processing Letters

| mr = 1216942

| pages = 315–320

| title = Fault tolerant routing in the star and pancake interconnection networks

| volume = 45

| year = 1993| citeseerx = 10.1.1.35.9056

}}.{{Cite book| last1 = Kaneko | first1 = K.

| last2 = Peng | first2 = S.

| contribution = Disjoint paths routing in pancake graphs

| doi = 10.1109/PDCAT.2006.56

| pages = 254–259

| title = Proceedings of Seventh International Conference on Parallel and Distributed Computing, Applications and Technologies, 2006 (PDCAT '06)

| year = 2006| isbn = 978-0-7695-2736-9 | s2cid = 18777751

}}.

The problem is notable as the topic of the only well-known mathematics paper by Microsoft founder Bill Gates (as William Gates), entitled "Bounds for Sorting by Prefix Reversal" and co-authored with Christos Papadimitriou. Published in 1979, it describes an efficient algorithm for pancake sorting.{{cite journal |title=Bounds for Sorting by Prefix Reversal |journal=Discrete Mathematics |volume=27 |pages=47–57 |year=1979 |doi=10.1016/0012-365X(79)90068-2 |last1=Gates |first1=W. |last2=Papadimitriou |first2=C. |author-link1=Bill Gates |author-link2=Christos Papadimitriou |doi-access=free }} In addition, the most notable paper published by Futurama co-creator David X. Cohen (as David S. Cohen), co-authored with Manuel Blum, concerned the burnt pancake problem.{{Cite journal|last1=Cohen|first1=D.S. |last2= Blum|first2=M.|doi=10.1016/0166-218X(94)00009-3|title=On the problem of sorting burnt pancakes|year=1995|journal=Discrete Applied Mathematics|volume=61|issue=2|pages=105|author-link1=David X. Cohen|author-link2=Manuel Blum|doi-access=free}}

The connected problems of signed sorting by reversals and sorting by reversals were also studied more recently. Whereas efficient exact algorithms have been found for the signed sorting by reversals,{{cite journal|last1=Kaplan|first1=H.|last2=Shamir|first2=R.|author3-link=Robert Tarjan|last3=Tarjan|first3=R.E.|title=Faster and Simpler Algorithm for Sorting Signed Permutations by Reversals|journal=Proc. 8th ACM-SIAM SODA|date=1997|pages=178–87}} the problem of sorting by reversals has been proven to be hard even to approximate to within certain constant factor,{{cite journal|last1=Berman|first1=P.|author2-link=Marek Karpinski|last2=Karpinski|first2=M.|url=http://theory.cs.uni-bonn.de/ftp/reports/cs-reports/1998/85193-CS.ps.gz|title=On Some Tighter Inapproximability Results.|journal=Proc. 26th ICALP (1999) |series=Lecture Notes in Computer Science |volume=1644|year=1999|pages=200–09}} and also proven to be approximable in polynomial time to within the approximation factor 1.375.{{cite journal|last1=Berman|first1=P.|author2-link=Marek Karpinski|last2=Karpinski|first2=M.|last3=Hannenhalli|first3=S.|url=http://theory.cs.uni-bonn.de/ftp/reports/cs-reports/2001/85228-CS.ps.gz|title=1.375-Approximation Algorithms for Sorting by Reversals.|journal=Proc. 10th ESA (2002) |series=Lecture Notes in Computer Science |volume=2461|pages=200–10|year=2002}}

Pancake graphs

File:Pancake graph g3.svg

File:Pancake graph g4.svg

{{main|Pancake graph}}

An n-pancake graph is a graph whose vertices are the permutations of n symbols from 1 to n and its edges are given between permutations transitive by prefix reversals. It is a regular graph with n! vertices, its degree is n−1. The pancake sorting problem and the problem to obtain the diameter of the pancake graph are equivalent.{{cite conference

| last1 = Asai | first1 = Shogo

| last2 = Kounoike | first2 = Yuusuke

| last3 = Shinano | first3 = Yuji

| last4 = Kaneko | first4 = Keiichi

| editor1-last = Nagel | editor1-first = Wolfgang E.

| editor2-last = Walter | editor2-first = Wolfgang V.

| editor3-last = Lehner | editor3-first = Wolfgang

| contribution = Computing the diameter of 17-pancake graph using a PC cluster

| doi = 10.1007/11823285_117

| pages = 1114–1124

| publisher = Springer

| series = Lecture Notes in Computer Science

| title = Euro-Par 2006, Parallel Processing, 12th International Euro-Par Conference, Dresden, Germany, August 28 – September 1, 2006, Proceedings

| volume = 4128

| year = 2006| isbn = 978-3-540-37783-2

}}

The pancake graph of dimension n, Pn can be constructed recursively from n copies of Pn−1, by assigning a different element from the set {1, 2, …, n} as a suffix to each copy.

Their girth:

:g(P_n) = 6 \text{, if } n>2.

The γ(Pn) genus of Pn is:{{Cite journal|last1=Nguyen|first1=Quan|last2=Bettayeb|first2=Said|date=2009-11-05|title=On the Genus of Pancake Network.|url=http://ccis2k.org/iajit/PDF/vol.8,no.3/1247.pdf |journal=The International Arab Journal of Information Technology |volume=8|issue=3|pages=289–292}}

:n!\left( \frac{n-4}{6} \right)+1 \le \gamma(P_n) \le n!\left( \frac{n-3}{4} \right)-\frac{n}{2}+1

Since pancake graphs have many interesting properties such as symmetric and recursive structures, small degrees and diameters compared against the size of the graph, much attention is paid to them as a model of interconnection networks for parallel computers.{{Cite journal|last1=Akl|first1=S.G.|last2=Qiu|first2=K.|last3=Stojmenović|first3=I.|date=1993|title=Fundamental algorithms for the star and pancake interconnection networks with applications to computational geometry.|journal=Networks|volume=23|issue=4|pages=215–225|doi=10.1002/net.3230230403|citeseerx=10.1.1.363.4949}}{{Cite journal|last1=Bass|first1=D.W.|last2=Sudborough|first2=I.H.|date=March 2003|title=Pancake problems with restricted prefix reversals and some corresponding Cayley networks.|journal=Journal of Parallel and Distributed Computing |volume=63|issue=3|pages=327–336|doi=10.1016/S0743-7315(03)00033-9|citeseerx=10.1.1.27.7009}}{{Cite journal|last1=Berthomé|first1=P.|last2=Ferreira|first2=A.|last3=Perennes|first3=S.|date=December 1996|title=Optimal information dissemination in star and pancake networks.|journal=IEEE Transactions on Parallel and Distributed Systems|volume=7|issue=12|pages=1292–1300|doi=10.1109/71.553290|citeseerx=10.1.1.44.6681}} When we regard the pancake graphs as the model of the interconnection networks, the diameter of the graph is a measure that represents the delay of communication.{{cite book|last1=Kumar|first1=V.|last2=Grama|first2=A.|last3=Gupta|first3=A.|last4=Karypis|first4=G.|title=Introduction to Parallel Computing: Design and Analysis of Algorithms|publisher=Benjamin/Cummings|date=1994}}{{cite book|last=Quinn|first=M.J.|title=Parallel Computing: Theory and Practice|edition=second|publisher=McGraw-Hill|date=1994}}

The pancake graphs are Cayley graphs (thus are vertex-transitive) and are especially attractive for parallel processing. They have sublogarithmic degree and diameter, and are relatively sparse (compared to e.g. hypercubes).

Algorithm

An example of the pancake sorting algorithm is given below in Python. The code is similar to bubble sort or selection sort.

def flip(arr, k: int) -> None:

left = 0

while left < k:

arr[left], arr[k] = arr[k], arr[left]

k -= 1

left += 1

def max_index(arr, k: int) -> int:

index = 0

for i in range(k):

if arr[i] > arr[index]:

index = i

return index

def pancake_sort(arr) -> None:

n = len(arr)

while n > 1:

maxidx = max_index(arr, n)

if maxidx != n - 1:

if maxidx != 0:

flip(arr, maxidx)

flip(arr, n - 1)

n -= 1

arr = [15, 8, 9, 1, 78, 30, 69, 4, 10]

pancake_sort(arr)

print(arr)

Related integer sequences

:

class="wikitable" align="center" style="float: center; text-align: right;"

|+ Number of stacks of given height {{math|n}} that require unique flips {{math|k}}  to get sorted

! rowspan=2 | Height
{{math|n}} !! colspan=16 | {{math|k}}

width="65px" | 0width="65px" | 1width="65px" | 2width="65px" | 3width="65px" | 4width="65px" | 5width="65px" | 6width="65px" | 7

! width="65px" | 8

width="65px" | 9width="65px" | 10width="65px" | 11width="65px" | 12width="65px" | 13width="65px" | 14width="65px"|15
1

| 1 || || || || || || || || || || || || || || ||

2

| 1 || 1 || || || || || || || || || || || || || ||

3

| 1 || 2 || 2 || 1 || || || || || || || || || || || ||

4

| 1 || 3 || 6 || 11 || 3 || || || || || || || || || || ||

5

| 1 || 4 || 12 || 35 || 48 || 20 || || || || || || || || || ||

6

| 1 || 5 || 20 || 79 || 199 || 281 || 133 || 2 || || || || || || || ||

7

| 1 || 6 || 30 || 149 || 543 || 1357 || 1903 || 1016 || 35 || || || || || || ||

8

| 1 || 7 || 42 || 251 || 1191 || 4281 || 10561 || 15011 || 8520 || 455 || || || || || ||

9

| 1 || 8 || 56 || 391 || 2278 || 10666 || 38015 || 93585 || 132697 || 79379 || 5804 || || || || ||

10

| 1 || 9 || 72 || 575 || 3963 || 22825 || 106461 || 377863 || 919365 || 1309756 || 814678 || 73232 || || || ||

11

| 1 || 10 || 90 || 809 || 6429 || 43891 || 252737 || 1174766 || 4126515 || 9981073 || 14250471 || 9123648 || 956354 || 6 || ||

12

| 1 || 11 || 110 || 1099 || 9883 || 77937 || 533397 || 3064788 || 14141929 || 49337252 || 118420043 || 169332213 || 111050066 || 13032704 || 167 ||

13

| 1 || 12 || 132 || 1451 || 14556 || 130096 || 1030505 || 7046318 || 40309555 || 184992275 || 639783475 || 1525125357 || 2183056566 || 1458653648 || 186874852 || 2001

Sequences from The Online Encyclopedia of Integer Sequences:

  • {{OEIS2C|A058986}} – maximum number of flips
  • {{OEIS2C|A067607}} – number of stacks requiring the maximum number of flips (above)
  • {{OEIS2C|A078941}} – maximum number of flips for a "burnt" stack
  • {{OEIS2C|A078942}} – the number of flips for a sorted "burnt-side-on-top" stack
  • {{OEIS2C|A092113}} – the above triangle, read by rows

References

{{Reflist}}

Further reading

{{refbegin}}

  • {{cite journal|last1=Chitturi|first1=B.|last2=Sudborough|first2=H.|title=Prefix Reversals on Strings|journal=Proceedings of the International Conference on Bioinformatics & Computational Biology|volume=2|date=2010|pages=591–598|url=https://www.researchgate.net/publication/221051667}}
  • {{cite journal|last1=Chitturi|first1=B.|title=A Note on Complexity of Genetic Mutations | journal=Discrete Math. Algorithm. Appl.|volume=3|issue=3|date=2011| pages= 269–287| doi=10.1142/S1793830911001206}}
  • {{cite journal|last1=Heydari|first1=M. H.|last2=Sudborough|first2=I. H.|title=On the Diameter of the Pancake Network|journal=Journal of Algorithms|date=1997|volume=25|issue=1|pages=67–94|doi=10.1006/jagm.1997.0874}}
  • {{cite journal|last1=Hurkens|first1=C.|last2=van Iersel|first2=L.|last3=Keijsper|first3=J.|last4=Kelk|first4=S.|last5=Stougie|first5=L.|last6=Tromp|first6=J.|title=Prefix Reversals on Binary and Ternary Strings|journal=SIAM Journal on Discrete Mathematics|date=2007|volume=21|issue=3|pages=592–611|doi=10.1137/060664252|arxiv=math/0602456}}
  • {{cite journal|last1=Roney-Dougal|first1=C.|author1-link= Colva Roney-Dougal |last2=Vatter|first2=V.|url=http://plus.maths.org/issue54/features/colvatter/ |title=Of Pancakes, Mice and Men|journal=Plus Magazine |volume=54 |date=March 2010}}

{{refend}}