Forgot your password?
typodupeerror
Image

Pancake Flipping Is Hard — NP Hard 260

Posted by samzenpus
from the sorting-a-large-stack dept.
mikejuk writes "French computer scientists have finally proved that sorting pancakes is hard — NP hard. No really — this isn't a joke. Well, it is slightly amusing but that's just because it is being presented as pancake flipping. The algorithm in question is sorting a permutation using prefix reversal — which is much easier to understand in terms of pancakes. Basically you have to sort a pancake stack by simply inserting your spatula and flipping the top part of the stack. We now know that if you can do the this in polynomial time then you have proved that P=NP."
This discussion has been archived. No new comments can be posted.

Pancake Flipping Is Hard — NP Hard

Comments Filter:
  • by halivar (535827) <bfelger.gmail@com> on Friday November 04, 2011 @09:47AM (#37947590) Homepage

    ...in finding the exact amount of maple syrup I need to pour on a pancake stack to ensure that my bacon is accidentally covered in it.

    Because I would never intentionally put maple syrup on my bacon; that's barbaric.

    • Just like how I would never intentionally dip my bacon, and sausage, in runny egg yolk and wrap it in a butter and syrup-soaked pancake. Not without adding peanut butter, that is.
    • by Tarlus (1000874)

      My day can now start on a happy note. Thanks for that. :)

    • by Dunbal (464142) * on Friday November 04, 2011 @10:20AM (#37947976)
      If you used real Canadian maple syrup from real Maple trees instead of that artificial corn syrup crap the rest of the world calls "maple syrup" that tastes like dead beetles, then it really wouldn't matter if you got some on your bacon because everything tastes better with Maple Syrup.
      • by X0563511 (793323)

        Those are some tasty beetles.

      • by MagicM (85041) on Friday November 04, 2011 @10:29AM (#37948118)

        that artificial corn syrup crap the rest of the world calls "maple syrup" that tastes like dead beetles

        It's called "beetle juice". It's quite popular in some areas. I love me some beetle juice.

        Mmmm. Beetle juice.

      • by Scaba (183684)

        Hmmm. Everything also tastes better with bacon. So the question is: does putting real maple syrup on your bacon cause them to improve one another's flavors until your breakfast reaches a maximum recursion limit?

      • Just go to your nearest Whole Foods (or other real food distributor) and get Grade-B maple syrup. It's not as filtered as the standard Grade-A syrup that most are used to. The flavor is incredible compared to the processed crap that everyone is used to.

        • by Rogue Haggis Landing (1230830) on Friday November 04, 2011 @11:04AM (#37948612)

          Just go to your nearest Whole Foods (or other real food distributor) and get Grade-B maple syrup. It's not as filtered as the standard Grade-A syrup that most are used to. The flavor is incredible compared to the processed crap that everyone is used to.

          FWIW, maple syrup grades [cookingforengineers.com] are more dependent on when the trees were tapped than on filtering and processing. (Actually, they're most dependent on what state, provincial, or national body is defining the grades, but that's a different story.) Early in the season, trees produce sap that tends to be higher in sugar and water, and lighter in flavor and color. This sap becomes grade A syrup. As the season progresses, the sap tends to become thicker, less sugary, darker, and stronger flavored, eventually becoming grade B and beyond. This varies from season to season and even from tree to tree, but is generally true.

          If you're really hardcore about your maple, you can round up some Vermont Grade C syrup, "commercial grade", that's usually used in large-scale baking operations. It's extremely thick and strong -- my wife calls it maple sludge. If you like maple, that's as close to the taste of the tree as you can get without gnawing on some bark.

        • by djeez (472062)

          I understand not everybody has this luxury, but what I do every spring is shop around in the various friends and families that happen to know people who own or operate a sugar shack and get various grades of maple syrup. I use grade A instead of white sugar for day-to-day usages (coffee, cereals, etc.), grade B when I'm out of grade A or for maple recipes and grade C for maple sauces and dressings. I once had access to grade D and the strength of the taste was incredible, especially in salad dressing.

          So y

          • by Dunbal (464142) *

            I understand not everybody has this luxury,

            There's no excuse nowadays. I live in Costa Rica and my local convenience store has "Roland" brand 100% genuine Canadian maple syrup. Of course it costs me like $35 for a tiny little jug, but hey you want good stuff you pay good money. Much better than the "dark ages" (30 years ago) where you could ONLY get maple syrup from Quebec, in Quebec province, and probably had to be able to cuss in French Canadian to get it. Maudit tabarnac.

      • by INT_QRK (1043164)
        ...or Vermont Maple syrup, but honestly, I don't think the Maple Trees much care, disloyal bastards.
    • by blair1q (305137)

      That problem is Artery-hard.

  • by Anonymous Coward

    How we can do it in polynomial time but computers can't?

    • by halivar (535827)

      Can you? If P!=NP, then logic dictates you will not be able to do it in polynomial time.

      That's why they say that if you could do it in polynomial time, then P=NP.

      • by Joce640k (829181)

        Am I missing something? I can do it in linear time...

        * Start with the bottom pancake and work towards the top
        * Locate next pancake, insert the spatula beneath it and flip it to the top
        * Insert the spatula at the position where it goes when sorted and flip again.

        Number of flips = 2 x Number of pancakes.

        • by Joce640k (829181)

          PS: If the pancake is burnt on one side then you just flip it over when it's on the top:

          Maximum number of flips to sort burnt pancakes = 3 x Number of pancakes.

        • by IkeTo (27776)

          I think the question being asked is, given a certain stack, what is the minimum number of flips required. 2(n-1) is of course an upper bound, but that is not the minimum.

          • by Joce640k (829181)

            FTA: "if you can find a polynomial time solution to the pancake problem then you have proved that P=NP."

            "f(n) = 2(n-1)" is a polynomial solution of order 1 .... where's my million dollar (and Nobel) prize?

    • by ledow (319597)

      "Polynomial time" does not necessarily mean "quicker than a computer" or even "quickly" or EVEN "slowly". It just means an amount of time proportional to the size of the stack to some power (squared, cubed, etc.)

      Just because a human "can do it" doesn't mean that they did it in NP or even P. It just means they did it in a particular time for a particular example. The important thing is "What was that time compared to the size of the data"? I can handsort a stack of one cards just as fast as a computer.

      • "size of the problem to some power" is the definition of polynomial time. Polynomial time problems are generally considered "easy" -- for example, your typical sorting algorithm is between n*log(n) and n^2. These grow slowly enough that general polynomial algorithms, even with relatively high exponents (like n^3 and n^4) are doable for reasonably large input sets.

        The time it takes to solve an NP-hard problem is more in line with "a constant raised to the power the size of the problem". So doubling the si

      • by nschubach (922175)

        I can handsort a stack of one cards just as fast as a computer.

        I don't know why, but I read this as if it said computers had hands...

    • by beelsebob (529313)

      I'm struggling to understand the problem, can someone tell me what I've missunderstood.

      As I understand it the idea is to sort the pancakes from large to small by flipping a substack. Surely you could apply selection sort, where each time you select a pancake you flip from below it up (resulting in it being on top), and then flip from the place you want to insert it up, resulting in it now being on top of the currently sorted pile.

      Clearly as I've just given an easy O(n^2) solution, I've missed something, wh

      • So the paper talks about SBPR (sorting by prefix reversals) and MIN-SBPR. The question is not "here, sort this stack of pancakes", it's "determine what the minimal number of flips is to sort an arbitrary stack of pancakes of size n".

        If I'm following this, then sorting the stack itself is relatively easy (as you said, n^2). Figuring out the optimal sorting is apparently what's hard.

    • by Dunbal (464142) *
      Because we use abstraction instead of algorithms. The same type that lets us do 2nd order differential equations in our heads to put our hands in the path of a ball we intend to catch, without actually doing the math. Kinda hard to bind abstraction to a mathematical formula though.
      • Is that, like, you know how to stroke it but it's kinda hard to tell your girlfriend how to do it right?

        • by Dunbal (464142) *
          Surprisingly I didn't find it hard to explain it to her at all. I guess I got lucky - my ex wife never seemed to get it at all.
    • Re: (Score:2, Funny)

      by jank1887 (815982)

      computers have a specific disadvantage with this problem. they have trouble holding the spatula.

    • by Hatta (162192)

      You can't.

  • by Tynin (634655) on Friday November 04, 2011 @09:52AM (#37947650)
    Sounds like they just reworded the Towers of Hanoi puzzle, which has been around for a long time.
    • Not exactly, in the towers game you have 3 stacks and you rearrange the pieces by removing the top of a stack and placing it on the top of another stack. The challenge comes from the following rule: a bigger piece can never be placed above a smaller piece.
      • Erh... like in the Towers of Hanoi [wikipedia.org] problem?

        • by nschubach (922175)

          I think the difference is that there's only one tower, and I assume you can maintain the order of the pancakes in the flipping stack as long as the top pancake doesn't land on a smaller one.

      • by canajin56 (660655)
        There's no rule about bigger on smaller. If there was it would be impossible to make even a single move, ever.
    • Yeah, pretty similar. Even the pancake flipping has been around since the 80s, lots of people have taken cracks at it. The interesting thing here is the P vs NP problem, which the paper barely even mentions and does not explain whatsoever.
    • Re:Towers of Hanoi? (Score:4, Informative)

      by yakatz (1176317) on Friday November 04, 2011 @10:08AM (#37947820) Homepage Journal
      The difference is in where you flip. In the Towers of Hanoi, you do not flip the whole stack, you can only move the pieces one at a time between poles.
      For the pancake problem, you only have one pole and you flip as many as you want at once.
      Good explanation: http://en.wikipedia.org/wiki/Pancake_sorting [wikipedia.org]
    • by Carewolf (581105)

      I thought the same thing, but it is not that close really. The setup is very different in that the plates starts mixed, you only have one pin, but you can move a whole stack of plates in one move.

      So not the towers of Hanoi, but the comparison does make it easier for me to see why it could take an exponential number of moves.

      • by Carewolf (581105)

        Hmm. This actually means that if you prove this is a reworded version of the Towers of Hanoi, you would have proven N != NP.

  • I love this problem. I have been reading about P=NP blah blah blah but never had a solid mental picture. This is great. I get it. Thanks. I wonder how many other mathematical misunderstandings could be cleared up with something as simple as pancakes?
    • by frisket (149522) <peter AT silmaril DOT ie> on Friday November 04, 2011 @10:05AM (#37947782) Homepage

      I love this problem. I have been reading about P=NP blah blah blah but never had a solid mental picture. This is great. I get it. Thanks. I wonder how many other mathematical misunderstandings could be cleared up with something as simple as pancakes?

      It's easy: P=pancakes and NP=no pancakes. When P=NP it means you ate them all. The problem is that P-NP != 0 because there's always some maple syrup left on the plate...

      • by Dunbal (464142) *
        Conservation of matter dictates that the pancakes are still there. Even if you ate them this just means that you moved them somewhere else (your stomach), but the pancakes do not cease to exist. Even after digestion if your stool is used as fertilizer to grow wheat for flour, corn for chickens to make eggs and grass and hay for cows to make milk, and the inorganic components are reprocessed into salt and baking soda, you can have pancakes again forever. Therefore "NP" (no pancakes) really has no meaning. Ye
        • Conservation of matter dictates that the pancakes are still there.

          You have never seen me eat pancakes. I gobble them down so fast that for every m of pancakes I eat, 1/c^2 of e is released.

      • So if I don't use any syrup, I can eat pancakes in polynomial time? It's all starting to make sense now.
    • Uh, did we read the same article? Because what I just read barely explained the pancake flipping algorithm, and that's it. If you like the pancake algorithm, that's fine, but that has almost nothing to do with P vs NP other than the fact that it is AN example of A problem. The article didn't explain how they proved it was NP-Hard, they didn't explain what P vs NP is at all, they didn't explain why P vs NP is significant...

      What exactly do you understand now that you didn't before?

      Mathematics is complic
      • Mathematics is complicated, unfortunately that's just the nature of it, there's no shortcuts. Sometimes ideas can be better communicated through metaphor, but I think you're gotten hung up on the pancakes and as a result you're missing the forest for the trees.

        Hmmm. That is an interesting take on metaphor. I was taught that metaphor is the refuge of writers whose thoughts are too fuzzy and undisciplined to be articulated in any other way. Metaphors are for communicating fuzzy ideas, not basic mathematical truths derivable from simple postulates. And no...I don't think mathematics is complicated. If it was complicated, humans couldn't do it. As my favorite author put it,

        "Anyone who cannot cope with mathematics is not fully human. At best he is a tolerable s

  • by Anonymous Coward

    I really like this example because it is so intuitive. Its obvious at a glance that 2n is the worst things could ever get (they say 2n -6 for n >= 16) .. the real problem is, when it is easier than 2n (not from trivial already in-order sections), how do you find the optimum? :)

  • by RenHoek (101570) on Friday November 04, 2011 @09:56AM (#37947686) Homepage

    Now if they can only explain to me what the hell 'polynomial time' is.. maybe with a bagel allegory..

  • Bill Gates did it. (Score:2, Informative)

    by Anonymous Coward

    Bill Gates' only published paper [berkeley.edu] gives a solution to the pancake-sorting problem. I discuss it at my blog [programmingpraxis.com].

  • Clarification from the article, the actual problem statement is:

    The question to be answered in both cases is, if the stack has n pancakes, what is the maximum number of flips needed as a function of n, i.e. f(n)?

    With regards to actual doing this for a specific stack, it's fairly trivial to order the pancakes in a polynomial number of flips (any n log n sort algorithm would be fine - with the actual flipping taking at most 2 flips per pancake).

    • by Millennium (2451)

      The thing is, you're not swapping individual pancakes. You have to flip the entire stack above the insertion point. So if I have a stack [1,2,3,4,5,6,7,8,9] and I flip from where the 5 is, my stack is now [5,4,3,2,1,6,7,8,9], not [5,2,3,4,1,6,7,8,9].

      That's what makes it hard. If all you were doing was swapping individual pancackes around then this would be easy, but you have to move stacks of pancakes.

      • by JMZero (449047)

        Well, there's two problems here: calculating an optimal number of flips (which I'm not sure about) and "sorting the stack with a polynomial number of flips" which is what I think is trivial (and which the summary seemed to suggest was difficult). I was just trying to clarify that this second problem is not hard (but my post itself wasn't clear).

        To further clarify, the naive algorithm for this second problem is simple and polynomial (in number of flips per pancake). Start at the bottom. If the largest panc

        • Yay! Real content on slashdot. For all programmers here we don't see algorithms often enough. Thanks.

  • The fiddly part is that the question is what is the minimal number of flips required to sort the stack. Finding some number of flips that will sort the stack is fairly straightforward.

  • Solution? (Score:4, Interesting)

    by Erich (151) on Friday November 04, 2011 @10:09AM (#37947832) Homepage Journal
    I had something like this was an interview question once. My solution:

    Assume we are stacking pancakes with largest at the bottom.

    1. Find the largest unsorted pancake
    2. Flip that to the top
    3. Flip from the bottom-most unsorted pancake. (One additional pancake is now sorted)
    4. Repeat until sorted

    To me, assuming that you consider "Find the largest unsorted pancake" to be O(N), the algorithm is O(N^2). Number of flips is 2N. Where's my turing award?

    So I must be missing something... Is one not able to find the largest unsorted pancake easily? Perhaps you are only able to look at the size of the topmost pancake. The article was unclear.

    • Re:Solution? (Score:4, Insightful)

      by Erich (151) on Friday November 04, 2011 @10:12AM (#37947866) Homepage Journal
      Oh, I see. The problem is not that pancake flipping itself is hard, that determining the optimal pancake flipping algorithm for a stack is hard. That's believable.
      • by adisakp (705706)

        Oh, I see. The problem is not that pancake flipping itself is hard, that determining the optimal pancake flipping algorithm for a stack is hard. That's believable.

        Yeah... well since finding the optimal flipping solution is NP hard, you'll save time with the simple O(N^2) solution.

  • There is a pancake flipping algorithm. Have you heard of it?

    Seriously, pancake flipping has been around forever. Seriously, its one of the few algorithms that even Bill Gates has implemented: http://en.wikipedia.org/wiki/Pancake_sorting#History [wikipedia.org] And apparently the co-creator of Futurama has as well.

    TFA mentions that they proved pancake flipping was NP-Hard. Cool. THATS IT. It doesn't explain how they got to this conclusion, it doesn't explain the significance, it doesn't extrapolate anything useful...
  • There seems to be a miscommunication here. You can prove that P = NP only if you can solve an NP-complete problem in polynomial time (or some other type that can be reduced to every other problem in the set). The article refers to NP-hard, which is not necessarily in the NPC set. Thus, solving the pancakes problem in polynomial time only guarantees that P = NP for the pancake problem (again, unless you can reduce it to every other problem).
    • by canajin56 (660655)
      NP-Hard means it can be used to solve any NP-Complete problem. Not the opposite of that, like you seem to think...
  • The more important question is knowing when to flip the pancake.

  • I can easily think of a polynomial time algorithm. Just do a selection sort. I can even do the "burnt pancakes problem" by doing a selection sort, and flipping the biggest pancake to the appropriate side when it's at the top. What makes this hard?
    • by Arlet (29997)

      The hard part is finding the optimal solution with the fewest number of flips.

    • by bmacs27 (1314285)
      I see now. Searching for the LEAST number of flips for a given stack is NP-Hard. The summary should be updated.

When I left you, I was but the pupil. Now, I am the master. - Darth Vader

Working...