![]() |
![]() Animating Recursive Algorithms Linda Stern, The University of Melbourne Lee Naish, The University of Melbourne Abstract 1. Introduction Although several algorithm animations are available, especially for the simpler algorithms [4], few satisfied our need for pedagogically oriented animation. Notable exceptions are Gloor's CD companion [7] to a text on algorithms [5] and the collaborative active textbooks of Marc Brown [2, 3], neither of which fit well with our syllabus and choice of textbook [11]. Our aim in developing AIA was to find a general approach to presenting and animating algorithms and to implement a framework that could be used as the basis for presenting specific algorithms. The expectation was that interactive visual presentation would help students develop both an overall conceptual idea of how each algorithm works and a more detailed procedural understanding. 2. The Algorithms
in Action Framework |
![]()
|
![]() |
![]() ![]() |
![]() Figure 1. Quicksort animation, showing recursive calls. The current recursive call is sorting the elements 25, 35, 30, with 30 as the pivot (shown by the box). Elements up to 20 have already been sorted, while elements from 40 onwards are waiting for a call on the stack to be done. |
![]() |
![]() ![]() |
![]() Figure 2. Binary search, showing 3 recursive calls. The horizontal lines under the data structure show subarrays for successive recursive calls. |
![]()
|
![]() Figure 3. Recursive binary search tree, showing three different ways to display the part of the data involved in the current recursive call: (a) The leaves, or outer extent, of the current tree are underlined; (b) Polygons are used to outline the subtree under investigation, with larger triangles showing the history of previous recursive calls; (c) A pointer to the root of the subtree corresponding to the current recursive call is shown (solid line arrow); optionally, a history of recursive calls can be kept (dotted line arrows). |
![]() |
8. Navigation Using
Non-Tail Recursive Algorithms When the code is not tail recursive, there is work to be done upon returning from the recursive call. The location of the recursive call within the code is important because it determines what work is to be done upon return from the call. The pedagogical challenge here is to help students keep track of the point in the calling function where the recursive call is made, as well as how the navigation is progressing through the data structure. For example, when navigating through a splay tree [11] "rotations" are performed after the return from the recursive call. Rotations are local operations on a small portion of the tree, intended to improve the overall balance of the tree. Because the place of the rotations within the algorithm may not be immediately obvious to a student just learning the algorithm, we supplement the main animation display with an abbreviated function stack (Figure 4). The aim of showing the function stack is to track the progress through the calling function, and to show the work still to be done after return from the recursive call. We are, in effect, combining two types of animation: animating the result of the program, in the tree, and simultaneously animating the state of the program in the function stack [8], to help students make the mental link between the two processes. |
![]() |
![]() |
![]()
|
![]() Figure 4. Splay tree search showing stack. In (a) the stack contains three function calls. The third (top) call has been called from the first line in the second call (highlighted). In (b), the third call has returned, and the second call is performing the rotation specified in its second line (now highlighted). |
|
![]() Figure 5. Insertion into radix trie. New nodes are formed as recursive calls are being made, but are not linked to the rest of the trie. The links will be made as the recursive calls return. |
![]() |
![]() For more complicated algorithms, we supplement the main part of the animation with a thumbnail sketch that abstracts the data structure and indicates the part of the data structure referred to in the recursive call. For example, the main animation in our representation of a Patricia trie [11] shows all the details necessary to follow the workings of the algorithms, such as keys and upward links. Thumbnail sketches capture the essence, showing only nodes and links in the subtree currently being searched, without detail. The region to be searched by future recursive call(s) is represented by an large blank space (Figure 6). Thumbnail sketches are used in both the growth and shrinking phases of the algorithm, and are visually linked to the main animation. Over the course of the algorithm the thumbnail sketches allow us to build up a history of recursive calls, which helps students keep track of the overall progress of the recursive navigation. |
![]() |
![]() |
![]() ![]() (a)
|
![]() Figure 6. Insertion into Patricia tree, with thumbnail sketches as navigation aids. |
![]() |
10. Evaluation |
![]() |
![]() ![]() |
![]() Figure 7. Usage of Algorithms in Action. Logging was done automatically during semester 1, 2001 and and semester 1, 2002. Numbers along the x-axis indicate teaching weeks, 1 being the first week of the semester. The y-axis shows the number of student sessions with AIA initiated during that week. Students can use multiple modules within a single session. Arrows indicate when Algorithms in Action was introduced to the class (week 5), and the week of the examination (week 15). Class size was approximately 300 students in both years.. |
![]() |
![]() Our initial intention was for students to use AIA in a top-down manner, first viewing algorithms at a more abstract level then increasing the level of detail (stepwise refinement). However, controlled observation revealed that some students used AIA in a bottom-up way, first expanding the pseudocode to give maximum detail then progressively contracting it as parts were understood. Following this observation, we concluded that students will use the tool according to their personal preferences and learning styles. We included a feature that allows the algorithm's pseudocode to be completely expanded with one mouse click, to facilitate bottom-up learning. Variations were also seen in the different use that students made of the textual explanations. During controlled observation, little use was made of the text, yet the detailed logging shows that some students make heavy use of the explanations. This may be due to different student preferences, but another possibility is that students are more reluctant to take the time to read text while being observed. Research of others suggests that textual explanations are essential for effective learning in algorithm animation systems [12, 9]. To date we have not performed specific evaluation of the ways recursion is visualized in AIA. Based on our more general evaluation, it seems likely that looking in depth at how students use the tool to learn recursive algorithms will reflect the variety of students' preferences and will also suggest further ways in which we can enhance the animation framework to support different styles of learning. |
![]() |
![]() |
11. Recursive,
Iterative or Both? At higher levels of abstraction, with the pseudocode contracted, it is possible to describe many algorithms in an iterative style, even when they may be coded recursively. Lower levels, with more detail, can reveal whether recursion or an iterative looping construct is used. Combining multiple implementations at the highest levels would emphasize to the students that recursion and iteration are merely implementation strategies, a concept that students often miss. As an example, we show an algorithm for red-black tree insertion. Red-black trees are a kind of balanced binary search tree. The insertion algorithm performs rotations, and also "splitting" operations, which manipulate data and determine what rotations are performed. Iterative insertion algorithms perform splitting and rotation as the tree is traversed from root to the leaf, where the data is eventually inserted [10], and can trivially be recoded as a tail recursive implementation, with the operations performed just before the recursive call. There are also left-recursive algorithms, where splitting and rotation are performed after the recursive calls return - effectively, as the tree is traversed from leaf back to root. Yet another alternative is middle-recursive [11], where splitting is performed during the traversal down to a leaf and rotation is performed during the upward traversal. Figure 8a shows the algorithm pseudocode at a high level in an iterative style. Figure 8b gives a lower level, or expanded, view. The two for loops have been expanded to reveal that the code is actually middle-recursive. The pseudocode expansion also shows that adding data at a leaf is done via the previous recursive call. By emphasising the common elements between different algorithm variations, we hope the students will gain a deeper understanding of algorithms and the variety of ways they can be implemented. |
![]() |
![]() |
![]() ![]() (a)
|
![]() Figure 8. Red-black tree insertion: (a) high level pseudocode, iterative; (b) lower level pseudocode, recursive implementation, showing relationship to iterative approach. |
![]() |
![]() ![]() 13. Availability |
![]() |
![]() |
14. Acknowledgments |
![]() |
![]() |
![]() ![]() 15. References [2] Brown, M., Najork, M., and Raisamo, R. A Java-based implementation of collaborative active textbooks. In VL97: Proceedings of the IEEE International Symposium on Visual Languages (1997), pp. 372-379. [3] Brown, M. H., and Najork, M. A. Collaborative active textbooks: A web-based algorithm animation system for an electronic classroom. SRC Research Report, Systems Research Center (1996). [4] Brummond, P. The complete collection of algorithm animations. Last accessed at URL http://www.cs.hope.edu/~alganim/ccaa on 4 September 2001. [5] Cormen, T., Leiserson, C., and Rivest, R. Introduction to Algorithms. MIT Press, 1990. [6] Dershem, H., and Brummond, P. Tools for web-based sorting animation. SIGCSE Bulletin 30: Proceedings of the 29th SIGCSE Technical Symposium on Computer Science Education, 1 (1998), 222-226. [7] Gloor, P., Dynes, S., and Lee, I. Animated algorithms: A hypermedia environment for "Introduction to Algorithms", MIT Press, 1993. ISBN 0-262-57096-3. [8] Kann, C., Lindeman, R. W., and Heller, R. Integrating algorithm animation into a learning environment. Computers Educ. 28 (1997), 223-228. [9] Naps, T., Eagan, J., and Norton, L. JAHVÉ - an environment to actively engage students in web-based algorithm visualization. SIGCSE Bull. 32 (2000), 109-113. [10] Sedgewick, R. Algorithms in C, 2nd ed. Addison-Wesley, 1990. [11] Sedgewick, R. Algorithms in C, Parts 1-4, 3rd ed. Addison-Wesley, 1998. [12] Stasko, J., Badre, A., and Lewis, C. Do algorithm animations assist learning? an empirical study and analysis. In Proceedings of the INTERCHI '93 Conference on Human Factors in Computing Systems (Amsterdam, 1993), pp. 61-66. [13] Stern, L., and Naish, L. Visual representations for recursive algorithms. In Proceedings of the 33rd SIGCSE Technical Symposium on Computer Science Education (Cincinnati, 2002), pp. 196-200. [14] Stern, L., Naish, L., and Sondergaard, H. Algorithms in action. http://www.cs.mu.oz.au/aia. [15] Stern, L., Søndergaard, H., and Naish, L. A strategy for managing content complexity in algorithm animation. In ITiCSE99: Proceedings of the Fourth Annual SIGCSE/SIGCUE Conference on Innovation and Technology in Computer Science Education (1999), pp. 127-130. [16] Wilcocks, D., and Sanders, I. Animating recursion as an aid to instruction. Computers Educ. 23 (1994), 221-226. |
![]()
|
![]() |
|
![]() |