6.0_Recursion
6.0_Recursion
Algorithm (IT-209)
Instructor: Abdullah Javed
([email protected])
Lecturer,
Govt. Postgraduate College, Jhelum
Lecture 6.0
Fall 2020
Recursion
• Some computer programming languages
allow a module or function to call itself. This
technique is known as recursion.
• Inrecursion, a function ‘a’ either calls itself
directly or calls a function ‘b’ that in turn
calls the original function ‘a’. The function
‘a’ is called recursive function
• Example
int function( int value ) {
if ( value < 1 ) return;
function ( value – 1 );
printf ( “%d”, value );
2
Recursion
• Recursion
is a principle closely related to
mathematical induction.
• Ina recursive definition, an object is defined
in terms of itself.
• Wecan recursively define sequences,
functions and sets.
• Recursionis 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.
3
Properties
• A recursive function can go infinite like a loop. To
avoid infinite running of recursive function, there
are two properties that a recursive function must
have
• Base/Anchor Criteria:
There must be at least one base criteria or condition,
such that, when this condition is met the function stops
calling itself recursively
• Progressive/Inductive/Recursive Approach:
The recursive calls should progress in such a way that
each time a recursive call is made it comes closer to the
base criteria
4
Implementation
• Many programming languages implement recursion
by means of stacks. Whenever a function (caller)
calls another function (callee), the caller function
transfers execution control to the callee.
• This implies, caller function has to suspend its
execution temporarily and resume later. Here, the
caller function needs to start exactly from the same
point where it puts itself on hold. It also needs the
exact same data values it was working on. For this
purpose, an activation record (or stack frame) is
created for the caller function. This keeps the
information about local variables, formal
parameters, return address and all information 5
Implementation (Continued…)
6
Analysis of Recursion
• Time Complexity
In case of recursion we try to figure out the number of times a
recursive call is being made. A call made to a function is Ο(1),
hence the (n) number of times a recursive call is made makes
the recursive function Ο(n).
• Space Complexity
Calculates what amount of extra space is required for a
module to execute. In case of iterations, the compiler hardly
requires any extra space. In case of recursion, the system
needs to store activation record each time a recursive call is
made. Hence, it is considered that space complexity of
recursive function may go higher than that of a function with
iteration.
7
Factorial Function
• The product of the positive integers from 1 to
n, inclusive, is called “n factorial” and it
usually denoted by n!
• Definition:
a) if n = 0, then n! = 1
b) if n > 0, then n! = n . ( n – 1 )!
8
Factorial Function (Continued…)
• Write a recursive function to calculate factorial of a given number
long f;
if ( num == 0 )
f = 1;
else
return f;
9
Fibonacci Sequence
• The Fibonacci sequence (denoted by F0, F1,
F2, …) is as follows:
• 0, 1, 1, 2, 3, 5,8, 13, 21, 34, 55, …
• Thatis, F0 = 0 and F1 = 1 and each
succeeding term is the sum of the two
preceding terms. For example the next two
terms of the sequence are
34 + 55 = 89 and 55 + 89 = 144
• Definition:
a) if n = 0 or n = 1, then Fn = n 10
Fibonacci Sequence (Continued…)
• Write a recursive function to calculate Fibonacci sequence
return fib;
}
11