Monday, August 1, 2011

FOR THE SAKE OF PANCAKE!

It was 12 in the afternoon and I was badly hungry. "One more hour for lunch break!”, I thought. One of the greatest teachers I strongly admire, started off with his lecture on some of the concepts of theoretical computer science. As expected we all were drowned in the world of his logics and were enjoying his lecture. It was as though my hypothalamus ceased to function and I forgot I was hungry. After a while he presented in front of us, a problem, that reminded me of my hunger once again!

It was about the yummiest south Indian food, dosa. The problem goes like this. A chef in some famous south Indian diner is preparing lots and lots of dosas, all of different sizes and is piling them up. But before delivering them, he wants to rearrange them such that the smallest one is on the top of the stack and the largest at the bottom. He has only one spatula, and he can place it somewhere in the stack, lifting the portion above the spatula, flipping it, and then placing it back on the stack. If there are $n$ dosas, what is the minimum number of flips performed by the chef to rearrange the stack? This, is the Indian version of the famous pancake problem.



Jacob Goodman, under the name Harry Dweighter, proposed the pancake problem in the year 1975. This scenario of a chef rearranging the pancakes was proposed by him, keeping in mind the minimum number of flips needed to obtain a correctly arranged stack of pancakes. One might wonder about the need to formulate such problem. It has many applications in the field of genetics, in sorting out the genome sequence.


The simplest solution to this problem requires $2n-2$ flips for $n$ pancakes. This can be explained with an example. The chef can place his spatula below the largest pancake, flip it, and place it back on the stack to obtain a newly arranged stack. He can then place his spatula below the second largest pancake and follow the same procedure for all the $n-1$ pancakes, because the last one left will be the smallest and it is evident that it will be on the top. Hence no flip is required for the smallest. From the above argument you can easily make out that each pancake is associated with two flips.


$2(n-1)+ 0= 2n-2$


The $0$ in the above equation indicates the number of flips for $n=1$.For remaining $n-1$ pancakes, the number of flips should be $2(n-1)$ flips, i.e $2n-2$ flips.


The above upper bound can be further reduced to $2n-3$. Consider you have only 2 pancakes with you. Now only one flip is required to re-arrange it. If there are $n$ pancakes, then apply the procedure mentioned above for $n-2$ pancakes and this will fetch you:


$2(n-2)+1=2n-3$ flips.


Here $+1$ is for the $n=2$ condition and the remaining $n-2$ pancakes, each being associated with 2 flips will amount to $2(n-2)$ flips, giving a total of $2n-3$ flips.


It occurred to some that the above algorithm can be improved and some attempts were made in the same.


In the year 1979, an algorithm was proposed by, the very famous Bill Gates of Microsoft fame ( perhaps the only technical paper that Gates came up with!) and Christos.H.Papadimitriou. The different sorting algorithm had lower and upper bounds to be $\frac{5n+5}{3}$ and $\frac{17n}{16}$ respectively.


Terminologies required to understand Gates’ and Papadimitriou’s algorithm:



$\sigma$: It is the permutation of numbers ranging from $1$ to $n$, you are provided with. The numbers in this permutation indeed signify the size of the pancakes. Larger the number greater is the size of the pancake.


For example, $\sigma$=( 5 1 4 2 3) denotes the arrangement of pancakes such that the largest i.e, number 5 is on the top of the stack with the third largest at the bottom and so on. The aim is to re-arrange it such that you get the identity permutation, which is $i$=( 1 2 3 4 5).


$S_n$ is the set of all permutations of numbers ranging from $1$ to $n$.



Adjacency: For a given stack of pancakes, an adjacency is an adjacent pair of pancakes in the stack such that there is no such pancake whose size is intermediate between the two.If the largest pancake is at the bottom this also counts as one extra adjacency.


For example if $\sigma$=( 2 5 3 7 6 1 4), (7,6) is an adjacency. Because they are adjacent to each other in the given $\sigma$ and as well as there are no pancakes of size in between 7 and 6.


Consecutive: If two pancakes are consecutive to each other in the identity permutation, then they are said to be consecutive.


For example: Given $\sigma$=( 3 5 1 2 4) : 1 is consecutive to 2, 2 is consecutive to 3, 3 is consecutive to 4, 4 is consecutive to 5, 5 is consecutive to 1.


A block: Sticking with the definition in the paper a block is a maximum length sublist $x=\sigma_i \sigma_{i+1}{.......}\sigma_{j-1} \sigma_j=y$ such that there is an adjacency between $\sigma_p$ and $\sigma_{p+1}$ for all $i\leq p \leq j$.


End points: The initial and final elements of the block are called end points.


Singleton: An element that does not belong to any block is called as a singleton.


For example consider $\sigma$=( 3 4 5 1 6 7 2):


In the above permutation:




  1. (3 4 5) is a block since the consecutive elements form an adjacency and so are ( 6 7). They form an adjacency as well as a block since its evident that every adjacency is also a block.

  2. 1 and 2 are singletons since they do not belong to any block.
A table has been given in the paper whose link is given below:
You can refer the paper for many cases which are used below
http://etd.ohiolink.edu/send-pdf.cgi/Armstrong%20Alyssa.pdf?wuhonors1242223287

In the following example $B$ denotes the initial element of the permutation, i.e., the leftmost element.The separation between the blocks and singletons is marked by the symbol $\__$.If B begins with any block then we denote it as $B\simC$. We do the same thing for the right most element of the initial block and denote the element consecutive to it as $D$. Remaining all elements between blocks and singletons are denoted by $\__$.

Example: let $\sigma=(3 4 1 5 2)$:




  • B=3

  • ( 3 4) is a block denoted by B$\sim$C.

  • A=2, since the element consecutive to 3 ( other than 4 which is already in the block) is 2.

  • D=5, since 4 is the rightmost element of the block and 5 is consecutive to 4 which is also a singleton.

  • So we denote this permutation as B$\sim$C$\__$D$\__$A.

Having these definitions in mind, we can proceed with the following example:


Let $\sigma \in S_8$, i.e., $\sigma=(2 3 4 7 6 1 5)$ and the identity permutation $i_8=( 1 2 3 4 5 6 7 8)$:


According to Gates' algorithm:




  • Begins with block ( 2 3 4) and 2 is consecutive to 1 and is also a singleton.Hence, by case 4,we have
    B=2, A=1
    So, we reverse at 6 to get ( 6 7 4 3 2 1 5).

  • Now, the permutation begins with the block ( 6 7 ). 6 is consecutive to 5 which is a singleton. So, by case 4,:
    B=6, A=5:
    We reverse at 1 to get ( 1 2 3 4 7 6 5).

  • Now, the initial block is ( 1 2 3 4) and 1 is consecutive to 7 of the block ( 7 6 5). By case 5 of the table,
    B=1, A=7
    So, we reverse at 4 to get ( 4 3 2 1 7 6 5).

  • Now, we got a single block, in such cases we go for the trivial algorithm,
    The largest is 7, so reverse at 7 to get ( 7 1 2 3 4 6 5) and further reverse at 5 to get ( 5 6 4 3 2 1 7).

  • Again we have got a single block, apply the trivial algorithm,
    Reverse at 6 to get ( 6 5 4 3 2 1 7) and at 1 to get (1 2 3 4 5 6 7).
Thus, we got the identity permutation after 7 reversals.

Using this algorithm Gates and Papdimitriou showed that the lower bound for the algorithm is 1.667. In 1997, Mohammad H. Heydari and I. Hal Sudborough improved the lower bound to $\frac{15n}{14}$ and computed the pancake numbers up to 13.



The above image shows the team of UT Dallas computer science students and their faculty adviser who improved upon a longstanding solution to the pancake problem.


An interesting variant of the problem, proposed by Gates and Papadimitriou, concerns pancakes that are burnt on one side. Here the pancakes must be sorted with the burnt side down.


Applications:



  1. The pancake problem is relevant to the construction of networks of parallel processors, in which pancake sorting can provide an effective routing algorithm between the processors.


  2. Analysis of Genomes: In evolutionary processes, changes in DNA sequences can cause a new species to get separated from an existing one, resulting in diversified life-forms. If you want to reconstruct such changes, you can look into genome sequences of related species. An event where a certain sequence is deleted and is replaced with its reverse sequence can be looked for. Thus analyzing the transformation from one species to the other is analogous to the problem of finding the shortest series of reversals.








14 comments:

  1. Thumbs up for the title..Nicely written with a desi touch.
    Just when i was wondering why would anyone need to find the flips, the answer was there :) Good Job :)
    Was a little dazzled looking at the mathematical formulae..but Examples came to rescue when it came to understanding of the prefix-reversal and block structure :) Beautiful :)
    It looks interesting and hence i grabbed my pen and paper to solve the reversal process of the permutation given.But got stuck at 4th point in Gate's algorithm.
    "At this point, the trivial algorithm is used to finish transforming the permutation." Did not understand this point.:(
    Nice to know the evolvement of problem with improved efficiency step by step. :)
    It is interesting to relate the simple pancake problem to biology.
    By the way,it would great if u could mention the names of the people below their pictures:)

    ReplyDelete
  2. 'FOR THE SAKE OF PANCAKE'-a bang on title...:):).very well started with the story.. you were hungry-->forgot ur hunger-->problem reminds u back of hunger...dat part is too gud..:):)
    Problem statement very smartly introduced by taking the indian version,dosa, and then narrowing it down to pancake problem..:)
    exactly before the point u mentioned 'u might be wondering ', i too, was actually wondering about its application,[de javu moment eh ? :):)] You grabbed my whole attention when you mentioned that Bill Gates gave an algorithm for this problem..;):)
    Algorithm and terminologies very nicely explained with examples..:) :) I was only confused as of which part you referred to as trivial algorithm..
    Excellent flow of the topic with well spread pictures of contributors..:):)Not only did you explain about pancake problem in an interesting way,but also mentioned its fascinating applications...:):) great work..:):)

    ReplyDelete
  3. Hey nice title:)
    was curious about this problem when cpr mentioned it.You made our job easy.
    was a little dazzled to see all the terminologies
    .But then later it appeared not so tough.It was amazing solving Gates algorithm.
    was amazed with the application part!
    Good job :)
    PS-Pictures are too good:)

    ReplyDelete
  4. the trivial algorithm is the algorithm that was explained in the introduction suction..the one with the 2n-2 complexity. You can try it turns out to be the same.for more details you can refer to the paper who's link is given below. In this paper a new method developed by the author has been explained.
    http://etd.ohiolink.edu/send-pdf.cgi/Armstrong%20Alyssa.pdf?wuhonors1242223287

    ReplyDelete
  5. gates algorithm is nice. a very effective improvisation to an otherwise naive algorithm.

    considering your exposition of the analogy....that is,the solution you have presented to the pancake problem..there are a few fallacies.

    1.the no of flips needed is 2n-3.

    2.the explanation of how each pancake is associated with two flips is not clearly given. infact it is not at all mentioned.

    3.it is rather confusing,so i would rather that you looked over carefully at your analogies before creating widespread confusion.

    4.the photo creates a hell lot of confusion. a picture speaks a thousand words. here it speaks a ten thousand wrong words. the dosas are all of approximately the same size in that picture.
    so the layman is going to be put off by these articles that show one thing and say another thing.

    5.i notice that the gates algorithm paper has been put in "as is" into the blog. these technical terms are highly confusing for mere mortals who have little background in such sophisticated mathematics.So i recommend that you take some of your precious time to explain these mathematical terminologies to us in more understandable lingo.

    6.for those of us who would prefer a less confusing source of info,on this particular topic at least,
    try,

    a)http://www.cut-the-knot.org/SimpleGames/Flipper.shtml

    b)http://en.wikipedia.org/wiki/Pancake_sorting

    oh i am wasting time. google the topic and you will get all you need and more.

    i am only pointing out the many flaws i saw in this article.
    nothing personal(smiley)

    ReplyDelete
  6. @Chetan.s.kumar:
    1.the number of flips needed is 2n-2 which is further improvised to fetch you 2n-3. Since the intention was to concentrate on gates' algorithm, it hardly mattered.But still i have also explained as to how it turns out to be 2n-3 .
    2.the explanation of how each pancake was associated with two flips was clearly given. But i have polished it a little bit since i might not be clear enough the first time.
    3.These are my initial attempts at blog writing. The analogies are not mine, I referred them from a paper!! I guess the confusions are not yet widespread!
    4.Since the photo was creating a hell lot of confusion to the readers ( good observation!) i have changed some:-)
    5.I had put the gates algorithm " as is " in the blog, because, i thought my own definitions would rather confuse the readers.So, I have made an attempt to explain the terminologies in layman language, by giving examples:-)
    6.And the links provided by you were not speaking anything about Gates' algorithm. But they might help others in understanding the problem statement.
    Thank you for taking out your precious time and giving a lot of inputs. They are well appreciated!(smiley):P

    ReplyDelete
  7. @chethan how are the improvements now?

    ReplyDelete
  8. sir,@shruthi...

    thanks for taking my suggestions(in good spirit).
    the improvements are very good.

    the article is excellent now,according to me.

    great stuff.

    ReplyDelete
  9. and @shruthi

    the gates algorithm was clear enough to me in the "old" article.

    but the pancake analogy was a little loopy.

    so i included the links relevant to this portion.

    anyway,
    nice.

    ReplyDelete
  10. and i suppose the stuff about 2n-3 is irrelevant to accomplished people who know this stuff well.

    but you have to bear in mind that people who read this algorithm trace it to understand it.

    in which case these small mistakes can drive them wild(personal experience).
    which is why i priggishly insisted on 2n-3

    ReplyDelete
  11. @chetan.s.kumar: Well, it was not a mistake.I dint think it was necessary to mention about the 2n-3 algorithm.Because in my first article, i only mentioned about 2n-2 algorithm and people to my knowledge did trace it successfully. Pointing out to mention the 2n-3 algorithm was actually helpful. Thank you :-)

    ReplyDelete
  12. you are welcome...

    ReplyDelete
  13. http://www.slidefinder.net/t/the_pancake_problem_mittagsseminar_eth/11710214
    It would be better to show something like this. As of my knowledge, the pictures or animations for supporting this topic is very much necessary. For a person who isn't aware of anything about the topic would search for good pictures and animations so that makes him understand things better.

    ReplyDelete