Download presentation
Presentation is loading. Please wait.
1
RECURSION Annie Calpe
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
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
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
The Sierpinski Curve ZIG (n): if (n = 1) turn left, advance 1 else
ZAG (n/2) ZAG (n): if (n = 1) turn right, advance 1 turn left else ZAG (n/2) ZIG (n/2)
16
ZIG(4) – ¼ Complete ** End of first ZIG (2) call ZIG (4) ZAG ZIG
17
ZIG(4) – ½ Complete ZIG (4) ZIG (2) ZAG (2) ZIG (2) ZAG (2) ZIG (1)
18
ZIG(4) – ½ Complete ** End of first ZAG (2) call ZAG ZIG ZAG ZIG ZIG
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 A move down = a push onto runtime stack A move up = a pop & a move down = a push
21
Some Uses For Recursion
Numerical analysis Graph theory Symbolic manipulation Sorting List processing Game playing General heuristic problem-solving Tree traversals
22
Why use it? PROS CONS 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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.