Вы находитесь на странице: 1из 31

# CHAPTER 2

Recursion
Learning Outcomes (LO)
• Show the understanding concept or recursion
• Construct a programme by apply the
technique of 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 Tower of Hanoi and 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
4. Loop (i>1)
4.1. Begin
4.2. fact = fact * i
4.3. i - -
4.4. End
5. Return fact
6. 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 “);
printf(“%d! = %d",number,factnum);
} Output:
Enter a number: 4
The factorial of 4! = 24
Recursive Algorithm to calculate n
factorial

1. Begin
2. If (n == 0)
2.1.return 1
3. Else
3.1. return (num * fact(num-1))
4. End
Programming
//Factorial (n) computation using recursion //Recursive factorial
#include <stdio.h> int fact (int num)
#include <conio.h> {
void main() if(number==0)
{ int fact (int); {
int num,factnum;
return 1;
}
printf(“\nEnter a number: ");
else
scanf(“ %d”, &num);
factnum=fact(num); {
printf("\nThe factorial of “); return (num*fact(num-1));
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: Multiplication
• Multiplication of two numbers can be
Example : To multiply 6 x 3, the result can be
achieved by adding 6, three times as follows:

6 + 6 + 6 = 18
Recursive Algorithm to multiplication
1. Begin
3. If (times == 1)
3.1.return num
4. Else
4.1. return (num + multiply (num,times-1))
5. End
Programming
//Multiplication using recursion //Recursive multiplication
#include <stdio.h> int multiply(int num,int times){
#include <conio.h> if(times==1)
return num;
void main() else
{ return num +
int multiply(int,int); multiply(num,times-1);
int num,times; }
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter how many times: ");
scanf("%d",&times);
Output:
printf("\nResult %d * %d = Enter a number: 6
%d",num,times,multiply(num,times));
Enter how many times: 3
getch(); Result 6 * 3 = 18
}
multiply(int num, int times)
multiply(6,3) Traces the execution
num = 6,
times = 3
times != 1,
multiply() will be called

return 6 + multiply(6, 3 - 1)
num = 12,
times = 2
times != 1,
multiply() will be called
return 12 + multiply(12, 2 - 1)
num = 18,
times = 1
times == 1,
Return num=18
Problem 3: 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)
2.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
}
Problem 4: Towers of Hanoi
• Suppose 3 needles labeled A, B, C and a set of disks
(varying sizes)
• Arranged the biggest disk at the bottom to the
smallest disk at the top
• Move the disk, one by one from needles A to C, using
needles B as an intermediate.
• Each needles must always be arranged form the
biggest at the bottom to the smallest at the top

http://www.cut-the-knot.org/recurrence/hanoi.shtml
http://www.mazeworks.com/hanoi/
Illustration: Initial state for Towers of
Hanoi

A B C
Solution to the Towers of Hanoi
for disks = 3

A B C A B C

Continue…

A B C A B C

Continue…

A B C A B C

Continue…

A B C A B C

## Step 6: B -> C Step 7: A -> C

Algorithm for disks movements Towers
of Hanoi

1. Begin
2. If (numDisks = 1)
1.1 Print (“Move disk 1 from”, from, “to” destination)
3. Else
2.1 Begin
2.2 Move(numDisks-1, from, intermediate, destination)
2.3 Print (“Move”, numDisks, “from”, from “to”, destination)
2.4 Move(numDisks-1,intermediate, destination , from)
2.5 End
4. End
//Tower of Hanoi Programming
#include <stdio.h> void move(int numDisk, char from, char destination , char
#include <conio.h> intermediate)
{
void main() if (numDisk==1)
{ printf("\nMove disk 1 from needle %c to needle %c",
void move(int, char,char,char); from,to);
int numDisk;
printf("\nEnter number of disks: "); else
scanf(" %d", &numDisk); {
if(numDisk>0)
move(numDisk , 'A','C','B'); move(numDisk-1, from, intermediate, destination);

## getch(); printf("\nMove disk %d from needle %c to needle

} %c",numDisk,from,to);

## Enter number of disks: 3

move(numDisk-1, intermediate, destination , from);
Move disk 1 from needle A to needle C
}
Move disk 2 from needle A to needle B
}
Move disk 1 from needle C to needle B
Move disk 3 from needle A to needle C
Move disk 1 from needle B to needle A
Move disk 2 from needle B to needle C
Move disk 1 from needle A to needle C
Illustration of solution to Towers of
Hanoi problem for n = 3

Move(1,A,C,B) A ->C

Move(2,A,B,C) A ->B

Move(1,C,B,A) C ->B

Move(3,A,C,B) A ->C
Move(1,B,A,C) B ->A

Move(2,B,C,A) B ->C

Move(1,A,C,B) A ->C
Thank You..
Any questions..?