Chapter 05 Recursion

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Chapter 5: Recursion

Introduction
Recursion is a repetitive process in which an
algorithm or function calls itself repeatedly until
some specified condition has been satisfied.
A function call itself is called a recursive function.
The recursive function must include a stopping
condition (anchor condition) or else the function
would never terminate (indefinite loop).
Example of the use of recursive algorithms:
Factorial, Fibonacci sequence (number) and Tower
of Hanoi.
Iteration vs. Recursion
There are two approaches to writing repetitive
algorithm: iteration & recursion.
Recursive algorithm take more storage and time than
the iterative version.
This is because a recursive function must store and
retrieve the value of each recursive call.
Although many function can be computed easily
without using recursion, there are others that are
easier to compute with recursion than without it.
Example: Tower of Hanoi game problem.
When to used Recursion?
If the algorithm or data structure naturally suited to
recursion. Example Quick Sort.

If the recursive solution shorter and more


understandable. Example Merge Sort.

If the recursive solution run within acceptable time


and space limit.
Problem 1: Factorial
The product of the integral positive integers from 1
to n, is called “n factorial”.
Denoted by n!
Factorial (n) = 1 for
n=0
Factorial (n) = n * (n – 1) * (n – 2) *…. * 1 for n>0
= n * factorial (n-1) for
n>0
Example of factorial
3! = 3*2*1 =6
4! = 4*3*2*1 =
24
5! = 5*4*3*2*1 =
120
6! = 6*5*4*3*2*1 =
720
Iterative Algorithm to calculate n
factorial
1. Begin
2. Set fact = 1
3. Set i =number
3. Loop (i>1)
3.1. Begin
3.2. fact = fact * i
3.3. i--
3.4. End
4. Return fact
5. End
Programming
/*Factorial (n) computation using /*Iterative factorial computation*/
iteration*/

#include <stdio.h> int fact (int number)


#include <conio.h> {
int i, fact=1;
int fact (int ) for(i=number;i>1;i--)
void main()
{ int fact (int); {
int number, factnum; fact=fact*i;
}
printf(“\nEnter a number: ");
scanf(“ %d”,&number); return fact;
factnum=fact(number);
}
printf("\nThe factorial for “); Output:
printf(“%d! = %d",number,factnum); Enter a number: 4
} The factorial of 4! = 24
Recursive Algorithm to calculate
n factorial
1. Begin
2. If (n == 0)
1.1.Return 1
3. Else
2.1. Return (num * fact(num-1))
4. End
Programming
//Factorial (n) computation using //Recursive factorial
recursion int fact (int num)
#include <stdio.h> {
#include <conio.h> if(num==0)
void main()
{
{ int fact (int);
return 1;
int num,factnum;
}
printf(“\nEnter a number: "); else
scanf(“ %d”, &num); {
factnum=fact(num); return (num*fact(num-1));
printf("\nThe factorial of “);
}
printf(" %d!=%d", num, factnum);
} Output:
}
Enter a number: 4
The factorial of 4! = 24
24
factorial (4)
n=4
factorial (4) = 24
return 4*6
Traces the execution
Since n<1
return 4 * factorial(3)
factorial (3) factorial (3) = 6
n=3 return 3*2
Since n<1
return 3* factorial(2)
factorial (2) factorial (2) = 2
n=2
return 2*1
Since n<1
return 2* factorial(1)
factorial (1)
factorial (1) = 1
n=1
Since n<1 return 1*1
return 1* factorial(0)
factorial (0)
factorial (0) = 1
n=0
Since n<1 return 1
return 1
Problem 2: Fibonacci
The Fibonacci sequence number is
defined as
0, 1, 1, 2, 3, 5, 8, 13, 21, . . .
Each number after the first two is the
sum of the two preceding number.
F0 = 0
F1 = 1
Fn = Fn-1 + Fn-2 for n>=2
Question
What is the next 2 terms of
the following Fibonacci
sequence number?

0, 1, 1, 2, 3, 5, 8, 13, 21, ?, ?
Example of fibonacci
F(0) = 0
F(1) = 1
F(2) = 0 + 1 = 1
F(3) = 1 + 1 = 2
F(4) = 1 + 2 = 3
F(5) = 2 + 3 = 5
F(6) = 3 + 5 = 8
Fibonacci algorithm
1. Begin
2. If (num = 0 or num = 1)
Stopping Condition (for which the function does
not call/refer to itself)
1.1 Return num
3. Return (fib(num-1) + fib(num-2)
4. End
Programming
//Fibbonaci sequence using recursion int fib(int n)
#include <stdio.h> {
#include <conio.h> if (num==0||num==1)
return num;
void main()
{ return (fib(num-1)+fib(num-2));
int fib(int); }
int num;
printf("\nEnter num for fibonacci
sequence: ");
scanf("%d",&num);

printf("\nFibonacci %d = Output:
%d",n,fib(num)); Enter n for fibonacci sequence: 4
getch(); Fibonacci 4 = 3
}
Exercise
Lab sheet 1: Power function using
Recursion

You might also like