Download presentation
Presentation is loading. Please wait.
Published byJade Green Modified over 9 years ago
2
Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2006 Lecture 1 Aug 29 th, 2OO6 Aug 29 th, 2OO6 Carnegie Mellon University
3
Fall 06 Course Staff Profs:Anupam Gupta John Lafferty John Lafferty TAs:Albert Sheu Andreas Krause Todd Phillips Webpage:http://www.cs.cmu.edu/~15251 Bboard:academic.cs.15-251 Please check the webpages and Bboards regularly!
4
If Suzie gets 60,90,80 in her tests, how many total test points will she have in her final grade? ((½ 60) + 90 + 80) × (30/250) = 24 Read the Course Document! Homework 35% Final 30% In-Class Quizzes 5% 3 In-Recitation Tests 30% Lowest homework grade is dropped Lowest test grade is worth half
5
Homework Seven points per day late penalty No homework will be accepted more than seven days late HOMEWORK #1 IS OUT TODAY! Homework will go out every Tuesday and is due the Tuesday after. Any written answers must be typset and given as a PDF file.
6
Extra Credit Tokens Each token is worth: A) One problem (up to 20 points) on a subsequent assignment. Each correct solution to an Extra Credit problem gives you a “token” or B) One late submission up to 3 days late on a subsequent assignment without any penalty. You have to explicitly tell us when you are using your token.
7
Clarifications? To use option B, your HW submission must still be at most 7 days late, and must say something like “I am using one token for this late submission, which is N days late”. We then only take off (N-3) × 7 points. To use option A, your HW submission must say: “I am using one token for problem XYZ (worth at most 20 pts).”
8
Collaboration + Cheating You may NOT share written work You may NOT use Google, or solutions to previous years’ homework You MUST read and sign the class honor code (last page of Course Document).
9
Textbook There is NO textbook for this class Students from previous years tell us that it’s imperative to come to class
10
((( ))) Feel free to ask questions
11
Pancakes With A Problem! Lecture 1 Aug 29 th, 2OO6
12
He does this by grabbing several from the top and flipping them over, repeating this (varying the number he flips) as many times as necessary The chef at our place is sloppy: when he prepares pancakes, they come out all different sizes When 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)
13
Developing A Notation: Turning pancakes into numbers 5 2 3 4 1 5234152341
14
How do we sort this stack? How many flips do we need? 5234152341
15
How many flips suffice? 5234152341
16
4 Flips Are Sufficient 1234512345 5234152341 4321543215 2341523415 1432514325
17
Best Way to Sort? 5234152341 X = Smallest number of flips required to sort: ? X ? Upper Bound Lower Bound 4
18
Four Flips Are Necessary 5234152341 4132541325 1432514325 Flip 2 must bring 4 to top (if it didn’t, we would spend more than 3) Flip 1 has to put 5 on bottom (if it didn’t, we would spend more than 3) Suppose 3 flips sufficed: But now we cant finish with Flip 3
19
Upper Bound Lower Bound 4 X 4 X = 4
20
Our First Theorem One can sort in 4 flips, and this is optimal. 5234152341
21
Food for thought… Can every stack of 5 pancakes be sorted in 4 flips? Does there exist a 5-stack that requires at least 5 flips? In general, how difficult can the cook make things for the waiter?
22
Two “adversaries” If the cook chose the worst possible stack of 5 pancakes and the waiter sorted it in the fewest possible flips, how many flips would it take?
23
5 th Pancake Number ? P 5 ? Upper Bound Lower Bound P 5 = The least number of flips required to sort the worst case stack of 5 pancakes.
24
5 th Pancake Number The least number of flips required to sort the worst case stack of 5 pancakes. 4 P 5 ? Upper Bound Lower Bound P 5 =
25
j....... The 5 th Pancake Number: The MAX of the X’s 120120 1 199199 32 X1X1 X2X2 X3X3 X 119 X 120 5234152341 XjXj 5432154321 1234512345 the fewest number of flips to sort stack 1 (best strategy for waiter given this stack)
26
120120 199199 3..... 0 1X3X3 X 119 X 120 5234152341 4 1234512345 5432154321 P 5 = MAX over s 2 stacks of 5 of MIN # of flips to sort s
27
P n = The least number of flips required to sort the worst-case stack of n pancakes.
28
“a”, not “the”
29
What is P n for small n? Can you do n = 0, 1, 2, 3 ?
30
Initial Values of P n n3012 PnPn 0013
31
P 3 = 3 132132 requires 3 Flips, hence ANY 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 two P 3 ≥ 3
32
Bracketing: What are the best lower and upper bounds that I can prove? ≤ f(x) ≤ [ ]
33
? P n ? Take a few minutes to try and prove upper and lower bounds on P n, for n > 3.
34
Bring biggest to top Place it on bottom Bring next largest to top Place second from bottom And so on… Bring-to-top Method
35
Upper Bound On P n : 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
36
Better Upper Bound On P n : 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
37
? P n 2n-3
38
Given any stack, does Bring-to-Top do the fewest flips possible for this given stack?
39
No. Bring-to-top not always optimal for a particular stack Bring-to–top takes 5 flips, but we can do in 4 flips 3214532145 5234152341 2314523145 4132541325 1432514325
40
? P n 2n-3 What other bounds can you prove on P n ?
41
9 16 Breaking Apart Argument Suppose a stack S has a pair of adjacent pancakes that will not be adjacent in the sorted stack Any sequence of flips that sorts stack S must have one flip that inserts the spatula between that pair and breaks them apart Furthermore, this is true of the “pair” formed by the bottom pancake of S and the plate
42
n P n 2 4 6 8.. n 1 3 5.. n-1 S S contains n pairs that will need to be broken apart during any sequence that sorts it Suppose n is even 2121 Detail: This construction only works when n>2
43
n P n 1 3 5 7.. n 2 4 6.. n-1 S S contains n pairs that will need to be broken apart during any sequence that sorts it Suppose n is odd 132132 Detail: This construction only works when n>3
44
n P n 2n – 3 for n > 3 Bring-to-top is within a factor of 2 of optimal!
45
From ANY stack to sorted stack in ≤ P n Reverse the sequences we use to sort From sorted stack to ANY stack in ≤ P n ? ((( ))) Hence, from ANY stack to ANY stack in ≤ 2P n
46
From ANY stack to ANY stack in ≤ 2P n. Can you find a faster way than 2P n flips to go from ANY to ANY? ((( )))
47
ANY Stack S to ANY stack T in ≤ P n S: 4,3,5,1,2T: 5,2,4,3,1 1,2,3,4,5 3,5,1,2,4 Rename 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”
48
The Known Pancake Numbers 0134501345 n PnPn 7 8 9 10 11 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13
49
P 14 is Unknown 1 2 3 4 … 13 14 = 14! orderings of 14 pancakes 14! = 87,178,291,200
50
Is This Really Computer Science?
51
Posed in Amer. Math. Monthly 82 (1) (1975), “Harry Dweighter” a.k.a. Jacob Goodman Sorting By Prefix Reversal
52
(17/16)n P n (5n+5)/3 William H. Gates and Christos Papadimitriou. Bounds For Sorting By Prefix Reversal. Discrete Mathematics, vol 27, pp 47-57, 1979.
53
(15/14)n P n (5n+5)/3 H. Heydari and H. I. Sudborough. On the Diameter of the Pancake Network. Journal of Algorithms, vol 25, pp 67-94, 1997.
54
How many different stacks of n pancakes are there? n! = 1 x 2 x 3 x … x n
55
Permutation Any particular ordering of all n elements of an n element set S, is called a permutation on the set S. Example: S = {1, 2, 3, 4, 5} One possible permutation: 5 3 2 4 1 5*4*3*2*1 = 120 possible permutations on S
56
Permutation Any particular ordering of all n elements of an n element set S, is called a permutation on the set S. Each different stack of n pancakes is one of the permutations on [1..n].
57
A Permutation is a NOUN. A permutation can also be a VERB. I start with a permutation S of pancakes. I continue to use a flip operation to permute my current permutation, so as to obtain the sorted permutation.
58
There are n! permutations of n elements. n! = 1 x 2 x 3 x … x n
59
Pancake Network: Definition For n! Nodes For each node, assign it the name of one of the n! stacks of n pancakes Put a wire between two nodes if they are one flip apart
60
Network For n=3 123123 321321 213213 312312 231231 132132
61
Network For n=4
62
Pancake Network: Message Routing Delay What is the maximum distance between two nodes in the network? PnPn
63
Pancake Network: Reliability If up to n-2 nodes get hit by lightning the network remains connected, even though each node is connected to only n-1 other nodes. The Pancake Network is optimally reliable for its number of edges and nodes.
64
Mutation Distance
65
High Level Point Computer Science is not merely about computers and programming, it is about mathematically modeling our world, and about finding better and better ways to solve problems. This lecture is a microcosm of this exercise.
66
One “Simple” Problem A host of problems and applications at the frontiers of science
67
Study Bee Please read the course document carefully. You must hand in a signed collaboration policy page.
68
Study Bee Definitions of: nth pancake number lower bound upper bound permutation Proof of: ANY to ANY in ≤ P n Important Technique: Bracketing
69
References Bill Gates & Christos Papadimitriou: Bounds For Sorting By Prefix Reversal. Discrete Mathematics, vol 27, pp 47-57, 1979. H. Heydari & H. I. Sudborough: On the Diameter of he Pancake Network. Journal of Algorithms, vol 25, pp 67-94, 1997
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.