Sie sind auf Seite 1von 26

Chapter 2: Recursion

Introduction
A function call itself is called a recursive function. Recursion is a repetitive process in which an algorithm or function calls itself repeatedly until some specified condition has been satisfied. 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! 4! 5! 6! = = = = 3 4 5 6 * * * * 2 3 4 5 * * * * 1 2*1 3*2*1 4*3*2*1 = = = = 6 24 120 720

Iterative Algorithm to calculate n factorial


1. 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

Programming
/*Factorial (n) computation using iteration*/ #include <stdio.h> #include <conio.h> 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

Recursive Algorithm to calculate n factorial


1. If (n == 0) 1.1.Return 1 1. Else 2.1. Return (num * fact(num-1))

Programming
//Factorial (n) computation using recursion #include <stdio.h> #include <conio.h> 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); } //Recursive factorial int fact (int num) { if(number==0) { return 1; } else { return (num*fact(num-1)); } }
Output: Enter a number: 4 The factorial of 4! = 24

factorial (4)

24
factorial (4) = 24 return 4*6

n=4 Since n!<1 return 4 * factorial(3) factorial (3) n=3 Since n!<1 return 3* factorial(2) factorial (2)

Traces the execution

factorial (3) = 6 return 3*2

factorial (2) = 2 return 2*1

n=2 Since n!<1 return 2* factorial(1) factorial (1) n=1 Since n!<1 return 1* factorial(0) factorial (0) n=0 Since n<1 return 1

factorial (1) = 1 return 1*1 factorial (0) = 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) F(1) F(2) F(3) F(4) F(5) F(6) = = = = = = = 0 1 0 1 1 2 3

+ + + + +

1 1 2 3 5

= = = = =

1 2 3 5 8

Fibonacci algorithm
1. If (num = 0 or num = 1) Stopping Condition (for which the function does not call/refer to itself) 1.1 Return num Return (fib(num-1) + fib(num-2)

2.

Programming
//Fibbonaci sequence using recursion #include <stdio.h> #include <conio.h> void main() { int fib(int); int num; printf("\nEnter num for fibonacci sequence: "); scanf("%d",&num); printf("\nFibonacci %d = %d",n,fib(num)); getch(); int fib(int n) { if (num==0||num==1) return num; } return (fib(num-1)+fib(num-2));

Output: Enter n for fibonacci sequence: 4 Fibonacci 4 = 3

Problem 3: 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

Solution to the Towers of Hanoi for disks = 3

B Initial

Step 1: A -> C

Continue

Step 2: A -> B

Step 3: C -> B

Continue

Step 4: A -> C

Step 5: B -> A

Continue

Step 6: B -> C

Step 7: A -> C

Algorithm for disks movements Towers of Hanoi


1. 1. If (numDisks = 1)
1.1 Print (Move disk 1 from, from, to destination)

Else
2.1 2.2 2.3 2.4 2.5 Begin Move(numDisks-1, from, intermediate, destination) Print (Move, numDisks, from, from to, destination) Move(numDisks-1,intermediate, destination , from) End

//Tower of Hanoi #include <stdio.h> #include <conio.h> 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(); }Enter number of disks: 3

Programming
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); } }

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) Move(2,A,B,C)

A ->C

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

C ->B

Move(3,A,C,B)

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

B ->A

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

A ->C

Exercise
Lab sheet 1: Recursion

Das könnte Ihnen auch gefallen