1 / 66

Pancakes With A Problem!

Pancakes With A Problem!. Course Staff. Prof: Bruce Maggs (Carnegie Mellon) TA: Souvik Sen Lectures developed at Carnegie Mellon, primarily by Prof. Steven Rudich. ((( ))). Please feel free to ask questions!. Course Document You must read this carefully.

desmond
Download Presentation

Pancakes With A Problem!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pancakes With A Problem!

  2. Course Staff • Prof: Bruce Maggs (Carnegie Mellon) • TA: Souvik Sen • Lectures developed at Carnegie Mellon, primarily by Prof. Steven Rudich.

  3. ((( ))) Please feel free to ask questions!

  4. Course DocumentYou must read this carefully. • Grading formula for the course. • 40% homework • 30% quizzes • 30% final • Seven points a day late penalty. • Collaboration/Cheating Policy • You may NOT share written work. • We reuse homework problems.

  5. The chef at our place is sloppy, and when he prepares a stack of pancakes they come out all different sizes. Therefore, when I deliver them to a customer, on the way to the table I rearrange them (so that the smallest winds up on top, and so on, down to the largest at the bottom). I do this by grabbing several from the top and flipping them over, repeating this (varying the number I flip) as many times as necessary.

  6. Developing A Notation:Turning pancakes into numbers

  7. 5 2 3 4 1 Developing A Notation:Turning pancakes into numbers

  8. Developing A Notation:Turning pancakes into numbers 5 2 3 4 1

  9. Developing A Notation:Turning pancakes into numbers 52341

  10. How do we sort this stack?How many flips do we need? 52341

  11. 4 Flips Are Sufficient 52341 14325 23415 43215 12345

  12. Algebraic Representation 52341 X = The smallest number of flips required to sort: Upper Bound ?  X  ? Lower Bound

  13. Algebraic Representation 52341 X = The smallest number of flips required to sort: Upper Bound ?  X  4 Lower Bound

  14. 4 Flips Are Necessary 52341 14325 41325 If we could do it in 3 flips Flip 1 has to put 5 on bottom Flip 2 must bring 4 to top (if it didn’t we’d need more than 3 flips).

  15. ?  X  4 Lower Bound

  16. X = 4 4  X  4 Lower Bound Upper Bound

  17. 5th Pancake Number P5 = The number of flips required to sort the worst case stack of 5 pancakes. Upper Bound ?  P5 ? Lower Bound

  18. 5th Pancake Number P5 = The number of flips required to sort the worst case stack of 5 pancakes. Upper Bound 4  P5 ? Lower Bound

  19. The 5th Pancake Number: The MAX of the X’s 1 2 3 52341 119 120 . . . . . . . X120 X1 4 X2 X3 X119

  20. P5 = MAX over s 2 stacks of 5 of MIN # of flips to sort s 1 2 3 52341 199 120 . . . . . . . X1 4 X2 X3 X119 X120

  21. Pn MAX over s  stacks of n pancakes of MIN # of flips to sort s Or, The number of flips required to sort a worst-case stack of n pancakes.

  22. Pn = The number of flips required to sort a worst-case stack of n pancakes. Be Cool. Learn Math-speak.

  23. What is Pn for small n? Can you do n = 0, 1, 2, 3 ?

  24. Initial Values Of Pn

  25. P3 = 3 • 132 requires 3 Flips, hence P3¸ 3. • ANY stack of 3 can be done in 3 flips. • Get the big one to the bottom (· 2 flips). • Use · 1 more flip to handle the top two. • Hence, P3· 3.

  26. nth Pancake Number Pn = Number of flips required to sort a worst case stack of n pancakes. Upper Bound ?  Pn ? Lower Bound

  27. ?  Pn ? Take a few minutes to try and prove bounds on Pn,for n>3.

  28. Bring To Top Method Bring biggest to top. Place it on bottom. Bring next largest to top. Place second from bottom. And so on…

  29. Upper Bound On Pn:Bring To Top Method For n Pancakes • If n=1, no work - we are done. • Else: flip pancake n to top and then flip it to position n. • Now use: Bring To Top Method For n-1 Pancakes Total Cost: at most 2(n-1) = 2n –2 flips.

  30. Better Upper Bound On Pn:Bring To Top Method For n Pancakes • If n=2, use one flip and we are done. • Else: flip pancake n to top and then flip it to position n. • Now use: Bring To Top Method For n-1 Pancakes Total Cost: at most 2(n-2) + 1 = 2n –3 flips.

  31. Bring to top not always optimal for a particular stack • 5 flips, but can be done in 4 flips 52341 14325 41325 23145 32145

  32. ?  Pn 2n - 3 What bounds can you prove on Pn?

  33. Breaking Apart Argument 9 16 • Suppose a stack S contains a pair of adjacent pancakes that will not be adjacent in the sorted stack. • Any sequence of flips that sorts stack S must involve one flip that inserts the spatula between that pair and breaks them apart.

  34. Breaking Apart Argument 916 • Suppose a stack S contains a pair of adjacent pancakes that will not be adjacent in the sorted stack. • Any sequence of flips that sorts stack S must involve one flip that inserts the spatula between that pair and breaks them apart. • Furthermore, this same principle is true of the “pair” formed by the bottom pancake of S and the plate.

  35. n  Pn S 2468..n1357..n-1 • Suppose n is even. • Such a stack S contains n pairs that must be broken apart during any sequence that sorts stack S.

  36. n  Pn S 21 • Suppose n is even. • Such a stack S contains n pairs that must be broken apart during any sequence that sorts stack S. Detail: This construction only works when n>2

  37. n  Pn S 1357..n 2468..n-1 • Suppose n is odd. • Such a stack S contains n pairs that must be broken apart during any sequence that sorts stack S.

  38. n  Pn S 132 • Suppose n is odd. • Such a stack S contains n pairs that must be broken apart during any sequence that sorts stack S. Detail: This construction only works when n>3

  39. n ≤ Pn≤ 2n – 3 (for n ≥ 3) Bring To Top is within a factor of two of optimal!

  40. n ≤ Pn≤ 2n – 3 (for n ≥ 3) So starting from ANY stack we can get to the sorted stack using no more than Pn flips.

  41. ((( ))) From ANY stack to sorted stack in · Pn. From sorted stack to ANY stack in · Pn ? Reverse the sequences we use to sort.

  42. From ANY stack to sorted stack in · Pn. From sorted stack to ANY stack in · Pn. Hence,From ANY stack to ANY stack in · 2Pn.

  43. ((( ))) From ANY stack to ANY stack in · 2Pn. Can you find a faster way than 2Pn flips to go from ANY to ANY?

  44. S:4,3,5,1,2 T:5,2,4,3,1 From ANY Stack S to ANY stack T in · Pn Rename the pancakes in S to be 1,2,3,..,n. Rewrite T using the new naming scheme that you used for S. T will be some list: p(1),p(2),..,p(n). The sequence of flips that brings the sorted stack to p(1),p(2),..,p(n) will bring S to T. 1,2,3,4,5 3,5,1,2,4

  45. 12345678910111213 01345 The Known Pancake Numbers Pn n 7891011131415

  46. P14 Is Unknown • 14! Orderings of 14 pancakes. • 14! = 87,178,291,200

  47. Is This Really Computer Science?

  48. Posed in Amer. Math. Monthly 82 (1) (1975), “Harry Dweighter” a.k.a. Jacob Goodman

  49. (17/16)n  Pn (5n+5)/3 Bill Gates & Christos Papadimitriou: Bounds For Sorting By Prefix Reversal. Discrete Mathematics, vol 27, pp 47-57, 1979.

  50. (15/14)n  Pn (5n+5)/3 H. Heydari & Ivan H. Sudborough. On the Diameter of the Pancake Network. Journal of Algorithms, vol 25, pp 67-94, 1997.

More Related