-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanims.py
More file actions
1768 lines (1479 loc) · 60 KB
/
Copy pathanims.py
File metadata and controls
1768 lines (1479 loc) · 60 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#from os import startfile
from random import randrange
from re import I
from unittest import skip
from manim import config as global_config
import solarized
from util import *
scene_width = 14.2
tree_scale = 3
node_radius = 0.2
arrow_width = 10
def create_arrow_between_nodes(start, end, node_radius, color, width):
return Arrow(
start=start + node_radius * np.subtract(end, start) / np.linalg.norm(np.subtract(end, start)),
end=end - node_radius * np.subtract(end, start) / np.linalg.norm(np.subtract(end, start)),
color=color,
stroke_width=width,
buff=0
)
def sugar(scene, tree, n1, n2, n_sh):
tree.rehang_subtree(
scene,
n1,
n2,
tree.vertices[n2].get_center() + n_sh * sh + H,
2 * DOWN,
2 * DOWN,
)
class Blank(Scene):
def construct(self):
return
class Intro(Scene):
def construct(self):
num_img = 26
img_positions = []
n1 = 10
n11 = 5
n2 = 20
for n, l1, l2 in [
(n1, [-4, -2, 0, 2, 4], [-2, 0, 2]),
(n11, [-4, -2, 2, 4], [-2, 0, 2]),
(n2, [-6, -4, -2], [-2, 0, 2]),
]:
for _ in range(n):
while (True):
new_pos = np.array(random.choice(l1) * RIGHT + random.choice(l2) * UP)
i = max(0, len(img_positions) - 6)
collision = False
for pos in img_positions[i:]:
if np.array_equal(pos, new_pos):
collision = True
if collision == True:
continue
else:
img_positions.append(new_pos)
break
dalle_images = []
for i in range(len(img_positions)):
dalle_images.append(
ImageMobject("img/dalle/p{}.jpg".format((i % num_img) + 1)).scale_to_fit_width(2).move_to(
img_positions[i]
)
)
dalle_title = Tex(r"Images generated by DALL·E 2", color=GRAY).scale(0.8).move_to(3.5 * UP + 3.5 * LEFT)
gpt3_title = Tex(r"The language model GPT-3", color=GRAY).scale(0.8).next_to(dalle_title, RIGHT).shift(
2 * RIGHT)
shft = 7*DOWN
dalle_website = Tex(r"https://openai.com/dall-e-2/", color = GRAY).scale(0.5).move_to(dalle_title.get_center()).shift(shft)
gpt3_website = Tex(r"https://beta.openai.com/playground", color = GRAY).scale(0.5).move_to(gpt3_title.get_center()).shift(shft)
dalle_title.shift(3.5 * RIGHT)
dalle_website.shift(3.5*RIGHT)
anims = []
self.play(FadeIn(dalle_title), FadeIn(dalle_website))
self.wait()
for i in range(len(img_positions)):
anims.append(
Succession(
FadeIn(dalle_images[i]),
Wait(3),
FadeOut(dalle_images[i])
)
)
if i == n1+n11:
anims.append(AnimationGroup(
FadeIn(gpt3_title),
FadeIn(gpt3_website),
dalle_title.animate().shift(3.5 * LEFT),
dalle_website.animate().shift(3.5 * LEFT),
))
self.play(
AnimationGroup(
*anims,
lag_ratio=0.1
)
)
self.wait()
class Polylog(Scene):
def construct(self):
authors = Tex(
r"\textbf{Václav Rozhoň, Vojtěch Rozhoň, Václav Volhejn}",
color=text_color,
font_size=40,
).shift(
3 * DOWN + 0 * LEFT
)
channel_name = Tex(r"polylog", color=GRAY)
channel_name[0][1].set_color(ORANGE)
channel_name.scale(4).shift(1 * UP)
self.wait()
logo_dalle = ImageMobject("img/D2.png").scale(0.5).set_z_index(100)
logo_solarized = ImageMobject("img/logo-solarized.png").scale(0.032).shift(0.3 * UP + 0.05 * LEFT)
prompts_dalle = [
Tex(
str,
color=GRAY
).scale(0.8).next_to(logo_dalle, DOWN)
for str in [r"DALL$\cdot$E 2 on the prompt:", r"A logo suitable for a youtube channel about computer science"]
]
Group(*prompts_dalle).arrange(DOWN).next_to(logo_dalle, DOWN)
asterisk = Tex(
r"*As of 2022, neural nets generating artificial images still struggle with texts inside the images. Probably going to be solved soon. ",
color=GRAY
).scale(0.2).next_to(Group(*prompts_dalle), DOWN).shift(0.4 * DOWN)
self.play(
FadeIn(logo_dalle),
FadeIn(prompts_dalle[0]),
FadeIn(prompts_dalle[1]),
FadeIn(asterisk)
)
self.wait()
self.add(logo_solarized)
self.play(
FadeOut(logo_dalle),
FadeOut(prompts_dalle[0]),
FadeOut(prompts_dalle[1]),
FadeOut(asterisk),
)
self.play(
logo_solarized.animate().move_to(2 * LEFT + 1 * UP + 0.55 * RIGHT),
Write(authors),
FadeIn(channel_name[0][0]),
FadeIn(channel_name[0][2:])
)
self.wait()
return
self.play(
*[FadeOut(o) for o in self.mobjects]
)
self.wait()
class Statement(Scene):
def construct(self):
self.next_section(skip_animations=False)
# caption = Tex("Problem", color=GRAY).scale(3)
# self.add_sound("audio/gong.wav")
# self.play(
# FadeIn(caption)
# )
# self.wait()
# self.play(
# FadeOut(caption)
# )
# The problem I’ll be solving is called Buds Re-hanging. In this problem, we are given a tree, so a bunch of nodes connected by edges such that no edges form a cycle.
statement = ImageMobject(
"img/cf-statement.png",
).scale_to_fit_width(scene_width / 2.0).shift(scene_width / 4 * LEFT + 5.8 * DOWN)
statement_caption = Tex("Buds Re-hanging (Codeforces 1566E)", color=GRAY).scale(0.8).move_to(
statement.get_center()).next_to(statement, UP)
self.play(
FadeIn(statement),
FadeIn(statement_caption)
)
self.wait()
self.play(Circumscribe(statement_caption, color=RED))
self.wait()
example_tree = Tree(
example_vertices,
example_edges,
layout="kamada_kawai",
layout_scale=tree_scale,
vertex_config={"radius": node_radius, "color": GRAY}, # for debugging
labels=False, # for debugging
edge_config={"color": text_color},
root=1
).move_to(scene_width / 4 * RIGHT)
self.play(
DrawBorderThenFill(example_tree)
)
self.wait()
# This tree is also rooted, so each node except the root has one parent node and possibly some children. The nodes that have no children are called leaves – let’s highlight them in green.
highlight_box = Rectangle(
width=scene_width / 2 - 0.0,
height=0.8,
color=RED,
).next_to(statement_caption, DOWN).shift(1.7 * DOWN).set_z_index(100)
self.play(
FadeIn(highlight_box),
)
self.play(
example_tree.animate().change_layout(rooted_position(pos_root=scene_width / 4 * RIGHT + 3 * UP)),
run_time=1
)
self.wait()
example_vertex = example_tree.vertices[2]
self.play(
example_vertex.animate().set_color(RED)
)
self.wait()
# parent
example_parent = example_tree.vertices[1]
edge_parent = create_arrow_between_nodes(
example_vertex.get_center(),
example_parent.get_center(),
node_radius,
RED,
arrow_width
)
self.play(
Create(edge_parent),
)
self.play(
Flash(example_parent, color=RED)
)
self.play(
Uncreate(edge_parent)
)
self.wait()
# children
example_children = [
example_tree.vertices[3],
example_tree.vertices[4],
example_tree.vertices[5],
]
edge_children = [
create_arrow_between_nodes(
example_vertex.get_center(),
child.get_center(),
node_radius,
RED,
arrow_width
) for child in example_children
]
self.play(
*[Create(e) for e in edge_children],
)
self.play(
*[Flash(child, color=RED) for child in example_children]
)
self.play(
*[Uncreate(e) for e in edge_children],
example_vertex.animate().set_color(GRAY)
)
self.wait()
# leaves
self.play(
*[
example_tree.vertices[v].animate().set_color(BLUE)
for v in example_tree.get_leaves()
],
*[
Flash(example_tree.vertices[v], color=BLUE)
for v in example_tree.get_leaves()
],
)
self.wait()
# So far, these are standard terms. In this problem specifically, we also need the concept of a bud. A bud is a node that’s not a root, has at least one child, and all its children are leaves. In other words, buds are basically the nodes that have only leaves as children but are not leaves themselves. I highlighted all of these in blue.
highlight_box.generate_target()
highlight_box.target = Rectangle(
width=highlight_box.get_width(),
height=1.5,
color=RED,
).next_to(highlight_box, DOWN, buff=DEFAULT_MOBJECT_TO_MOBJECT_BUFFER / 2.0).shift(0.1*UP)
self.play(
MoveToTarget(highlight_box),
)
self.wait()
arrow = Arrow(
start=ORIGIN + 0 * RIGHT,
end=ORIGIN + LEFT,
color=RED,
stroke_width=10
).scale(3).move_to(3 * LEFT + 0.33 * DOWN)
self.play(
Create(arrow)
)
self.wait()
line_height = 0.25
for _ in range(2):
self.play(
arrow.animate().shift(line_height * DOWN)
)
self.wait()
self.play(
*[
example_tree.vertices[v].animate().set_color(RED)
for v in example_tree.get_buds()
],
*[
Flash(example_tree.vertices[v], color=RED)
for v in example_tree.get_buds()
],
)
self.wait()
self.play(
Uncreate(arrow)
)
self.wait()
# Now, we’re allowed to manipulate our tree it in the following way: we can take a bud and re-hang it and its children to another node of the tree.
self.next_section(skip_animations=False)
highlight_box.generate_target()
highlight_box.target = Rectangle(
width=highlight_box.get_width(),
height=1.3,
color=RED,
).next_to(highlight_box, DOWN, buff=DEFAULT_MOBJECT_TO_MOBJECT_BUFFER / 2.0).shift(0.05*UP)
self.play(
MoveToTarget(highlight_box),
)
self.wait()
H = 1 * DOWN
sugar(self, example_tree, 5, 13, 0)
sugar(self, example_tree, 5, 2, 1)
self.wait()
self.wait(2)
# Notice that in this case, after we cut this bud off the tree, this guy becomes a new bud, and after we put the bud back here, this guy stops being a leaf and also this is not a bud anymore.
ar = Arrow(
start=ORIGIN,
end=(1 * RIGHT + 1 * DOWN) / 1.0,
color=RED,
).move_to(
example_tree.vertices[2].get_center() + (1 * LEFT + 1 * UP) / 2.0
)
ar2 = Arrow(
start=ORIGIN,
end=(1 * LEFT + 1 * DOWN) / 1.0,
color=RED,
).move_to(example_tree.vertices[12].get_center() + (1 * RIGHT + 1 * UP) / 2.0)
ar3 = ar2.copy().shift(H)
self.play(
AnimationGroup(
FadeIn(ar),
run_time = 0.2
),
)
anims = [
Succession(
Wait(0.3),
AnimationGroup(
FadeIn(ar2),
FadeIn(ar3),
run_time = 0.2
)
)
]
example_tree.rehang_subtree(
self,
5,
13,
example_tree.vertices[13].get_center() + 0 * sh + H,
2 * DOWN,
2 * DOWN,
additional_anims = anims
)
self.play(
FadeOut(ar),
FadeOut(ar2),
FadeOut(ar3),
)
self.wait()
# And now the question is: You’re allowed to do these operations any number of times with any buds you choose. If you do the operations as cleverly as possible, what’s the lowest number of leaves the tree can have? For example, the number of leaves at the beginning is 7. You can see how it changes when we do the operations and the lowest we can get seems to be 5.
self.next_section(skip_animations=False)
highlight_box.generate_target()
highlight_box.target = Rectangle(
width=highlight_box.get_width(),
height=0.55,
color=RED,
).next_to(highlight_box, DOWN, buff=DEFAULT_MOBJECT_TO_MOBJECT_BUFFER / 2.0).shift(0.07*UP)
self.play(
MoveToTarget(highlight_box),
)
self.wait()
num_leaves = Tex(r"\# leaves: ", color=BLUE).move_to(1.5 * RIGHT + 3 * DOWN)
num_leaves_counter = Integer(7, color=BLUE).next_to(num_leaves, RIGHT)
num_leaves_counter.add_updater(lambda x: x.set_value(Forest.get_leaves_cnt()))
self.play(
FadeIn(num_leaves),
FadeIn(num_leaves_counter)
)
self.wait()
sugar(self, example_tree, 2, 10, 0)
sugar(self, example_tree, 5, 1, -2)
sugar(self, example_tree, 2, 13, 0)
sugar(self, example_tree, 5, 10, 0)
sugar(self, example_tree, 2, 1, -2)
sugar(self, example_tree, 5, 2, 1)
sugar(self, example_tree, 5, 1, -5)
sugar(self, example_tree, 12, 1, 5)
sugar(self, example_tree, 12, 10, 0)
sugar(self, example_tree, 5, 3, 0)
sugar(self, example_tree, 5, 13, 0)
sugar(self, example_tree, 2, 11, 0)
sugar(self, example_tree, 5, 3, 0)
# Because this is a coding problem, it is also important how large the input data is. You can see that the tree can have around 10^5 nodes, which means that our algorithm for computing the answer needs to have close to linear time complexity.
self.play(
Group(
statement,
statement_caption,
highlight_box,
).animate().shift(6 * UP)
)
self.wait()
highlight_box.generate_target()
highlight_box.target = Rectangle(
width=highlight_box.get_width(),
height=0.6,
color=RED,
).next_to(highlight_box, DOWN, buff=DEFAULT_MOBJECT_TO_MOBJECT_BUFFER / 2.0).shift(0.85 * DOWN)
self.play(
MoveToTarget(highlight_box),
)
self.wait()
# There are examples in the problem statement to make this clearer. Let’s look at the first one: the tree looks like this. It has four leaves. But if we rehang this bud here and this bud there, we get a tree with two leaves, which is the correct answer.
highlight_box.generate_target()
highlight_box.target = Rectangle(
width=highlight_box.get_width(),
height=1.32,
color=RED,
).next_to(highlight_box, DOWN, buff=DEFAULT_MOBJECT_TO_MOBJECT_BUFFER / 2.0).shift(3.2 * DOWN)
self.play(
MoveToTarget(highlight_box),
)
self.wait()
self.play(
Uncreate(example_tree),
)
sample_tree = Tree(
sample_vertices,
sample_edges,
layout="kamada_kawai",
layout_scale=tree_scale,
vertex_config={"radius": node_radius, "color": GRAY}, # for debugging
labels=False, # for debugging
edge_config={"color": text_color},
root=1
) # .move_to(scene_width/4 * RIGHT)
W = 2 * sh
sample_tree.change_layout(
{
1: ORIGIN,
2: H - W,
3: H,
4: H + W,
5: 2 * H - 3 * W / 2,
6: 2 * H - W / 2,
7: 2 * H + W,
}
).move_to(scene_width / 4 * RIGHT + 3 * UP)
self.play(
DrawBorderThenFill(sample_tree)
)
sample_tree.pretty_colour()
self.wait()
sugar(self, sample_tree, 4, 3, 0)
sugar(self, sample_tree, 2, 7, 0)
self.play(
Group(
statement,
statement_caption,
highlight_box,
).animate().shift(5 * UP)
)
highlight_box2 = Rectangle(
width=highlight_box.get_width(),
height=0.22,
color=RED,
).move_to(highlight_box.get_center()).shift(5.18 * DOWN)
self.play(
Create(highlight_box2),
)
self.wait()
sugar(self, sample_tree, 2, 4, 2)
sugar(self, sample_tree, 2, 3, -2)
sugar(self, sample_tree, 4, 1, 1)
sugar(self, sample_tree, 2, 1, -1)
sugar(self, sample_tree, 2, 3, 0)
sugar(self, sample_tree, 4, 5, 0)
sugar(self, sample_tree, 4, 3, -2)
self.wait()
class Statement2(Scene):
def construct(self):
example_tree = Tree(
example_vertices,
example_edges,
layout=rooted_position(),
layout_scale=tree_scale,
vertex_config={"radius": node_radius, "color": GRAY}, # for debugging
labels=False, # for debugging
edge_config={"color": text_color},
root=1
).move_to(scene_width/4 * RIGHT + 1*UP)
self.play(
DrawBorderThenFill(example_tree)
)
example_tree.pretty_colour()
num_leaves = Tex(r"\# leaves: ", color=BLUE).move_to(1.5 * RIGHT + 3 * DOWN)
num_leaves_counter = Integer(7, color=BLUE).next_to(num_leaves, RIGHT)
num_leaves_counter.add_updater(lambda x: x.set_value(Forest.get_leaves_cnt()))
self.play(FadeIn(num_leaves), FadeIn(num_leaves_counter))
self.wait()
sugar(self, example_tree, 5, 13, 0)
sugar(self, example_tree, 2, 10, 0)
sugar(self, example_tree, 5, 1, -2)
sugar(self, example_tree, 2, 13, 0)
sugar(self, example_tree, 5, 10, 0)
sugar(self, example_tree, 2, 1, -2)
sugar(self, example_tree, 5, 2, 1)
self.wait()
class Solution(Scene):
def construct(self):
self.next_section(skip_animations=False)
# caption = Tex("Solution", color=GRAY).scale(3)
# self.add_sound("audio/gong.wav")
# self.play(
# FadeIn(caption)
# )
# self.wait()
# self.play(
# FadeOut(caption)
# )
num_leaves = Tex(r"\# leaves: ", color=BLUE).move_to(5.5 * LEFT + 2 * UP)
num_leaves_counter = Integer(7, color=BLUE).next_to(num_leaves, RIGHT)
example_tree = Tree(
example_vertices,
example_edges,
layout=rooted_position(),
layout_scale=tree_scale,
vertex_config={"radius": node_radius, "color": GRAY}, # for debugging
labels=False, # for debugging
edge_config={"color": text_color},
root=1
).shift(3 * UP)
self.play(
DrawBorderThenFill(example_tree)
)
num_leaves_counter.add_updater(lambda x: x.set_value(Forest.get_leaves_cnt()))
# self.play(
# FadeIn(example_tree),
# FadeIn(num_leaves),
# FadeIn(num_leaves_counter),
# )
# self.wait(2)
# In competitive programming problems like these, it’s often useful to first play around with the problem and get a feeling for what the correct direction might be. So, I first drew an example tree that looked roughly like the example we already saw [obrázek stromu]
# Then I spent a lot of time just playing with the tree simply to understand what the rehanging operations are doing.
sugar(self, example_tree, 5, 10, -1)
sugar(self, example_tree, 2, 6, 0)
sugar(self, example_tree, 2, 1, -2)
sugar(self, example_tree, 5, 1, -5)
sugar(self, example_tree, 12, 1, 5)
sugar(self, example_tree, 12, 10, 0)
sugar(self, example_tree, 5, 3, 0)
sugar(self, example_tree, 5, 13, 0)
sugar(self, example_tree, 2, 11, 0)
sugar(self, example_tree, 5, 1, -2)
# And after some time I realized the following thing. Let’s look for example at this bud and enclose it and its leaves in this pointy circle. And then do some random operations. You can see that the bud and its leaves always stay together, they never separate.
rel_pos_5 = 0.2
circle5 = CubicBezier(
ORIGIN,
1.4 * (2 * LEFT + 2 * DOWN),
1.4 * (2 * RIGHT + 2 * DOWN),
ORIGIN,
color=BLACK
)
self.play(
Flash(example_tree.vertices[5], color = RED)
)
self.wait()
example_tree.add_object_to_vertex(5, self, circle5, rel_pos_5)
sugar(self, example_tree, 12, 6, 0)
sugar(self, example_tree, 2, 8, 0)
sugar(self, example_tree, 12, 1, -5)
sugar(self, example_tree, 2, 10, 0)
sugar(self, example_tree, 5, 11, 0)
sugar(self, example_tree, 12, 5, 2)
# return back
sugar(self, example_tree, 2, 1, -2)
sugar(self, example_tree, 12, 10, 0)
sugar(self, example_tree, 5, 2, 1)
sugar(self, example_tree, 12, 11, 0)
example_tree.remove_object(5, self)
self.wait()
# And that holds in general. I realized that I can repeatedly cut the buds from the tree in any order and rehang them below the root, like this. Now I draw my pointy circle around each bud and start doing some random bud-cutting operations. You can see how the nodes in the same circle always stay together.
sugar(self, example_tree, 5, 1, -5)
sugar(self, example_tree, 12, 1, 5)
circle_shifts = [
0.4, 0.4, 0.4, 0.3
]
circles = [
CubicBezier(
ORIGIN,
1.4 * (2 * LEFT + 2 * DOWN),
1.4 * (2 * RIGHT + 2 * DOWN),
ORIGIN,
color=BLACK
),
CubicBezier(
ORIGIN,
1.2 * (2 * LEFT + 2 * DOWN),
1.4 * (1.1 * RIGHT + 2 * DOWN),
ORIGIN,
color=BLACK
),
CubicBezier(
ORIGIN,
1.4 * (2 * LEFT + 2 * DOWN),
1.4 * (2 * RIGHT + 2 * DOWN),
ORIGIN,
color=BLACK
),
CubicBezier(
ORIGIN,
1.3 * (1.5 * LEFT + 2 * DOWN),
1.3 * (1.5 * RIGHT + 2 * DOWN),
ORIGIN,
color=BLACK
),
]
example_tree.add_object_to_vertex(5, self, circles[0], circle_shifts[0])
example_tree.add_object_to_vertex(2, self, circles[1], circle_shifts[1])
example_tree.add_object_to_vertex(9, self, circles[2], circle_shifts[2])
example_tree.add_object_to_vertex(12, self, circles[3], circle_shifts[3])
sugar(self, example_tree, 5, 10, -1)
sugar(self, example_tree, 2, 6, 0)
sugar(self, example_tree, 12, 11, 0)
sugar(self, example_tree, 2, 13, 0)
sugar(self, example_tree, 5, 1, -5)
sugar(self, example_tree, 2, 1, -2)
sugar(self, example_tree, 12, 3, 0)
sugar(self, example_tree, 12, 1, 5)
# sugar(self, example_tree, 5, 3, 0)
# sugar(self, example_tree, 5, 4, 0)
# sugar(self, example_tree, 5, 13, 0)
# sugar(self, example_tree, 5, 1, -5)
return
example_tree.remove_object(5, self)
example_tree.remove_object(2, self)
example_tree.remove_object(9, self)
example_tree.remove_object(12, self)
self.next_section(skip_animations=False)
# Ok, so all the rehangings that we do are just shuffling the four buds in some ways.
class Solution2(Scene):
def construct(self):
starting_tree = Tree(
example_vertices,
example_edges,
layout=rooted_position(),
layout_scale=tree_scale,
vertex_config={"radius": node_radius, "color": GRAY}, # for debugging
labels=False, # for debugging
edge_config={"color": text_color},
root=1
).shift(2 * UP + 4 * LEFT)
self.play(
DrawBorderThenFill(starting_tree)
)
starting_tree.pretty_colour()
# When I understood this, I asked the following question. Let’s say that I take the four buds and stack them below the root in some other configuration, for example like this, to get a new tree.
example_tree = starting_tree.copy()
self.play(example_tree.animate().shift(8*RIGHT))
self.wait()
buds = [
example_tree.remove_subtree(self, v)
for v in [5, 2, 12, 9]
]
self.play(
buds[0].animate().shift(-0 * sh + 1 * H),
buds[1].animate().shift(2*H - sh),
buds[2].animate().shift(1 * sh + 0*H),
buds[3].animate().shift(0 * sh + 2 * H),
)
self.wait()
self.play(
buds[0].animate().shift(-2 * sh),
buds[1].animate().shift(-2*H + sh),
buds[2].animate().shift(-1 * sh + 0*H),
buds[3].animate().shift(-2 * sh - 1 * H),
)
self.wait()
example_tree.add_subtree(self, buds[1], 1)
example_tree.add_subtree(self, buds[0], 3)
example_tree.add_subtree(self, buds[3], 2)
example_tree.add_subtree(self, buds[2], 9)
self.wait()
# The question is: Is there always a way to start with the original tree and use my rehanging operations to end up with the new tree? [pauza] Well, it turns out the answer is yes!
a = Arrow(
start = starting_tree.vertices[11].get_center(),
end = example_tree.vertices[3].get_center(),
color = RED,
buff = 1,
)
a_text = Tex("Rehangings", color = GRAY).next_to(a, UP)
self.play(FadeIn(a), FadeIn(a_text))
self.wait()
small_tree_scale = 0.3
self.play(
example_tree.animate().shift(2 * RIGHT ).scale(small_tree_scale),
starting_tree.animate().shift(2 * LEFT ).scale(small_tree_scale),
FadeOut(a),
FadeOut(a_text),
)
self.wait()
self.play(
Circumscribe(starting_tree, color = RED)
)
self.wait()
self.play(
Circumscribe(example_tree, color = RED)
)
self.wait()
middle_tree = Tree(
example_vertices,
example_edges_mid,
layout=rooted_position_mid(),
layout_scale=tree_scale,
vertex_config={"radius": node_radius, "color": GRAY}, # for debugging
labels=False, # for debugging
edge_config={"color": text_color},
root=1,
).scale(small_tree_scale)
middle_tree.shift(
(example_tree.vertices[1].get_center() * 2.0 / 5 + starting_tree.vertices[1].get_center() * 3.0 / 5.0)
- middle_tree.vertices[1].get_center()
)
# Why is that? Well, one fancy way of seeing it is to use my special tree where everything hangs below the root as an intermediary.
self.play(DrawBorderThenFill(middle_tree))
middle_tree.pretty_colour() # TODO
self.wait()
# I can convert my starting tree into this form,
t1 = starting_tree.copy()
#self.play(
# t1.animate().shift(
# (middle_tree.vertices[1].get_center() - starting_tree.vertices[1].get_center()) / 2.0
# )
#)
arrow_shift = 2 * DOWN
ar_buffer = 0.5
shft_tree1 = shift_horizontal=(middle_tree.vertices[1].get_center() - starting_tree.vertices[1].get_center()) / 2.0
a1 = Arrow(
starting_tree.vertices[1].get_center() + arrow_shift,
starting_tree.vertices[1].get_center() + shft_tree1 + arrow_shift,
color=RED,
buff=ar_buffer
)
t1.rehang_subtree(self, 5, 1, t1.vertices[1].get_center() + (-5 * sh + H) * small_tree_scale,
2 * DOWN * small_tree_scale, 2 * DOWN * small_tree_scale, shft_tree1
, additional_anims = [Create(a1)])
t2 = t1.copy()
# self.play(
# t2.animate().shift(
# (middle_tree.vertices[1].get_center() - starting_tree.vertices[1].get_center()) / 2.0
# )
# )
a2 = Arrow(
t1.vertices[1].get_center() + arrow_shift,
t1.vertices[1].get_center() + shft_tree1 + arrow_shift,
color=RED,
buff=ar_buffer
)
t2.rehang_subtree(self, 12, 1, t2.vertices[1].get_center() + (+5 * sh + H) * small_tree_scale,
2 * DOWN * small_tree_scale, 2 * DOWN * small_tree_scale,
shift_horizontal=shft_tree1, additional_anims = [Create(a2)]
)
self.wait()
# but the same holds also for the final tree.
t3 = example_tree.copy()
# self.play(
# t3.animate().shift(
# -(example_tree.vertices[1].get_center() - middle_tree.vertices[1].get_center()) / 3.0
# )
# )¨
shft_tree2 = -(example_tree.vertices[1].get_center() - middle_tree.vertices[1].get_center()) / 3.0
a3 = Arrow(
example_tree.vertices[1].get_center() + arrow_shift,
example_tree.vertices[1].get_center() + shft_tree2 + arrow_shift,
color=BLUE,
buff=ar_buffer
)
t3.rehang_subtree(self, 5, 1, t3.vertices[1].get_center() + (-5 * sh + H) * small_tree_scale,
2 * DOWN * small_tree_scale, 2 * DOWN * small_tree_scale, shft_tree2
, additional_anims = [Create(a3)]
)
t4 = t3.copy()
# self.play(
# t4.animate().shift(
# -(example_tree.vertices[1].get_center() - middle_tree.vertices[1].get_center()) / 3.0
# )
# )
a4 = Arrow(
t3.vertices[1].get_center() + arrow_shift,
t3.vertices[1].get_center() + shft_tree2 + arrow_shift,
color=BLUE,
buff=ar_buffer
)
t4.rehang_subtree(self, 12, 1, t4.vertices[1].get_center() + (+5 * sh + H) * small_tree_scale,
2 * DOWN * small_tree_scale, 2 * DOWN * small_tree_scale, shft_tree2, additional_anims = [Create(a4)]
)
t5 = t4.copy()
# self.play(
# t5.animate().shift(
# -(example_tree.vertices[1].get_center() - middle_tree.vertices[1].get_center()) / 3.0
# )
# )
a5 = Arrow(
t4.vertices[1].get_center() + arrow_shift,
t4.vertices[1].get_center() + shft_tree2 + arrow_shift,
color=BLUE,
buff=ar_buffer
)
t5.rehang_subtree(self, 9, 1, t5.vertices[1].get_center() + (+2 * sh + H) * small_tree_scale,
2 * DOWN * small_tree_scale, 2 * DOWN * small_tree_scale, shft_tree2, additional_anims = [Create(a5)]
)
self.wait()
# If I now revert the operations that convert the final tree into the special form, I get a way of converting my starting tree to the final one.
# So, we proved that the rehanging operations can create any possible configuration of the buds.
tt = t5.copy()
tt.rehang_subtree(self, 9, 2, tt.vertices[2].get_center() + (+2 * sh + H) * small_tree_scale,
2 * DOWN * small_tree_scale, 2 * DOWN * small_tree_scale, -shft_tree2, additional_anims = [Transform(a5, Arrow(a5.get_left(), a5.get_right(), buff=0, color=RED)),]
)
self.remove(tt)
tt = t4.copy()
tt.rehang_subtree(self, 12, 9, tt.vertices[9].get_center() + (+3 * sh + H) * small_tree_scale,
2 * DOWN * small_tree_scale, 2 * DOWN * small_tree_scale, -shft_tree2, additional_anims = [Transform(a4, Arrow(a4.get_left(), a4.get_right(), buff=0, color=RED)),]
)
self.remove(tt)
tt = t3.copy()
tt.rehang_subtree(self, 5, 3, tt.vertices[3].get_center() + (+0 * sh + H) * small_tree_scale,
2 * DOWN * small_tree_scale, 2 * DOWN * small_tree_scale, -shft_tree2, additional_anims = [Transform(a3, Arrow(a3.get_left(), a3.get_right(), buff=0, color=RED)),]
)
self.remove(tt)
self.remove(middle_tree)
self.wait()
self.play(
Circumscribe(starting_tree, color=RED)
)
self.wait()
self.play(
Circumscribe(example_tree, color=RED)
)
self.wait()
# rubik part