chap 2 recursion
TRANSCRIPT
-
8/2/2019 Chap 2 Recursion
1/26
-
8/2/2019 Chap 2 Recursion
2/26
IntroductionAfunction call itselfis called a recursive function.
Recursion is a repetitive process in which an
algorithm or function calls itself repeatedly untilsome specified condition has been satisfied.
The recursive function must include a stoppingcondition (anchor condition) or else the function
would never terminate (indefinite loop).Example of the use of recursive algorithms:Factorial, Fibonacci sequence (number) and Towerof Hanoi.
-
8/2/2019 Chap 2 Recursion
3/26
Iteration vs. RecursionThere are two approaches to writing repetitivealgorithm: iteration & recursion.
Recursive algorithm take more storage and time thanthe iterative version.
This is because a recursive function must store andretrieve the value of each recursive call.
Although many function can be computed easily
without using recursion, there are others that areeasier to compute with recursion than without it.Example: Tower of Hanoi game problem.
-
8/2/2019 Chap 2 Recursion
4/26
When to used Recursion?If the algorithm or data structure naturally suited torecursion. Example Tower of Hanoi and Quick Sort.
If the recursive solution shorter and moreunderstandable. Example Merge Sort.
If the recursive solution run within acceptable timeand space limit.
-
8/2/2019 Chap 2 Recursion
5/26
Problem 1: FactorialThe product of the integral positive integers from 1 ton, is called nfactorial.
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
-
8/2/2019 Chap 2 Recursion
6/26
Example of factorial3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 245! = 5 * 4 * 3 * 2 * 1 = 120
6! = 6 * 5 * 4 * 3 * 2 * 1 = 720
-
8/2/2019 Chap 2 Recursion
7/26
Iterative Algorithm to calculate n
factorial1. Set fact= 1
2. Set i=number
3. Loop (i>1)3.1. Begin
3.2. fact= fact* i
3.3. i - -
3.4. End
4. Return fact
-
8/2/2019 Chap 2 Recursion
8/26
Programming/*Factorial (n) computation using
iteration*/
#include #include
int fact (int )void main(){ int fact (int);
int number, factnum;
printf(\nEnter a number: ");scanf( %d,&number);factnum=fact(number);
printf("\nThe factorial for );printf(%d! = %d",number,factnum);
}
/*Iterative factorial computation*/
int fact (int number)
{
int i, fact=1;for(i=number;i>1;i--)
{
fact=fact*i;
}
return fact;
}
Output:
Enter a number: 4
The factorial of 4! = 24
-
8/2/2019 Chap 2 Recursion
9/26
Recursive Algorithm to calculate
n factorial1. If (n== 0)
1.1.Return11. Else
2.1. Return (num * fact(num-1))
-
8/2/2019 Chap 2 Recursion
10/26
//Recursive factorial
int fact (int num)
{
if(number==0)
{
return 1;
}
else
{return (num*fact(num-1));
}
}
Programming//Factorial (n) computation using
recursion
#include
#include
void main(){ int fact (int);
int num,factnum;
printf(\nEnter a number: ");
scanf( %d, &num);factnum=fact(num);
printf("\nThe factorial of );
printf(" %d!=%d", num, factnum);
} Output:Enter a number: 4
The factorial of 4! = 24
-
8/2/2019 Chap 2 Recursion
11/26
Traces the executionn = 4Since n!
-
8/2/2019 Chap 2 Recursion
12/26
Problem 2: FibonacciThe Fibonacci sequence number isdefined as
0, 1, 1, 2, 3, 5, 8, 13, 21, . . .
Each number after the first two is thesum of the two preceding number.
F0 = 0
F1 = 1
Fn = Fn-1 + Fn-2 for n>=2
-
8/2/2019 Chap 2 Recursion
13/26
QuestionWhat is the next 2 terms ofthe following Fibonacci
sequence number?
0, 1, 1, 2, 3, 5, 8, 13, 21, ?, ?
-
8/2/2019 Chap 2 Recursion
14/26
Example of fibonacciF(0) = 0
F(1) = 1F(2) = 0 + 1 = 1
F(3) = 1 + 1 = 2
F(4) = 1 + 2 = 3F(5) = 2 + 3 = 5
F(6) = 3 + 5 = 8
-
8/2/2019 Chap 2 Recursion
15/26
Fibonacci algorithm1. If (num= 0 or num= 1)
Stopping Condition (for which the function does not
call/refer to itself)1.1 Return num
2. Return (fib(num-1) + fib(num-2)
-
8/2/2019 Chap 2 Recursion
16/26
int fib(int n){
if (num==0||num==1)return num;
return (fib(num-1)+fib(num-2));}
Programming//Fibbonaci sequence using recursion#include #include
void main(){int fib(int);int num;printf("\nEnter num for fibonaccisequence: ");
scanf("%d",&num);
printf("\nFibonacci %d =%d",n,fib(num));
getch();}
Output:
Enter n for fibonacci sequence: 4
Fibonacci 4 = 3
-
8/2/2019 Chap 2 Recursion
17/26
Problem 3: Towers of HanoiSuppose 3 needles labeled A, B, C and a setof disks (varying sizes)
Arranged the biggest disk at the bottom tothe smallest disk at the top
Move the disk, one by one from needles A toC, using needles B as an intermediate.
Each needles must always be arranged formthe biggest at the bottom to the smallest atthe top
http://www.cut-the-knot.org/recurrence/hanoi.shtml
http://www.mazeworks.com/hanoi/
-
8/2/2019 Chap 2 Recursion
18/26
Illustration: Initial state for
Towers of Hanoi
A B C
-
8/2/2019 Chap 2 Recursion
19/26
Solution to the Towers of Hanoi
for disks = 3
A B C
Initial
A B C
Step 1: A -> C
-
8/2/2019 Chap 2 Recursion
20/26
Continue
A B C
Step 2: A -> B
A B C
Step 3: C -> B
-
8/2/2019 Chap 2 Recursion
21/26
Continue
A B C
Step 4: A -> C
A B C
Step 5: B -> A
-
8/2/2019 Chap 2 Recursion
22/26
Continue
A B C
Step 6: B -> C
A B C
Step 7: A -> C
-
8/2/2019 Chap 2 Recursion
23/26
Algorithm for disks movements
Towers of Hanoi1. If (numDisks = 1)
1.1 Print (Move disk 1 from, from, to destination)
1. Else2.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
-
8/2/2019 Chap 2 Recursion
24/26
Programming//Tower of Hanoi#include
#include
void main()
{
void move(int, char,char,char);
int numDisk;
printf("\nEnter number of disks: ");
scanf(" %d", &numDisk);
if(numDisk>0)
move(numDisk , 'A','C','B');
getch();
}
void move(int numDisk, char from, char destination , char
intermediate){
if (numDisk==1)
printf("\nMove disk 1 from needle %c to needle %c",from,to);
else
{
move(numDisk-1, from, intermediate, destination);
printf("\nMove disk %d from needle %c to needle%c",numDisk,from,to);
move(numDisk-1, intermediate, destination , from);
}
}
Enter number of disks: 3Move 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
-
8/2/2019 Chap 2 Recursion
25/26
Illustration of solution to Towers
of Hanoi problem for n = 3
Move(3,A,C,B)
Move(2,A,B,C)
Move(2,B,C,A)
A ->C
Move(1,A,C,B)
Move(1,C,B,A)
A ->B
A ->C
C ->B
Move(1,B,A,C)
Move(1,A,C,B)
B ->C
B ->A
A ->C
-
8/2/2019 Chap 2 Recursion
26/26
ExerciseLab sheet 1: Recursion