pancakes with a problem! great theoretical ideas in computer science anupam gupta cs 15-251 fall...

76
Pancakes With A Problem! Pancakes With A Problem! Great Theoretical Ideas In Computer Science Great Theoretical Ideas In Computer Science Anupam Anupam Gupta Gupta CS 15-251 Fall CS 15-251 Fall 2O05 2O05 Lecture 1 Lecture 1 Aug 29 Aug 29 th th , 2OO5 , 2OO5 Carnegie Mellon Carnegie Mellon University University

Upload: angelica-owens

Post on 29-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Pancakes With A Problem!Pancakes With A Problem!

Great Theoretical Ideas In Computer ScienceGreat Theoretical Ideas In Computer Science

Anupam Anupam GuptaGupta

CS 15-251 Fall 2O05CS 15-251 Fall 2O05

Lecture 1Lecture 1 Aug 29Aug 29thth, 2OO5, 2OO5 Carnegie Mellon Carnegie Mellon UniversityUniversity

Page 2: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Course StaffCourse Staff

Profs:Profs: Anupam GuptaAnupam Gupta

John LaffertyJohn Lafferty

TAs:TAs: Kanat TangwongsanKanat Tangwongsan

Yinmeng ZhangYinmeng Zhang

http://www.cs.cmu.edu/~15251

Please check the webpages regularly!Please check the webpages regularly!

Page 3: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Please feel free to ask questions!

((( )))

Page 4: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Course DocumentCourse DocumentYou must read this carefully.You must read this carefully.

1.1. Grading formula for the course.Grading formula for the course.a) 40% homework b) 5% in-class quizzesc) 25% in-recitation testsd) 30% final

2.2. Seven points a day Seven points a day late penaltylate penalty..

3.3. Collaboration/Cheating PolicyCollaboration/Cheating Policya) You may NOT share written work.b) We reuse homework problems.

Page 5: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Pancakes With A Problem!Pancakes With A Problem!

Great Theoretical Ideas In Computer ScienceGreat Theoretical Ideas In Computer Science

Anupam Anupam GuptaGupta

CS 15-251 Fall 2O05CS 15-251 Fall 2O05

Lecture 1Lecture 1 Aug 29Aug 29thth, 2OO5, 2OO5 Carnegie Mellon Carnegie Mellon UniversityUniversity

Page 6: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

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 7: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Developing A Notation:Developing A Notation:Turning pancakes into numbersTurning pancakes into numbers

Page 8: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Developing A Notation:Developing A Notation:Turning pancakes into numbersTurning pancakes into numbers

Page 9: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Developing A Notation:Developing A Notation:Turning pancakes into numbersTurning pancakes into numbers

2

3

4

5

1

Page 10: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Developing A Notation:Developing A Notation:Turning pancakes into numbersTurning pancakes into numbers

52341

Page 11: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

How do we sort this stack?How do we sort this stack?How many flips do we need?How many flips do we need?

52341

Page 12: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

How do we sort this stack?How do we sort this stack?How many flips do we need?How many flips do we need?

52341

Page 13: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie
Page 14: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

4 Flips Are Sufficient4 Flips Are Sufficient

12345

52341

43215

23415

14325

Page 15: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Algebraic RepresentationAlgebraic Representation

52341

X = The smallest number of flips required to sort:

? X ?Upper Bound

Lower Bound

Page 16: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Algebraic RepresentationAlgebraic Representation

52341

X = The smallest number of flips required to sort:

? X 4Upper Bound

Lower Bound

Page 17: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Better Upper/Lower Bound?Better Upper/Lower Bound?

? X 4

Page 18: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

4 Flips are necessary in this case4 Flips are necessary in this case

52341

41325

14325

Flip 1 has to put 5 on bottom

Flip 2 must bring 4 to top.

Page 19: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Upper Bound

Lower Bound

4 X 4

X = 4

Page 20: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

55thth Pancake Number Pancake Number

The number of flips required to sort the

worst case stack of 5 pancakes.

? P5 ?Upper Bound

Lower Bound

P5 =

Page 21: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

55thth Pancake Number Pancake Number

The number of flips required to sort the

worst case stack of 5 pancakes.

4 P5 ?Upper Bound

Lower Bound

P5 =

Page 22: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

. . . . . . . . . .

The 5The 5thth Pancake Number: Pancake Number: The MAX of the X’sThe MAX of the X’s

120

1199

32

X1 X2 X3 X119 X120

52341

4

Page 23: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

The 5The 5thth Pancake Number: Pancake Number: The MAX of the X’sThe MAX of the X’s

120

199

3 . . . . . . . . . .

X1 X2 X3 X119 X120

52341

4

12345

54321

Page 24: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

120

199

3 . . . . . . . . . .

X1 X2 X3 X119 X120

52341

4

12345

54321

P5 = MAX over s2 stacks of 5 of MIN # of flips to sort s

Page 25: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

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.

Page 26: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

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.

Page 27: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Be Cool.

Learn Math-speak.

Pn = The number of flips required to sort

a worst-case stack of n pancakes.

Page 28: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

What is Pn for small n?

Can you do

n = 0, 1, 2, 3 ?

Page 29: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Initial Values Of Pn

n 0 1 2 3

Pn 0 0 1 3

Page 30: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

PP33 = 3 = 3

113322 requires 3 Flips, hence requires 3 Flips, hence PP33 ≥ 3. ≥ 3.

ANYANY stack of 3 can be done by stack of 3 can be done by getting the big one to the bottom (getting the big one to the bottom (≤≤ 2 flips), and then using 2 flips), and then using ≤≤ 1 extra flip 1 extra flip to handle the top two. Hence, to handle the top two. Hence, PP33 = 3. = 3.

Page 31: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

nnthth Pancake Number Pancake Number

The number of flips required to sort a worst

case stack of n pancakes.

? Pn ?Upper Bound

Lower Bound

Pn =

Page 32: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Bracketing:Bracketing:What are the best lower and upper What are the best lower and upper

bounds that I can prove?bounds that I can prove?

≤ f(x) ≤[ ]

Page 33: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

? Pn ?

Take a few minutes to try

and prove upper and

lower bounds on Pn,

for n > 3.

Page 34: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Bring biggest to top. Place it on bottom.

Bring next largest to top.

Place second from bottom.

And so on…

Bring-to-top MethodBring-to-top Method

Page 35: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Upper Bound On PUpper Bound On Pnn::Bring To Top Method For n PancakesBring To Top Method For n Pancakes

If n=1, no work required - we are done!If n=1, no work required - we are done!

Otherwise, Otherwise, flip pancake n to topflip pancake n to top and and then then flip it to position nflip it to position n..

Now use: Now use: Bring To Top Method

For n-1 Pancakes

Total Cost: at most 2(n-1) = 2n –2 flips.

Page 36: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

BetterBetter Upper Bound On P Upper Bound On Pnn::Bring To Top Method For n PancakesBring To Top Method For n Pancakes

If n=2, If n=2, at most one flipat most one flip and we are done. and we are done.

Otherwise, Otherwise, flip pancake n to topflip pancake n to top and and then then flip it to position nflip it to position n..

Now use: Now use: Bring To Top Method

For n-1 Pancakes

Total Cost: at most 2(n-2) + 1 = 2n –3 flips.

Page 37: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Bring to top not always optimal Bring to top not always optimal for a particular stackfor a particular stack

Bring-to–top takes 5 flips, Bring-to–top takes 5 flips,

but we can do in 4 flipsbut we can do in 4 flips

32145

52341

23145

41325

14325

Page 38: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

? Pn 2n-3

What other

bounds can you prove on

Pn?

Page 39: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Better Upper/Lower Bounds?Better Upper/Lower Bounds?

Page 40: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

9

16

Suppose a stack S contains a pair Suppose a stack S contains a pair of adjacent pancakes that will not of adjacent pancakes that will not be adjacent in the sorted stack. be adjacent in the sorted stack.

Any sequence of flips that sorts Any sequence of flips that sorts stack S must involve one flip that stack S must involve one flip that inserts the spatula between that inserts the spatula between that pair and breaks them apart. pair and breaks them apart.

Breaking Apart ArgumentBreaking Apart Argument

Page 41: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

9

16

Suppose a stack S contains a pair Suppose a stack S contains a pair of adjacent pancakes that will not of adjacent pancakes that will not be adjacent in the sorted stack. be adjacent in the sorted stack.

Any sequence of flips that sorts Any sequence of flips that sorts stack S must involve one flip that stack S must involve one flip that inserts the spatula between that inserts the spatula between that pair and breaks them apart. pair and breaks them apart.

Furthermore, this same principle is Furthermore, this same principle is true of the “pair” formed by the true of the “pair” formed by the bottom pancake of S and the bottom pancake of S and the plate.plate.

Breaking Apart ArgumentBreaking Apart Argument

Page 42: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

n n P Pnn2468..n1357..

n-1

S

Suppose n is evenSuppose n is even. . S contains n pairs that S contains n pairs that will need to be broken will need to be broken apart during any apart during any sequence that sorts stack sequence that sorts stack S.S.

Page 43: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

n n P Pnn

Suppose n is evenSuppose n is even. . S contains n pairs that S contains n pairs that will need to be broken will need to be broken apart during any apart during any sequence that sorts stack sequence that sorts stack S.S. 2

1

S

Detail: This construction only works when n>2

Page 44: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

n n P Pnn

Suppose n is oddSuppose n is odd. . S contains n pairs that S contains n pairs that will need to be broken will need to be broken apart during any apart during any sequence that sorts stack sequence that sorts stack S.S.

1357..n2468..

n-1

S

Page 45: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

n n P Pnn

132

S

Detail: This construction only works when n>3

Suppose n is oddSuppose n is odd. . S contains n pairs that S contains n pairs that will need to be broken will need to be broken apart during any apart during any sequence that sorts stack sequence that sorts stack S.S.

Page 46: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

n Pn 2n – 3 for n ≥ 3

Bring To Top is within a factor

of two of optimal!

Page 47: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

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 48: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

From ANY stack to sorted stack in ≤ Pn.

Reverse the sequences we use

to sort.

From sorted stack to ANY stack in ≤ Pn ?

((( )))

Page 49: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Hence,

From ANY stack to ANY stack in ≤ 2Pn.

From ANY stack to sorted stack in ≤ Pn.

From sorted stack to ANY stack in ≤ Pn ?

Page 50: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

From ANY stack to ANY stack in ≤ 2Pn.

Can you find a faster way

than 2Pn flips to go

from ANY to ANY?

((( )))

Page 51: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Any-to-Any?Any-to-Any?

S: 4,3,5,1,2

T: 5,2,4,3,1

Page 52: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

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: (1),(2),..,(n).

The sequence of flips that brings the sorted stack to (1),(2),..,(n) will bring S to T.

S: 4,3,5,1,2

T: 5,2,4,3,1

1,2,3,4,5 3,5,1,2,4

Page 53: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

The Known Pancake NumbersThe Known Pancake Numbers

112233445566778899

1010111112121313

0011334455

n Pn

778899

10101111131314141515

Page 54: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

PP1414 Is Unknown Is Unknown

14! Orderings of 14 pancakes.14! Orderings of 14 pancakes.

14! = 87,178,291,20014! = 87,178,291,200

Page 55: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Is This Really Computer Science?Is This Really Computer Science?

Page 56: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

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

Page 57: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

(17/16)n Pn (5n+5)/3

William Gates & Christos

Papadimitriou

Bounds For Sorting By Prefix Reversal.

Discrete Mathematics, vol 27,

pp 47-57, 1979.

Page 58: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

(15/14)n Pn (5n+5)/3

H. Heydari &H. I. Sudborough

On the Diameter of the Pancake

Network. Journal of Algorithms, vol 25,

pp 67-94, 1997.

Page 59: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

PermutationPermutation

Any particular ordering of all n elements of Any particular ordering of all n elements of an n element set S, is called a an n element set S, is called a permutationpermutation on the set S.on the set S.

Example: S = {1, 2, 3, 4, 5}Example: S = {1, 2, 3, 4, 5}

One possible permutation: 5 3 2 4 1One possible permutation: 5 3 2 4 1

5*4*3*2*1 = 120 possible permutations on 5*4*3*2*1 = 120 possible permutations on SS

Page 60: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

PermutationPermutation

Any particular ordering of all n elements Any particular ordering of all n elements of an n element set S, is called a of an n element set S, is called a permutationpermutation on the set S. on the set S.

Each different stack of n pancakes is one Each different stack of n pancakes is one of the permutations on [1..n].of the permutations on [1..n].

Page 61: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Representing A PermutationRepresenting A Permutation

We have many ways to specify a permutation on S. We have many ways to specify a permutation on S. Here are two methods:Here are two methods:

1)1) We list a sequence of all the elements of [1..n], We list a sequence of all the elements of [1..n], each one written exactly once. each one written exactly once. Ex: 6 4 5 2 1 3Ex: 6 4 5 2 1 3

2)2) We give a function We give a function on S such that on S such that (1) (1) (2) (2) (3) .. (3) .. (n) is a sequence that (n) is a sequence that lists [1..n], each one exactly once. lists [1..n], each one exactly once. Ex: Ex: (1)=6 (1)=6 (2)=4 (2)=4 (3) = 5 (3) = 5 (4) = 2 (4) = 2 (4) = 1 (4) = 1 (6) (6) =3=3

Page 62: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

A Permutation is a NOUNA Permutation is a NOUN

An ordering S of a stack of pancakes An ordering S of a stack of pancakes is a permutation. is a permutation.

Page 63: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

A Permutation is a NOUN.A Permutation is a NOUN.A permutation can also be a VERB.A permutation can also be a VERB.

An ordering S of a stack of pancakes An ordering S of a stack of pancakes is a permutation.is a permutation.

We can permute S to obtain a new We can permute S to obtain a new stack S’.stack S’.

PermutePermute also means to rearrange so also means to rearrange so as to obtain a permutation of the as to obtain a permutation of the original.original.

Page 64: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Permute A Permutation.Permute A Permutation.

I start with a I start with a permutation S of permutation S of

pancakes. pancakes. I continue to use a flip I continue to use a flip operation to permute operation to permute

my current permutation, my current permutation, so as to obtain the so as to obtain the

sorted permutation.sorted permutation.

Page 65: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

There are n! = 1*2*3*4*…*n There are n! = 1*2*3*4*…*n permutations on n elements.permutations on n elements.

Easy proof in the first counting Easy proof in the first counting lecture.lecture.

Page 66: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Pancake Network:Pancake Network:Definition For n! NodesDefinition For n! Nodes

For each node, assign it the name of For each node, assign it the name of one of the n! stacks of n pancakes.one of the n! stacks of n pancakes.

Put a wire between two nodes if they Put a wire between two nodes if they are one flip apart.are one flip apart.

Page 67: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Network For n=3Network For n=3

123

321

213

312

231

132

Page 68: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Network For n=4Network For n=4

Page 69: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Pancake Network:Pancake Network:Message Routing Delay Message Routing Delay

What is the maximum distance What is the maximum distance between two nodes in the network?between two nodes in the network?

Pn

Page 70: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Pancake Network:Pancake Network:ReliabilityReliability

If up to n-2 nodes get hit by If up to n-2 nodes get hit by lightning the network remains lightning the network remains connected, even though each node connected, even though each node is connected to only n-1 other is connected to only n-1 other nodes. nodes.

The Pancake Network is optimally reliable for its number of edges and

nodes.

Page 71: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Mutation DistanceMutation Distance

Page 72: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

High Level PointHigh 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.

Page 73: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

One “Simple” Problem

A host of problems and applications

at the frontiers of

science

Page 74: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Study Bee

Please read the Please read the course document course document carefully.carefully.

You must hand in aYou must hand in asigned cheating signed cheating policy page.policy page.

Page 75: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

Study Bee

Definitions of:nth pancake numberlower boundupper boundpermutation

Proof of:ANY to ANY in ≤ Pn

Important Technique:Bracketing

Page 76: Pancakes With A Problem! Great Theoretical Ideas In Computer Science Anupam Gupta CS 15-251 Fall 2O05 Lecture 1 Aug 29 th, 2OO5 Aug 29 th, 2OO5 Carnegie

ReferencesReferences

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