115-251Great Theoretical Ideas in Computer Science15-251Great Theoretical Ideas in Computer Sciencewww.cs.cmu.edu/~15251Course StaffInstructorsVictor AdamchikDanny SleatorTAsDrew Besse Adam Blank Dmtriy Chernyak Sameer Chopra Dan Kilgallin Alan Pierce Homework50%Final20%In-Class Quizzes4%3 Tests30%Lowest non-programming HW grade is droppedLowest quiz droppedAll tests countedGradingWeekly HomeworkTen points per day late penaltyNo homework will be accepted more than two days lateHomeworks handed out Tuesdays (except for a couple) and are due the following Tuesday, at midnightHomework MUST be typeset, and a single PDF file2Collaboration + CheatingYou may NOT share written workYou may NOT use Google (or other search engines)You MUST sign the class honor codeYou may NOT use solutions to previous years’ homework.TextbookThere is NO textbook for this classWe have class notes in wiki formatYou too can edit the wiki!!!((( )))Feel free to ask questionsAnd take advantage of our generous office hours15-251Cooking for Computer Scientists3Lecture 1 (January 12, 2010)Pancakes With A Problem!He does this by grabbing several from the top and flipping them over, repeating this (varying the number he flips) as many times as necessaryThe chefs at our place are sloppy: when they prepare pancakes, they come out all different sizesWhen 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) How do we sort this stack?How many flips do we need?How do we sort this stack?How many flips do we need?Developing A Notation:Turning pancakes into numbers5234152341How do we sort this stack?How many flips do we need?523414523414 Flips Are Sufficient1234552341432152341514325Best Way to Sort52341X = Smallest number of flips required to sort:? X ?Upper BoundLower Bound4Can we do better?52341Four Flips Are Necessary523414132514325Flip 2 must bring 4 to top (if it didn’t, we would take more than three flips)Flip 1 has to put 5 on bottom (else wewould take 3 flips just to get 5 to bottom)If we could do it in three flips:Upper BoundLower Bound4 X 4X = 452341whereX = Smallest number of flips required to sort:5MAX over s stacks of 5 of MIN # of flips to sort sP5= . . . . . .120111932X1X2X3X119X12052341412345543215thPancake NumberNumber of flips required to sort the worst case stack of 5 pancakesP5=5thPancake Number? P5 ?Upper BoundLower Bound4―For all 5-pancake stacks s, we can sort s in this much time‖―There exists a 5-pancake stack which will make metake this much time‖MAX over s stacks of n pancakes of MIN # of flips to sort sPn=The number of flips required to sort the worst-case stack of n pancakesPn=What is Pnfor small n?Can you do n = 0, 1, 2, 3 ? Initial Values of Pnn 30 1 2Pn0 0 1 3P3= 3132requires 3 Flips, henceANY stack of 3 can be done by getting the big one to the bottom (≤ 2 flips), and then using ≤ 1 flips to handle the top twoP3≥ 36nthPancake NumberNumber of flips required to sort the worst case stack of n pancakesPn=? Pn ?Upper BoundLower BoundBracketing:What are the best lower and upper bounds that I can prove?≤ f(x) ≤[]? Pn ?Try to find upper and lower bounds on Pn,for n > 3Bring biggest to top Place it on bottom Bring next largest to topPlace second from bottomAnd so on…Bring-to-top MethodUpper Bound On Pn:Bring-to-top Method For n PancakesIf n=1, no work required — we are done!Otherwise, flip pancake n to top and then flip it to position nNow use:Bring To Top Method For n-1 PancakesTotal Cost: at most 2(n-1) = 2n –2 flipsBetter Upper Bound On Pn:Bring-to-top Method For n PancakesIf n=2, at most one flip and we are done!Otherwise, flip pancake n to top and then flip it to position nNow use:Bring To Top Method For n-1 PancakesTotal Cost: at most 2(n-2) + 1 = 2n – 3 flips7? Pn 2n-3For a particular stackbring-to-top not always optimalBring-to-top takes 5 flips, but we can do in 4 flips3214552341231454132514325? Pn 2n-3What other bounds can you prove on Pn?916Breaking Apart ArgumentSuppose a stack S has a pair of adjacent pancakes that will not be adjacent in the sorted stackAny sequence of flips that sorts stack S must have one flip that inserts the spatula between that pair and breaks them apartFurthermore, this is true of the ―pair‖ formed by the bottom pancake of S and the platen Pn2468..n135..n-1SS contains n pairs that will need to be broken apart during any sequence that sorts itSuppose n is even21Detail: This construction only works when n>2n Pn1357..n246..n-1SS contains n pairs that will need to be broken apart during any sequence that sorts itSuppose n is odd132Detail: This construction only works when n>38n Pn 2n – 3 for n > 3Bring-to-top is within a factor of 2 of optimal!From ANY stack to sorted stack in ≤ PnReverse the sequences we use to sortFrom sorted stack to ANY stack in ≤ Pn ?((( )))Hence, from ANY stack to ANY stack in ≤ 2PnCan you find a faster way than 2Pnflips to go from ANY to ANY?((( )))ANY Stack S to ANY stack T in ≤ PnS: 4,3,5,1,2 T: 5,2,4,3,11,2,3,4,53,5,1,2,4Rename the pancakes in S to be 1,2,3,..,n Rewrite T using the new naming scheme that you used for S The sequence of flips that brings the sorted stack to the ―new T‖ will bring S to T―new T‖The Known Pancake Numbers01345nPn7891011123456789101112131415161713141516171819P18is Unknown1234…1718 = 18! orderings of 18 pancakes18! = 6.402373 × 1015To give a lower bound of 21 (say), we would have to find one of these stacks for which no sequences of 20 swaps sort the stack.It is either 20 or 21, we don’t know which.9Is This Really Computer Science?Posed in Amer. Math. Monthly82 (1) (1975), ―Harry Dweighter‖ a.k.a. Jacob GoodmanSorting By Prefix Reversal(17/16)n Pn (5n+5)/3William Gates and Christos Papadimitriou. Bounds For Sorting By Prefix Reversal. Discrete Mathematics, vol 27, pp 47-57, 1979.(15/14)n Pn (5n+5)/3H. Heydari and H. I. Sudborough. On the Diameter of the Pancake Network. Journal of Algorithms, vol 25, pp 67-94, 1997.(15/14)n Pn (18/11)nB. Chitturi, W. Fahle, Z. Meng, L. Morales, C. O. Shields, I. H. Sudborough and W. Voit. An (18/11)n upper bound for sorting by prefix reversals, to appear in Theoretical Computer Science, 2008. (3/2)n BPn 2n-2David S. Cohen and Manuel Blum.On the problem of sorting burnt pancakes.
View Full Document