Visualizing Algorithms with Manim
Published:
What’s Manim?
Manim is a Python graphics library for creating mathematics animations. Originally created by Grant Sanderson, host of the excellent 3blue1brown youtube channel, it now exists in two flavours: the community edition and the original project. Although they both have their strengths, I chose to work with the community edition as it has a reputation for being better documented and being more beginner friendly.
Manim is useful for quickly creating impressive animations and visualizations of mathematical concepts. It’s a great tool to liven up presentations and I find that its strength lies in giving examples to illustrate new concepts. Through playing around with manim, I’ve discovered it’s surprisingly easy to illustrate any “action” through animation. So easy, in fact, that it can be hard not to resist putting these animations everywhere.
Moving slightly away from its applications to math visualization, I’ve found that manim is also very good at presenting algorithms. Here are a few reasons why.
Managing Focus
One of the frustrations of learning a new algorithm is having to keep all of the elements in mind while imagining their actions, even when said elements are inactive. Because we haven’t understood the algorithm yet, we are forced to retain information that might not be relevant to the current state of the algorithm. Take keeping track of the elements of a set, or remembering what state we last updated, for example.
The simple act of making elements fade in or out during an animation guides the viewer. For example, when an element fades out, the viewer intuitively knows that it becomes irrelevant for the next steps. Furthermore, all of the relevant information can be kept on the screen, this leads to more focus being put on how the elements interact rather than what the values of these elements are.
Ease of Creation
Animating an algorithms usually follows directly from an implementation of said algorithm. Because the animations are created using code, a straightforward way to represent an algorithm is to give every action the algorithm makes a corresponding animation. For example, it took less than 20 lines of code to create a shuffling method for a deck of cards object which returns an animation which looks like this:
It is important to note that Manim is designed with object oriented programming in mind. I’ve personally found this to fit the python language and the animation use case quite well.
Bringing Intuition
The final point I would like to make is that these animations are most useful as a tool for demonstration. An animation by itself will never be sufficient to give a proper understanding of an algorithm. So far I have found two ways of integrating these animations into a presentation.
- Using the animation as an example after having shown some pseudocode and given an explanation. Here the animation plays the role of tying everything together and helping the viewer confirm their understanding of what the algorithm is doing.
- Using the animation as an introduction to the algorithm. This method can be used to give an intuitive idea of what an algorithm does before bringing up its implementation. In this case, the spoken part of the presentation plays a crucial role in providing enough context for the animation to make sense.
In order to bring explanations while the animation is running, we may want to be able to control the pace of the animation. This is made possible by Manim Slides which allows us to step through an animation step-by-step.
Examples
A Simple Graph Algorithm
Here is an extract of the first animation I made and used in a presentation. This graph algorithm is quite simple and I was able to explain it thanks to the animation, without having to show any pseudocode.
Although this specific section of the animation could have just been slides, it does illustrate something imortant. Notice that the use of colour allows us to animate on the graph directly, and that this bypasses having sets of visited states explicitly written anywhere. Partly because of the graph’s presence, writting the sets down doesn’t even cross our minds as something reasonable to do. What I’m trying to highlight here is that using highly visual tools naturally incentivizes us to present in a highly visual way.
A Sliding Window Algorithm
My second attempt at an animation involved a slightly more complicated, “sliding-window” algorithm. First notice that there’s a lot more going on. If I had to redo this, I would get rid of the “L/R” in the top left, which is meant to indicate whether we are looking at the left or right side of an interval, in favour of emphasizing the tip of the interval throught a color change and perhaps a size increase. This animation is also entirely programmatic, meaning that nothing is hard-coded except for the positioning of the intervals. One benefit of this is that a potential demonstrations of edge-cases comes for free.
I will refrain from giving an explanation of what this algorithm is actually doing to give the reader an idea of what amount of information the animation brings alone. This should also give you an idea of how much context is necessary in order to justify an animation like this one.
A Cryptographic Protocol
This animation represents a cryptographic protocol for a two-player game of “mental poker”. The protocol makes use of a commutative encryption scheme, meaning that if an object is encoded twice with two encryption keys, it does not matter in which order we apply our decryption keys. In order to understand the animation, it should be sufficient to know that objects passing through the red line represent information being shared by one party to the other and that the $e_i$ are encryption keys while the $d_i$ are the corresponding decryption keys.
The audience was highly receptive to the animation, even though we had to explain what an encryption key was only a few minutes prior.