Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Overview
Introduction Review: the Basics How it works - Examples - Factorial - Fibonacci Sequence - Sierpinski Curve Stacks Applications Design Considerations
Introduction to Recursion
Recursion can be used to manage repetition. Recursion is a process in which a module achieves a repetition of algorithmic steps by calling itself.
Each recursive call is based on a different, generally simpler, instance.
Introduction to Recursion
4 Fundamental Rules :
1.
2. 3. 4.
Base Case : Always have at least one case that can be solved without recursion. Make Progress : Any recursive call must progress towards a base case. Always Believe : Always assume the recursive call works. Compound Interest Rule : Never duplicate work by solving the same instance of a problem in separate recursive calls.
Basic Form :
void recurse () { recurse (); }
int main () { recurse (); }
4. 5.
The module calls itself. New variables and parameters are allocated storage on the stack. Function code is executed with the new variables from its beginning. It does not make a new copy of the function. Only the arguments and local variables are new. As each call returns, old local variables and parameters are removed from the stack. Then execution resumes at the point of the recursive call inside the function.
Recursion Trees
A key tool for analyzing recursive algorithms is the recursion tree. The total processing time is related to the total # of nodes The necessary storage space is related to its height
Factorial
Factorial (n): IF (n = 0) RETURN 1 ELSE RETURN n * Factorial (n-1)
Calculates n*(n-1)*(n-2)**(1)*(1)
Fibonacci Numbers
F (n) = F (n-1) + F (n-2)
Fibonacci (n) IF (n <= 1) RETURN n ELSE RETURN Fibonacci (n-1) + Fibonacci (n-2)
** Inefficient use of recursion !!
A continuous mapping from a lower-dimensional space into a higher-dimensional one, using fractals.
Fractals are shapes that occur inside other, similar shapes. A useful property of a space-filling curve is that it tends to visit all the points in a region once it has entered that region.
The limiting curve of an infinite sequence of curves numbered by an index n=1,2,3 It ends up covering every point in the region. Fills 2-D space (fills a plane using lines)
ZIG(4) Complete
ZIG (4) ZIG (2) ZIG ZAG ZIG
ZAG
ZIG(4) Complete
ZIG (4)
ZIG (2) ZAG (1) ZIG (1) ZAG (1) ZAG (1) ZAG (1) ZIG (1) ZAG (1) ZIG (1)
ZAG (2)
ZIG(4) Complete
ZAG ZIG ZAG ZIG ZAG ZIG
ZAG
ZAG
Recursion is controlled in a computer by means of a pushdown stack. A push = a new function call A pop = a completed execution of a function call Stack overflow is possible
Numerical analysis
Graph theory Symbolic manipulation Sorting
List processing
Game playing General heuristic problem-solving Tree traversals
Clearer logic Often more compact code Often easier to modify Allows for complete analysis of runtime performance Overhead costs
CONS
Summary
Recursion can be used as a very powerful programming tool There is a tradeoff between time spent constructing and maintaining a program and the cost in time and memory of execution.
References
The New Turing Omnibus Dewdney Data Structures & Problem Solving in Java Weiss Computing and Algorithm Shackelford Recursion Tutorial National University of Ireland, Dept of I.T.