Visualizing Recursion
February 20, 2013
It seems that when the concept of recursion is introduced, a lot of people (including myself) have a hard time to understand it. Although, it is a powerful technique that is used in important algorithms. In addition, recursion often leads to simple and straightforward solutions.
I think one way of making things more clear is to visualize the process that is generated by a recursive algorithm.
Fibonacci Numbers
The classical example for introducing recursion is Fibonacci numbers. Consider this implementation in Python:
What is interesting about this algorithm is that it generates a process with a tree structure. This is illustrated in the following.
Just by looking at the source code it is hard to reason about its generated process. In contrast, I think the illustration helps.
The picture also shows why this naive implementation is quite slow. A large number of function calls are computed multiple times.
Merge Sort
Another classical recursive algorithm is Merge sort. It is a divide and conquer algorithm that recursively breaks the input down into smaller problems and merges it back in sorted order. The algorithm is implemented like this (full implementation is available here):
The generated process of sorting 9, 1, 4, 6, 3, 2, 7, 8 is shown below.
Constructing All Permutations
The problem of constructing all permutations of a string can be solved recursively. For instance, this implementation:
produces the following process for “abc”.
Moreover, the resulting permutations of “abc” reside at the leaves of the process tree: “abc”, “acb”, “bac”, “bca”, “cab” and “cba”.
Conclusion
Of course, there are different strategies that help with understanding recursive processes. I think visualizing the process of a recursive algorithm is very helpful. Especially in the beginning.