1 / 76

Pancakes With A Problem!

Pancakes With A Problem!. Course Staff. Profs: Anupam Gupta John Lafferty TAs: Kanat Tangwongsan Yinmeng Zhang http://www.cs.cmu.edu/~15251 Please check the webpages regularly!. ((( ))). Please feel free to ask questions!. Course Document You must read this carefully.

kinah
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 • Profs: Anupam Gupta • John Lafferty • TAs: Kanat Tangwongsan • Yinmeng Zhang • http://www.cs.cmu.edu/~15251 • Please check the webpages regularly!

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

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

  5. Pancakes With A Problem!

  6. 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.

  7. Developing A Notation:Turning pancakes into numbers

  8. Developing A Notation:Turning pancakes into numbers

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

  10. Developing A Notation:Turning pancakes into numbers 52341

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

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

  13. 4 Flips Are Sufficient 52341 14325 23415 43215 12345

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

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

  16. Better Upper/Lower Bound? ?  X  4

  17. 4 Flips are necessary in this case 52341 14325 41325 Flip 1 has to put 5 on bottom Flip 2 must bring 4 to top.

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

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

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

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

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

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

  24. Pn = MAX over s2 stacks of n pancakes of MIN # of flips to sort s Pn = The number of flips required to sort the worst-case stack of n pancakes.

  25. Pn = MAX over s2 stacks of n pancakes of MIN # of flips to sort s Pn = The number of flips required to sort a worst-case stack of n pancakes.

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

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

  28. Initial Values Of Pn

  29. P3 = 3 • 132 requires 3 Flips, hence P3 ≥ 3. • ANY stack of 3 can be done by getting the big one to the bottom (≤ 2 flips), and then using ≤ 1 extra flip to handle the top two. Hence, P3 = 3.

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

  31. ≤ f(x) ≤ ] [ Bracketing:What are the best lower and upper bounds that I can prove?

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

  33. Bring-to-top Method Bring biggest to top. Place it on bottom. Bring next largest to top.Place second from bottom. And so on…

  34. Upper Bound On Pn:Bring To Top Method For n Pancakes • If n=1, no work required - we are done! • Otherwise, 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.

  35. Better Upper Bound On Pn:Bring To Top Method For n Pancakes • If n=2, at most one flip and we are done. • Otherwise, 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.

  36. Bring to top not always optimal for a particular stack • Bring-to–top takes 5 flips, • but we can do in 4 flips 52341 14325 41325 23145 32145

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

  38. Better Upper/Lower Bounds?

  39. 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.

  40. 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. • Furthermore, this same principle is true of the “pair” formed by the bottom pancake of S and the plate.

  41. n  Pn S 2468..n1357..n-1 • Suppose n is even. S contains n pairs that will need to be broken apart during any sequence that sorts stack S.

  42. n  Pn • Suppose n is even. S contains n pairs that will need to be broken apart during any sequence that sorts stack S. S 21 Detail: This construction only works when n>2

  43. n  Pn S 1357..n 2468..n-1 • Suppose n is odd. S contains n pairs that will need to be broken apart during any sequence that sorts stack S.

  44. n  Pn S • Suppose n is odd. S contains n pairs that will need to be broken apart during any sequence that sorts stack S. 132 Detail: This construction only works when n>3

  45. n  Pn 2n – 3 for n ≥ 3 Bring To Top is within a factor of two of optimal!

  46. n  Pn 2n – 3 for n ≥ 3 Starting from ANY stack we can get to the sorted stack using no more than Pn flips.

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

  48. 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.

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

More Related