cp0805_18-aug-2011_rm01

Upload: revathi-durai

Post on 06-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    1/24

    RECURSION

    Annie Calpe

    11.12.04

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    2/24

    Overview

    Introduction Review: the Basics

    How it works - Examples- Factorial- Fibonacci Sequence- Sierpinski Curve

    Stacks Applications Design Considerations

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    3/24

    Introduction to Recursion

    Recursion can be used to manage

    repetition.

    Recursion is a process in which amodule achieves a repetition of

    algorithmic steps by calling itself.

    Each recursive call is based on a

    different, generally simpler, instance.

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    4/24

    Introduction to Recursion

    Recursion is something of a divide andconquer, top-down approach to problem

    solving.

    - It divides the problem into pieces orselects out one key step, postponing therest.

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    5/24

    4 Fundamental Rules :

    1. Base Case: Always have at least one casethat can be solved without recursion.

    2. Make Progress: Any recursive call mustprogress towards a base case.

    3. Always Believe: Always assume therecursive call works.

    4. Compound Interest Rule: Never duplicatework by solving the same instance of aproblem in separate recursive calls.

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    6/24

    Basic Form :

    void recurse (){

    recurse (); //Function calls itself}

    int main ()

    {recurse (); //Sets off the recursion

    }

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    7/24

    How does it work?

    1. The module calls itself.

    2. New variables and parameters are allocatedstorage on the stack.

    3. Function code is executed with the newvariables from its beginning. It does not makea new copy of the function. Only thearguments and local variables are new.

    4. As each call returns, old local variables andparameters are removed from the stack.

    5. Then execution resumes at the point of the

    recursive call inside the function.

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    8/24

    Recursion Trees

    A key tool for analyzing recursive algorithmsis the recursion tree.

    The total processing time is related to thetotal # of nodes

    The necessary storage space is related to itsheight

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    9/24

    To Build a Recursion Tree:

    root = the initial call

    Each node = a particular callEach new call becomes a child of the nodethat called it

    A tree branch (solid line) = a call-return pathbetween any 2 call instances

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    10/24

    Factorial

    Factorial (n):

    IF (n = 0)

    RETURN 1ELSE

    RETURN n * Factorial (n-1)

    Calculates n*(n-1)*(n-2)**(1)*(1)

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    11/24

    Fibonacci Numbers

    Fibonacci (n)

    IF (n

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    12/24

    Recursion Tree showing

    Fibonacci calls

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    13/24

    Space Filling Curves

    A continuous mapping from a lower-dimensionalspace into a higher-dimensional one, using fractals.

    Fractals are shapes that occur inside other, similarshapes.

    A useful property of a space-filling curve is that ittends to visit all the points in a region once it hasentered that region.

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    14/24

    The Sierpinski Curve

    The limiting curve of an infinite sequenceof curves numbered by an index n=1,2,3

    It ends up covering every point in theregion.

    Fills 2-D space (fills a plane using lines)

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    15/24

    The Sierpinski Curve

    ZIG (n):

    if (n = 1)

    turn left, advance 1turn left, advance 1

    else

    ZIG (n/2)ZAG (n/2)

    ZIG (n/2)

    ZAG (n/2)

    ZAG (n):

    if (n = 1)

    turn right, advance 1turn right, advance 1

    turn left

    else

    ZAG (n/2)ZAG (n/2)

    ZIG (n/2)

    ZAG (n/2)

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    16/24

    ZIGZAG

    ZIG

    ZAG

    ZIG (4)

    ZIG (2)

    ZIG

    (1)

    ZAG(1)

    ZIG

    (1)

    ZAG(1)

    ZIG(4) Complete

    ** End of first ZIG (2) call

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    17/24

    ZIG(4) Complete

    ZIG (4)

    ZIG (2)

    ZIG(1)

    ZAG(1)

    ZIG(1)

    ZAG(1)

    ZAG(1)

    ZIG(1)

    ZAG(1)

    ZAG(1)

    ZAG (2) ZIG (2) ZAG (2)

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    18/24

    ZIGZAG

    ZIG

    ZAGZAG

    ZAG

    ZIGZAG

    ZIG(4) Complete

    ** End of first ZAG (2) call

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    19/24

    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.

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    20/24

    Recursion is controlled in a computer bymeans of a pushdown stack.

    A push = a new function callA pop = a completed execution of

    a function call

    Stack overflow is possible

    Run-time Stack Use

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    21/24

    Some Uses For Recursion

    Numerical analysis

    Graph theory

    Symbolic

    manipulation

    Sorting

    List processing

    Game playing

    General heuristic

    problem-solving

    Tree traversals

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    22/24

    Why use it?

    PROS Clearer logic

    Often more compact code

    Often easier to modify

    Allows for complete analysis of runtime

    performance

    CONS Overhead costs

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    23/24

    Summary

    Recursion can be used as a very powerfulprogramming tool

    There is a tradeoff between time spentconstructing and maintaining a program andthe cost in time and memory of execution.

  • 8/3/2019 CP0805_18-Aug-2011_RM01

    24/24

    References

    The New Turing Omnibus Dewdney

    Data Structures & Problem Solving in Java

    Weiss Computing and Algorithm Shackelford

    Recursion Tutorial National University ofIreland, Dept of I.T.

    http://geminga.it.nuigalway.ie/cai_tutor/recursio.htmhttp://geminga.it.nuigalway.ie/cai_tutor/recursio.htm