1 / 24

RECURSION

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.

estrella
Download Presentation

RECURSION

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RECURSION Annie Calpe 11.12.04

  2. Overview • Introduction • Review: the Basics • How it works - Examples - Factorial - Fibonacci Sequence - Sierpinski Curve • Stacks • Applications • Design Considerations

  3. 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.

  4. 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.

  5. 4 Fundamental Rules : • 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.

  6. Basic Form : void recurse () { recurse (); //Function calls itself } int main () { recurse (); //Sets off the recursion }

  7. How does it work? • 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.

  8. 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 Recursion Trees

  9. 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

  10. Factorial Factorial (n): IF (n = 0) RETURN 1 ELSE RETURN n * Factorial (n-1) Calculates n*(n-1)*(n-2)*…*(1)*(1)

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

  12. Recursion Tree showing Fibonacci calls

  13. 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.

  14. 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)

  15. 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) The Sierpinski Curve

  16. ZIG(4) – ¼ Complete ZIG (4) ZAG ZIG ZIG (2) ZIG ZAG ZIG (1) ZIG (1) ZAG (1) ZAG (1) ** End of first ZIG (2) call

  17. ZIG(4) – ½ Complete ZIG (4) ZIG (2) ZAG (2) ZIG (2) ZAG (2) ZIG (1) ZIG (1) ZIG (1) ZAG (1) ZAG (1) ZAG (1) ZAG (1) ZAG (1)

  18. ZIG(4) – ½ Complete ZAG ZIG ZAG ZIG ZIG ZAG ZAG ZAG ** End of first ZAG (2) call

  19. ZIG(4) – The Rest? • No need to go further. Why? • ANSWER: Because of Rule #3 - We’ve shown that the base case works as well as the next case.

  20. 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

  21. Numerical analysis Graph theory Symbolic manipulation Sorting List processing Game playing General heuristic problem-solving Tree traversals Some Uses For Recursion

  22. Why use it? PROS • Clearer logic • Often more compact code • Often easier to modify • Allows for complete analysis of runtime performance CONS • Overhead costs

  23. 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.

  24. 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.

More Related