Sei sulla pagina 1di 24

RECURSION

Annie Calpe 11.12.04

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

Recursion is something of a divide and conquer, top-down approach to problem solving.


- It divides the problem into pieces or selects out one key step, postponing the rest.

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 (); }

//Function calls itself

//Sets off the recursion

How does it work?


1. 2. 3.

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

To Build a Recursion Tree:


root = the initial call


Each node = a particular call Each new call becomes a child of the node that called it A tree branch (solid line) = a call-return path between any 2 call instances

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 !!

Recursion Tree showing Fibonacci calls

Space Filling Curves

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 Sierpinski Curve

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)

The Sierpinski Curve


ZIG (n): if (n = 1) turn left, advance 1 turn left, advance 1 else ZIG (n/2) ZAG (n/2) ZIG (n/2) ZAG (n/2) ZAG (n): if (n = 1) turn right, advance 1 turn right, advance 1 turn left else ZAG (n/2) ZAG (n/2) ZIG (n/2) ZAG (n/2)

ZIG(4) Complete
ZIG (4) ZIG (2) ZIG ZAG ZIG

ZAG (1) ZIG (1) ZAG (1) ZIG (1)

ZAG

** End of first ZIG (2) call

ZIG(4) Complete

ZAG (2) ZIG (2)

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

** End of first ZAG (2) call

ZIG(4) The Rest?

No need to go further. Why?


ANSWER: Because of Rule #3 - Weve shown that the base case works as well as the next case.

Run-time Stack Use

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

Some Uses For Recursion

Numerical analysis
Graph theory Symbolic manipulation Sorting

List processing
Game playing General heuristic problem-solving Tree traversals

Why use it?


PROS

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.

Potrebbero piacerti anche