Download presentation
Presentation is loading. Please wait.
1
Recursion Lakshmish Ramaswamy
2
Basics of Recursion A recursive method makes a call to itself directly or indirectly Is this not endless circular logic? Call is for a simpler instance Several real-world applications are recursive Dictionary lookup for definitions Scanning files on a computer Computer languages
3
A Simple Example Calculate sum of first N integers (S(N))
We know S(1) = 1 S(N) can be expressed as S(N) = S(N-1) + N S(2) = 2 + S(1) S(3) = S(2) + 3 S(N) = N(N+1)/2 <---- Closed form Closed form are often complex Recursive expressions might be simpler
4
Recursive Method for S(N)
5
Points to Note Function calls a clone of itself (note the difference in parameters) Only one clone is active at any instance Computer does all the book keeping for you Base case – Instance that can be solved without recursion S(1) is the base case Recursive call should make progress towards base case
6
Two Rules Always have at least one base case solved without recursion
Any recursive call should make progress towards base case
7
Second Example Printing numbers in any base
Simpler case – Printing number in decimal form No method that can print an integer Method that can print a single character Digits are treated as characters print(2457) can be expressed as print(245) followed by printing 7 (via character printing method)
8
Decimal Printing Method
Identify the base condition? How is the progress done?
9
Printing Number in any Base
Exception if base > 16 Infinite loop if base is 1 Notice identical call to the method (No progress)
10
Robust Implementation
11
How is Recursion Supported
Method invocation requires bookkeeping Recursion is no different (may be a special case) Activation records for implementing methods Activation records hold important information like parameter values & local variables
12
Stacks for Activation Records
Stack is a good data structure for reversing order First-In-Last-Out paradigm Why stacks? Notice that methods return in reverse order of invocation Activation record at top is that of currently active method When a method is called, its activation record is pushed on the stack Top record is popped when the method returns
13
Stacks and Recursive Methods
Each method invocation results in an activation record The base case is the last one to be pushed in and the first one to pop out
14
Fibonacci Numbers Definition Very interesting properties
FN = FN-1 + FN-2 F1 = 1 F0 = 0 Very interesting properties Sum of first N Fibonacci numbers < FN+2 Sum of squares of two consecutive Fibonacci numbers is also a Fibonacci number Natural recursive implementation
15
Simple but Inefficient Implementation
16
What is the Problem? Each call to fib(n-1) and fib(n-2) results in calling fib(n-3) Keeps getting worse – Observe how many times F2, F1 and F0 are called Lesson – Avoid duplicate work of solving same instance in separate duplicate calls
17
Factorial Computation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.