-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.idyll
368 lines (304 loc) · 15.7 KB
/
index.idyll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
[meta
title:"Blueberry Pancakes"
description:"A toy algorithms problem."
shareImageUrl:"https://idyll.pub/post/blueberry-pancakes-28b1a2e1a8986c44ac091f08/static/images/share.png"
shareImageWidth:"1600"
shareImageHeight:"800"
/]
[Header
title:"Blueberry Pancakes"
subtitle:"A toy algorithms problem"
authors:`[
{ name: "Caleb Robinson", link: "http://calebrob.com/" },
{ name: "Fred Hohman", link: "https://fredhohman.com" }
]`
/]
[Analytics google:"UA-42146340-1" /]
[script src:"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"/]
Algorithms are pervasive in modern society, and brain teaser puzzles are a great way to get more familiar with algorithmic thinking!
In this article, we will pose a deceptively simple problem about flipping pancakes and walkthrough the process of constructing a formal proof.
For fans of algorithms, proofs, and breakfast-themed mathematics problems. 🥞
## What's the pancake problem?
[Aside]
[image style:`{width: '100%', border: '3px solid #b45326'}` src:"static/images/stack.jpg" /]
*Picture the above, but stacked 100 pancakes tall!*
[/Aside]
You have a spatula and 100 blueberry pancakes.
Each pancake is assigned a number corresponding to the number of blueberries it contains.
For example: pancake 1, which we denote as [pancake number:1/], has one blueberry, [pancake number:2/] has two blueberries, and so on.
These pancakes are in a stack and ordered randomly.
// [pancakeVis /]
**Flipping rule.** If you repeatedly flip the top [Equation]x[/Equation] pancakes where [Equation]x[/Equation] is the number of blueberries in the top pancake (i.e., pick up the first [Equation]x[/Equation] pancakes, reverse them, and set them down), can you prove the following:
**Given any pancake order, if you repeatedly flip the top pancakes according to our flipping rule above, the pancake with one blueberry will eventually appear on the top of the stack.**
[br/]
*Hint: this will work for other numbers of pancakes besides 100, specifically any integer number greater than 1.*
## Starting with a short stack
A good place to start when proving something is to work through small versions of the problem by hand to gain intuition about what is going on.
Let's say that instead of 100 blueberry pancakes, we have five blueberry pancakes (again in some random order).
What happens if you apply the process described in the problem above?
Click the "Flip the pancakes!" button to flip once.
Keep clicking and follow [pancake number:1/].
[var name:"flip" value:0 /]
[var name:"reset" value:0 /]
[pancakeStack flip:flip reset:reset numOfPancakes:5/]
[span style:`{textAlign: 'center'}`]
You have flipped **[Display value:flip format:"d" /]** times.
[div]
[button onClick:`flip++`]
Flip the pancakes!
[/button]
[button onClick:`reset++;flip=0;`]
New batch
[/button]
[/div]
[/span]
Nice flipping! You may have noticed that [pancake number:1/] will rise to the top pretty quickly and then get stuck there.
If you repeat this a few times (click "New batch" to randomize the stack), we also see that, sometimes, all the pancakes will end up sorted.
This doesn’t happen every time, but it does happen often enough to make one curious.
Something about the flipping process wants to sort the pancakes.
## All possible pancake placements
To dig further into this, we can enumerate the configurations in which the stack ends up sorted (at least for five pancakes!).
For this to happen, the last flip must move [pancake number:1/] to the top, and keep all other pancakes in sorted order—it turns out that there are only four cases where this can happen.
The following tree structure shows the final sorted configuration at the root: **"5,4,3,2,1"**.
Beneath, it shows all the configurations that could lead to that outcome as children.
Here, the *bottom* of the stack of pancakes is the leftmost number in the list, while the *top* of the stack is the rightmost number.
For example: in **"3,2,4,1,5"**, [pancake number:3/] is on the bottom and [pancake number:5/] is on top.
[div className:"superRootNode noClick"]
[ul]
[li]5,4,3,2,1[/li]
[ul]
[li]1,2,3,4,5[/li]
[li]5,1,2,3,4[/li]
[li]5,4,1,2,3[/li]
[li]5,4,3,1,2[/li]
[/ul]
[/ul]
[/div]
Notice that in three out of the four configurations, [pancake number:5/] is at the bottom of the stack already, and in the case that it is not at the bottom, it must be moved to the bottom!
Now, what happens if we expand our previous tree structure a level deeper:
[div className:"superRootNode noClick"]
[ul]
[li]5,4,3,2,1[/li]
[ul]
[li]1,2,3,4,5[/li]
[ul]
[li]1,2,5,4,3[/li]
[/ul]
[li]5,1,2,3,4[/li]
[ul]
[li]4,3,2,1,5[/li]
[/ul]
[li]5,4,1,2,3[/li]
[ul]
[li]3,2,1,4,5[/li]
[li]5,3,2,1,4[/li]
[li]5,4,1,3,2[/li]
[/ul]
[li]5,4,3,1,2[/li]
[ul]
[li]2,1,3,4,5[/li]
[li]5,2,1,3,4[/li]
[li]5,4,2,1,3[/li]
[/ul]
[/ul]
[/ul]
[/div]
If you have pen and paper, try to expand one more level and notice what happens.
* **Not all configurations are reversible.** It is impossible to find a configuration of pancakes that can be flipped resulting in some of the configurations listed in the second level. If you keep expanding these you might notice that the only configurations that you can expand are those that have pancakes in their sorted location. For example, we can expand **"5,4,3,1,2"** 3 times, around [pancake number:5/], [pancake number:4/], and [pancake number:3/] because these are the pancakes that are in their final positions in **"5,4,3,1,2"**.
* **Flipping the largest pancake is a permanent move.** Once you flip the largest number in your stack of pancakes, you cannot change its position through any subsequent flip. In some cases (like the ones enumerated in the trees above), this results in a sorted stack of pancakes; however, in other cases, [pancake number:1/] goes to the top without the remaining pancakes being sorted. [br/][br/] More formally, there is at most one way to move the last pancake in the stack (by flipping the [Equation]N[/Equation]'th pancake), there are at most two flips that can move the second to last pancake in the stack (by flipping the [Equation]N[/Equation]'th and [Equation]N-1[/Equation]'th pancakes), and so on. [br/][br/] *Teaser: this observation is an important one for our upcoming proof!*
[Aside]
**Aside.** Notice that **"5,4,3,2,1"** is not the only final configuration in which the [pancake number:1/] finishes on top!
[br/] [br/]
It is possible to reach many other final configurations where [pancake number:1/] is at the top of the stack; however, these will not be sorted stacks.
For example, consider the boring configurations where [pancake number:1/] is initially on top: no flips are needed, but the remaining pancakes could be in any of [Equation]4![/Equation] remaining different configurations.
[/Aside]
## The complete pancake placement tree
For completeness, and for those without pen and paper, below is the complete tree of all configurations that lead to the sorted end configuration of **"5,4,3,2,1"**!
We've included Python code to reproduce this and encourage further exploration at the [end of the article](#technical-footnote).
Click on configurations to expand them to see the sequence of configurations that lead to having the stack of pancakes in sorted order.
[div className:"superRootNode"]
[ul]
[li] 5,4,3,2,1 [/li]
[ul]
[li className:"rootNode"] 1,2,3,4,5 [/li]
[ul]
[li className:"treeNode"] 1,2,5,4,3 [/li]
[/ul]
[li className:"rootNode"] 5,1,2,3,4 [/li]
[ul]
[li className:"treeNode"] 4,3,2,1,5 [/li]
[/ul]
[li className:"rootNode"] 5,4,1,2,3 [/li]
[ul]
[li className:"treeNode"] 3,2,1,4,5 [/li]
[li className:"treeNode"] 5,3,2,1,4 [/li]
[ul]
[li className:"treeNode"] 4,1,2,3,5 [/li]
[/ul]
[li className:"treeNode"] 5,4,1,3,2 [/li]
[ul]
[li className:"treeNode"] 2,3,1,4,5 [/li]
[li className:"treeNode"] 5,2,3,1,4 [/li]
[ul]
[li className:"treeNode"] 4,1,3,2,5 [/li]
[ul]
[li className:"treeNode"] 4,1,5,2,3 [/li]
[ul]
[li className:"treeNode"] 4,1,5,3,2 [/li]
[/ul]
[li className:"treeNode"] 4,1,3,5,2 [/li]
[ul]
[li className:"treeNode"] 4,1,2,5,3 [/li]
[/ul]
[/ul]
[li className:"treeNode"] 5,2,4,1,3 [/li]
[ul]
[li className:"treeNode"] 3,1,4,2,5 [/li]
[ul]
[li className:"treeNode"] 3,1,4,5,2 [/li]
[/ul]
[/ul]
[/ul]
[/ul]
[/ul]
[li className:"rootNode"] 5,4,3,1,2 [/li]
[ul]
[li className:"treeNode"] 2,1,3,4,5 [/li]
[ul]
[li className:"treeNode"] 2,1,5,4,3 [/li]
[/ul]
[li className:"treeNode"] 5,2,1,3,4 [/li]
[ul]
[li className:"treeNode"] 4,3,1,2,5 [/li]
[ul]
[li className:"treeNode"] 4,3,1,5,2 [/li]
[/ul]
[/ul]
[li className:"treeNode"] 5,4,2,1,3 [/li]
[ul]
[li className:"treeNode"] 3,1,2,4,5 [/li]
[li className:"treeNode"] 5,3,1,2,4 [/li]
[ul]
[li className:"treeNode"] 4,2,1,3,5 [/li]
[li className:"treeNode"] 5,3,1,4,2 [/li]
[ul]
[li className:"treeNode"] 2,4,1,3,5 [/li]
[ul]
[li className:"treeNode"] 2,5,3,1,4 [/li]
[ul]
[li className:"treeNode"] 2,5,4,1,3 [/li]
[/ul]
[/ul]
[/ul]
[/ul]
[/ul]
[/ul]
[/ul]
[/ul]
[/div]
[script src:"static/custom.js"/]
## The pancake proof
Hopefully after working through the example above you are convinced that the original problem statement is likely true.
Using the observations gained from the exercise above, we will now formally prove that [pancake number:1/] will always come out on top!
As hinted in the previous section, the proof relies on the observation that once you flip the largest pancake in the stack you cannot move it again.
**Proof.** Consider a stack of [Equation]N[/Equation] sequentially numbered blueberry pancakes in some random order.
First, observe that if the largest pancake, pancake [Equation]N[/Equation], in the stack is flipped, then it can never be moved again.
For example, [without loss of generality](https://en.wikipedia.org/wiki/Without_loss_of_generality), let [Equation]N=100[/Equation].
Now, if pancake 100 is on top of the stack and is flipped, then it will move to the bottom of the stack.
In order to move this pancake again, there would need to be a pancake with more than 100 blueberries in the stack, but we already said [Equation]N=100[/Equation], so this cannot be done.
The proof continues as follows:
* Define a cycle as a list of greater than two states of the stack such that after a flip is performed in the last configuration you return to the first configuration.
* [For the sake of contradiction](https://en.wikipedia.org/wiki/Proof_by_contradiction), assume that there is a cycle.
* Out of all the first pancakes from each state in the cycle, one of them must be largest.
* Once you flip the largest it cannot return to its initial position based on our reasoning above.
* If the largest pancake cannot return to its initial position, then it is impossible to have a cycle, a contradiction.
* Because there are a finite number of configurations of the stack ([Equation]N![/Equation] many configurations in total), and there are no cycles, each flip produces a new unique configuration; therefore, if we keep flipping then we will eventually arrive at the state with [pancake number:1/] on top, when the process then halts.
## But I'm still hungry!
If you're still craving more, there is an impressive body of technical work dealing with pancakes—flipping, sorting, and even burning pancakes!
Mathematicians get hungry too.
Interested readers should check out the relevant [Wikipedia page](https://en.wikipedia.org/wiki/Pancake_sorting).
## Acknowledgments
* Thanks to Nebojsa Jojic and Kolya Malkin for the interesting problem.
* Thanks Le Hou for the brainstorming session immediately following the introduction of said interesting problem.
* Thanks to [a href:"https://twitter.com/mathisonian"]@mathisonian[/a] for feedback.
## References
* This article was created using [a href:"https://idyll-lang.org/"]Idyll[/a].
* The source code is available on [a href:"https://github.com/fredhohman/blueberry-pancakes"]Github[/a].
[hr/]
## Technical footnote
[a id:"technical-footnote"/]
Below you'll find the Python code that we used to generate the trees above.
```python
def inverse_flip(node, i):
'''Given a node and an index, return the node
that results from having previously flipped node[i]'''
return node[:i] + node[i+1:][::-1] + [node[i]]
def expand(node, indent="", current_depth=0, print_tree=False):
'''Recursive method for printing out all configurations
that lead to a given node. Returns the largest depth reached
by a child node.'''
if print_tree:
print(indent, current_depth,node)
largest_depth = current_depth
for i in range(len(node) - 1):
if i + node[i] == len(node): # we can do a flip
new_node = inverse_flip(node, i)
# recurse and keep track of the deepest child node
t_largest_depth = expand(
new_node,
indent + " ",
current_depth + 1,
print_tree = print_tree
)
largest_depth = max(
largest_depth,
t_largest_depth
)
return largest_depth
```
For example, we can use:
```python
expand([5,4,3,2,1], print_tree=True)
```
to print the tree from earlier in the article.
The numbers before each stack indicate its depth in the tree.
```python
0 [5, 4, 3, 2, 1]
1 [1, 2, 3, 4, 5]
2 [1, 2, 5, 4, 3]
1 [5, 1, 2, 3, 4]
2 [4, 3, 2, 1, 5]
1 [5, 4, 1, 2, 3]
2 [3, 2, 1, 4, 5]
2 [5, 3, 2, 1, 4]
3 [4, 1, 2, 3, 5]
2 [5, 4, 1, 3, 2]
3 [2, 3, 1, 4, 5]
3 [5, 2, 3, 1, 4]
4 [4, 1, 3, 2, 5]
5 [4, 1, 5, 2, 3]
6 [4, 1, 5, 3, 2]
5 [4, 1, 3, 5, 2]
6 [4, 1, 2, 5, 3]
4 [5, 2, 4, 1, 3]
5 [3, 1, 4, 2, 5]
6 [3, 1, 4, 5, 2]
1 [5, 4, 3, 1, 2]
2 [2, 1, 3, 4, 5]
3 [2, 1, 5, 4, 3]
2 [5, 2, 1, 3, 4]
3 [4, 3, 1, 2, 5]
4 [4, 3, 1, 5, 2]
2 [5, 4, 2, 1, 3]
3 [3, 1, 2, 4, 5]
3 [5, 3, 1, 2, 4]
4 [4, 2, 1, 3, 5]
4 [5, 3, 1, 4, 2]
5 [2, 4, 1, 3, 5]
6 [2, 5, 3, 1, 4]
7 [2, 5, 4, 1, 3]
```
From this we see that the longest possible path to **"5,4,3,2,1"** takes 7 flips starting from **"2,5,4,1,3"**.
In fact, this is the longest path considering *any* final configuration using five pancakes, i.e., out of all possible final configurations using five pancakes, the greatest number of flips occurs when the final configuration is sorted.
To conclude, we will leave you with a question: Is it the case for every [Equation]N[/Equation], that the longest path from an initial configuration to a final configuration must be when the final configuration is in a sorted order?
In other words, what is the worst case number of flips that can happen for [Equation]N[/Equation] pancakes?