How To Think Like A Computer Scientist - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

How To Think Like A Computer Scientist

Description:

So starting from ANY stack we can get to the sorted stack using no more than Pn flips. ... Hence it requires at least n flips to sort the original stack. ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 55
Provided by: steve1103
Category:

less

Transcript and Presenter's Notes

Title: How To Think Like A Computer Scientist


1
(No Transcript)
2
Pancakes With A Problem
Steven Rudich
3
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) by grabbing several from the top and
flipping them over, repeating this (varying the
number I flip) as many times as necessary.
4
Developing A NotationTurning pancakes into
numbers
5
Developing A NotationTurning pancakes into
numbers
5
2
3
4
1
6
Developing A NotationTurning pancakes into
numbers
5
2
3
4
1
7
Developing A NotationTurning pancakes into
numbers
52341
8
4 Flips Are Sufficient
12345
52341
43215
23415
14325
9
Algebraic Representation
52341
X The smallest number of flips required
to sort
Upper Bound
? ? X ? ?
Lower Bound
10
Algebraic Representation
52341
X The smallest number of flips required
to sort
Upper Bound
? ? X ? 4
Lower Bound
11
4 Flips Are Necessary
52341
41325
14325
Flip 1 has to put 5 on bottom. Flip 2 must bring
4 to top.
12
? ? X ? 4
Lower Bound
13
4 ? X ? 4
Upper Bound
Lower Bound
14
5th Pancake Number
P5 The number of flips required to sort the
worst case stack of 5 pancakes.
Upper Bound
? ? P5 ? ?
Lower Bound
15
5th Pancake Number
P5 The number of flips required to sort the
worst case stack of 5 pancakes.
Upper Bound
4 ? P5 ? ?
Lower Bound
16
The 5th Pancake Number The MAX of the Xs
120
1
199
3
2
52341
. . . . . . .
X1
4
X2
X3
X119
X120
17
nth Pancake Number
Pn The number of flips required to sort the
worst case stack of n pancakes.
Upper Bound
? ? Pn ? ?
Lower Bound
18
? ? Pn ? ?
What bounds can you prove on Pn?
19
Bring To Top Method
Bring biggest to top. Place it on bottom. Bring
next largest to top. Place second from bottom.
And so on
20
Upper Bound On PnBring To Top Method For n
Pancakes
  • If n1, no work - 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.
21
Better Upper Bound On PnBring To Top Method For
n Pancakes
  • If n2, at most one flip, 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.
22
Bring to top not always optimal for a particular
stack
32145
52341
23145
41325
14325
  • 5 flips, but can be done in 4 flips

23
? ? Pn ? 2n - 3
What bounds can you prove on Pn?
24
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.

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

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

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

Detail This construction only works when ngt3
28
n ? Pn ? 2n - 3
Bring To Top is within a factor of two of optimal
29
n ? Pn ? 2n - 3
So starting from ANY stack we can get to the
sorted stack using no more than Pn flips.
30
From ANY stack to Sorted Stack in Pn
Can you see why we can get from the Sorted stack
to ANY stack in Pn flips?
Reverse the sequences we use to sort.
31
From Sorted Stack to ANY stack in Pn
Can you see why we can get from ANY stack S to
ANY stack T in Pn flips?
32
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
33
The Known Pancake Numbers
Pn
n
  • 12345678910111213
  • 01345

7891011131415
34
P14 Is Unknown
  • 14! Orderings of 14 pancakes.
  • 14! 87,178,291,200

35
General Versus Particular
  • General
  • We proved bounds for all pancake numbers
  • Particular
  • Brute force gave exact bounds for a small set of
    pancake numbers

36
Is This Really Computer Science?
37
Posed in Amer. Math. Monthly 82 (1) (1975),
Harry Dweighter a.k.a. Jacob Goodman
38
(17/16)n ? Pn ? (5n5)/3
Bill Gates Christos Papadimitriou Bounds For
Sorting By Prefix Reversal. Discrete Mathematics,
vol 27, pp 47-57, 1979.
39
(15/14)n ? Pn ? (5n5)/3
H. Heydari H. I. Sudborough On the Diameter of
he Pancake Network. Journal of Algorithms, vol
25, pp 67-94, 1997.
40
Pancake NetworkDefinition 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.

41
Network For n3
213
123
312
321
132
231
42
Network For n4
43
Pancake NetworkMessage Routing Delay
  • What is the maximum distance between two nodes in
    the network?

Pn
44
Pancake NetworkReliability
  • 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.
45
Mutation Distance
46
One Simple Problem
A host of problems and applications at the
frontiers of science
47
(No Transcript)
48
(No Transcript)
49
(No Transcript)
50
Lower Bound On Pn
  • Adjacency in a Stack Any pair of consecutively
    sized pancakes next to each other in the stack.
  • Adjacency Count of a stack number of Adjacencies
    in the stack PLUS 1 more if the largest pancake
    is at the bottom.

51
Examples Of Adjacency Counts
13524
21345
12345
54321
4
4
5
0
52
Adjacency Count Zero(for n gt 3)
2468..n1357..n-1
1357 ..n 2468..n-1
n even
n odd
53
Adjacency Lemma
  • Each flip can raise the Adjacency Count by at
    most one.

A...BC. . . D
B...AC. . . D
54
n ? Pn
  • There is a stack of n pancakes of Adjacency Count
    0. The sorted stack has count n. Each flip
    increases the count by at most 1.
  • Hence it requires at least n flips to sort the
    original stack.
Write a Comment
User Comments (0)
About PowerShow.com