compsci 102 spring 2012 - duke university€¦ · pancakes with a problem! lecture adapted from...
TRANSCRIPT
![Page 1: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/1.jpg)
CompSci 102 Spring 2012
Prof. Rodger January 11, 2012
![Page 2: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/2.jpg)
Announcements/Logistics
No recitation Friday the 13th (tomorrow) Read the information/policies about this
course on the course web site. www.cs.duke.edu/courses/spring12/cps102
![Page 3: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/3.jpg)
Please feel free to ask questions!
((( )))
![Page 4: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/4.jpg)
Pancakes With A Problem!
Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich.
![Page 5: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/5.jpg)
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.
![Page 6: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/6.jpg)
Developing A Notation: Turning pancakes into numbers
![Page 7: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/7.jpg)
Developing A Notation: Turning pancakes into numbers
1
2
3
4
5
![Page 8: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/8.jpg)
Developing A Notation: Turning pancakes into numbers
2
3
4
5
1
![Page 9: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/9.jpg)
Developing A Notation: Turning pancakes into numbers
52341
![Page 10: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/10.jpg)
How do we sort this stack? How many flips do we need?
5 2 3 4 1
![Page 11: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/11.jpg)
4 Flips Are Sufficient
12345
52341
43215
23415
14325
![Page 12: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/12.jpg)
Algebraic Representation
52341
X = The smallest number of flips required to sort:
? ≤ X ≤ ? Upper Bound
Lower Bound
![Page 13: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/13.jpg)
Algebraic Representation
52341
X = The smallest number of flips required to sort:
? ≤ X ≤ 4 Upper Bound
Lower Bound
![Page 14: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/14.jpg)
4 Flips Are Necessary
52341
41325
14325
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).
![Page 15: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/15.jpg)
? ≤ X ≤ 4 Lower Bound
![Page 16: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/16.jpg)
Upper Bound
Lower Bound
4 ≤ X ≤ 4
X = 4
![Page 17: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/17.jpg)
5th Pancake Number
P5 = The number of flips required to sort the worst case stack of 5 pancakes.
? ≤ P5 ≤ ? Upper Bound
Lower Bound
![Page 18: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/18.jpg)
5th Pancake Number
P5 = The number of flips required to sort the worst case stack of 5 pancakes.
4 ≤ P5 ≤ ? Upper Bound
Lower Bound
![Page 19: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/19.jpg)
5th Pancake Number
P5 = The number of flips required to sort the worst case stack of 5 pancakes.
4 ≤ P5 ≤ ? Upper Bound
Lower Bound
How many different pancake stacks are there with 5 unique
elements?
![Page 20: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/20.jpg)
The 5th Pancake Number: The MAX of the X’s
120
1 119
3 2 . . . . . . .
X1 X2 X3 X119 X120
52341
4
![Page 21: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/21.jpg)
P5 = MAX over s stacks of 5 of MIN # of flips to sort s
120
1 119
3 2 . . . . . . .
X1 X2 X3 X119 X120
52341
4
![Page 22: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/22.jpg)
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.
![Page 23: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/23.jpg)
Be Cool.
Learn Math-speak.
Pn = The number of flips required to sort a worst-case stack of n pancakes.
![Page 24: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/24.jpg)
What is Pn for small n?
Can you do n = 0, 1, 2, 3 ?
![Page 25: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/25.jpg)
Initial Values Of Pn
n 0 1 2 3
Pn 0 0 1 3
![Page 26: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/26.jpg)
P3 = 3
1 3 2 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.
![Page 27: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/27.jpg)
nth Pancake Number
Pn = Number of flips required to sort a worst case stack of n pancakes.
? ≤ Pn ≤ ? Upper Bound
Lower Bound
![Page 28: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/28.jpg)
? ≤ Pn ≤ ?
Take a few minutes to try
and prove bounds on Pn,
for n>3.
![Page 29: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/29.jpg)
Bring To Top Method
Bring biggest to top. Place it on bottom. Bring
next largest to top. Place second from bottom. And
so on…
![Page 30: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/30.jpg)
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.
![Page 31: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/31.jpg)
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.
![Page 32: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/32.jpg)
Bring to top not always optimal for a particular stack
5 flips, but can be done in 4 flips
32145
52341
23145
41325
14325
![Page 33: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/33.jpg)
? ≤ Pn ≤ 2n - 3
What bounds can you prove on
Pn?
![Page 34: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/34.jpg)
9
16
Breaking Apart Argument
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.
![Page 35: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/35.jpg)
9 16
Breaking Apart Argument
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.
![Page 36: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/36.jpg)
n ≤ Pn
Suppose n is even. Such a stack S contains n pairs that must be broken apart during any sequence that sorts stack S.
2 4 6 8 . . n 1 3 5 7 . .
n-1
S
![Page 37: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/37.jpg)
n ≤ Pn
Suppose n is even. Such a stack S contains n pairs that must be broken apart during any sequence that sorts stack S.
21
S
Detail: This construction only works when n>2
![Page 38: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/38.jpg)
n ≤ Pn
Suppose n is odd. Such a stack S contains n pairs that must be broken apart during any sequence that sorts stack S.
1 3 5 7 . . n 2 4 6 8 . .
n-1
S
![Page 39: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/39.jpg)
n ≤ Pn
Suppose n is odd. Such a stack S contains n pairs that must be broken apart during any sequence that sorts stack S.
132
S
Detail: This construction only works when n>3
![Page 40: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/40.jpg)
n ≤ Pn ≤ 2n – 3 (for n ≥ 3)
Bring To Top is within a factor
of two of optimal!
![Page 41: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/41.jpg)
So starting from ANY stack we can get to the sorted stack using no more than Pn flips.
n ≤ Pn ≤ 2n – 3 (for n ≥ 3)
![Page 42: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/42.jpg)
From ANY stack to sorted stack in · Pn.
Reverse the sequences we use
to sort.
From sorted stack to ANY stack in · Pn ?
((( )))
![Page 43: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/43.jpg)
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.
![Page 44: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/44.jpg)
From ANY stack to ANY stack in · 2Pn.
Can you find a faster way
than 2Pn flips to go from
ANY to ANY?
((( )))
![Page 45: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/45.jpg)
From ANY Stack S to ANY stack T in · Pn
Rename the pancakes in T to be 1,2,3,..,n. T : 5, 2, 4, 3, 1 Tnew : 1, 2, 3, 4, 5 π(5), π(2), π(4), π(3), π(1) Rewrite S using π(1),π(2),..,π(n) S : 4, 3, 5, 1, 2 Snew : π(4), π(3), π(5), π(1), π(2) 3, 4, 1, 5, 2
![Page 46: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/46.jpg)
From ANY Stack S to ANY stack T in · Pn
T : 5, 2, 4, 3, 1 Tnew : 1, 2, 3, 4, 5 S : 4, 3, 5, 1, 2 Snew : 3, 4, 1, 5, 2
The sequence of steps that brings Snew to Tnew (sorted stack) also brings S to T
![Page 47: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/47.jpg)
The Known Pancake Numbers
1 2 3 4 5 6 7 8 9
10 11 12 13
0 1 3 4 5
n Pn
7 8 9 10 11 13 14 15
![Page 48: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/48.jpg)
P14 Is Unknown
14! Orderings of 14 pancakes. 14! = 87,178,291,200
![Page 49: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/49.jpg)
Is This Really Computer Science?
![Page 50: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/50.jpg)
Posed in Amer. Math. Monthly 82 (1) (1975), “Harry Dweighter” a.k.a. Jacob Goodman
![Page 51: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/51.jpg)
(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.
![Page 52: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/52.jpg)
(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.
![Page 53: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/53.jpg)
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} Example permutation: 5 3 2 4 1 120 possible permutations on S
![Page 54: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/54.jpg)
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].
![Page 55: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/55.jpg)
Representing A Permutation
We have many choices of how to specify a permutation on S. Here are two methods:
1) List a sequence of all elements of [1..n],
each one written exactly once. Ex: 6 4 5 2 1 3
2) Give a function π on S s.t. π(1) π(2) π(3) .. π(n) is a sequence listing [1..n], each one exactly once.
Ex: π(6)=3 π(4)=2 π(5)=1 π(2)=4 π(1)=6 π(3)=5
![Page 56: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/56.jpg)
A Permutation is a NOUN
An ordering S of a stack of pancakes is a permutation.
![Page 57: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/57.jpg)
A Permutation is a NOUN
An ordering S of a stack of pancakes is a permutation. We can permute S to obtain a new stack S’. Permute also means to rearrange so as to obtain a permutation of the original.
Permute is also a VERB
![Page 58: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/58.jpg)
Permute A Permutation.
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.
![Page 59: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/59.jpg)
Ultra-Useful Fact
There are n! = n*(n-1)*(n-2) … 3*2*1 permutations on n elements. Proof: in the first counting lecture.
![Page 60: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/60.jpg)
Pancake Network
This network has n! nodes Assign each node the name of one of the possible n! stacks of pancakes. Put a wire between two nodes if they are one flip apart.
![Page 61: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/61.jpg)
Network For n=3
123
321
213
312
231
132
![Page 62: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/62.jpg)
Network For n=4
![Page 63: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/63.jpg)
Pancake Network: Routing Delay
What is the maximum distance between two nodes in the pancake network?
Pn
![Page 64: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/64.jpg)
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.
![Page 65: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/65.jpg)
Mutation Distance
Combinatorial “puzzle” to find the shortest series of reversals to transform
one genome into another
![Page 66: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/66.jpg)
Journal of the ACM, Vol. 46, No 1, 1999. Over 350 citations!
![Page 67: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/67.jpg)
One “Simple” Problem
A host of problems and applications at the frontiers
of science
![Page 68: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/68.jpg)
Study Bee
Definitions of: nth pancake number lower bound upper bound permutation Proof of: ANY to ANY in · Pn
![Page 69: CompSci 102 Spring 2012 - Duke University€¦ · Pancakes With A Problem! Lecture adapted from Bruce Maggs/Lecture developed at Carnegie Mellon, primarily by Prof. Steven Rudich](https://reader030.vdocuments.net/reader030/viewer/2022040308/5ed832300fa3e705ec0e0385/html5/thumbnails/69.jpg)
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 the Pancake Network. Journal of Algorithms, vol 25, pp 67-94, 1997