0% found this document useful (0 votes)
8 views

Lecture 8 (DS) - Recursion

The document discusses recursion in programming, explaining how a function can call itself and the properties necessary to avoid infinite loops. It covers the implementation of recursion using stacks, time and space complexity analysis, and provides examples of recursive functions for calculating factorials and the Fibonacci sequence. Additionally, it includes examples of recursive functions for computing values such as GCD.

Uploaded by

baba.nazar7204
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 8 (DS) - Recursion

The document discusses recursion in programming, explaining how a function can call itself and the properties necessary to avoid infinite loops. It covers the implementation of recursion using stacks, time and space complexity analysis, and provides examples of recursive functions for calculating factorials and the Fibonacci sequence. Additionally, it includes examples of recursive functions for computing values such as GCD.

Uploaded by

baba.nazar7204
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Structure and Algorithms

Lecture # 8

Ehtisham Rasheed

Department of Computer Science


University of Gurjat, Gujrat
Recursion
• Some computer programming languages allow a module or
function to call itself. This technique is known as recursion.
• In recursion, 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 );
2
printf ( “%d”, value );
}
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
• Basic 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 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
3
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 passed to the caller function. 4
Implementation (Continued…)

5
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.
6
Factorial Function
• The product of the positive integers from 1 to n, inclusive, is
called “n factorial” and it usually denoted by n!

• It is also convenient to define 0! = 1

• Definition:
a) if n = 0, then n! = 1
b) if n > 0, then n! = n . ( n – 1 )! 7
Factorial Function (Continued…)
• Write a recursive function to calculate factorial of a given number
Long fact ( int num ) {
long f;
if ( num == 0 )
f = 1;
else
f = num * fact ( num – 1 )
return f;
} 8
Fibonacci Sequence
• The Fibonacci sequence (denoted by F0, F1, F2, …) is as follows:
• 0, 1, 1, 2, 3, 5,8, 13, 21, 34, 55, …
• That is, 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
9

b) if n > 1, then Fn = Fn-2 + Fn-1


Fibonacci Sequence (Continued…)
• Write a recursive function to calculate Fibonacci sequence
int fibo ( int num ) {
long fib, fibA, fibB;
if ( num == 0 || num == 1 ) { fib = num; return fib; }

fibA = fibo ( num – 2 );


fibB = fibo ( num – 1 );
fib = fibA + fibB;
return fib; 10

}
Recursion Example
• Let a and b denote positive integers. Suppose a function Q is
defined recursively as follows:

• Find the value of Q(2, 3), Q (14, 3) and Q(34, 12).


11
Recursion Example
• Let A and B be nonnegative integers. Suppose a function GCD is
recursively defined as follows:

• Find GCD(6, 15), GCD(20,28) and GCD(40, 10)


12

You might also like