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

Chapter Three Recursion

Uploaded by

matias bahiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Chapter Three Recursion

Uploaded by

matias bahiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter Three Data Structures and Algorithms

3.1. Recursion

Many operations on data structures – especially those on nonlinear linked structures such as
trees and graphs- are easier to implement using the powerful technique of recursion. A recursive
function is one that calls itself. This powerful technique produces repetition without using loops
(e.g., while loops or for loops). Thus it can produce substantial results from very little code.
Recursion allows elegantly simple solutions to difficult problems. But it can also be misused,
producing inefficient code. Recursive code is usually produced from recursive algorithms.

3.1.1. Recursive Function

A recursive function is one that calls itself. This causes automatic repetition, a virtual
loop. In fact most algorithms that use iteration can be recast, replacing each loop with a recursive
call. So recursion can be viewed as an alternative to iteration.

Recursive functions are typically more concise than their iterative alternatives. For
example consider the factorial function.

The factorial function is defined mathematically by:

n !=
{¿ n1( n−1
, if n=0∨1
) ! , if n>1

The function n! “recurs” on the right side of the equation

n !=n ( n−1 ) !

As the expression (n-1)!.

The first 10 values of the factorial function are shown in Table shown on the right. The
first and second value, 0! And 1!, are defined by the upper half of the definition: 0! = 1 (for n = 0
or 1). All the rest of the values are defined by the lower half of the definition:

In order to use recursion for problem solving, one has to first understand to give solution
to a smaller but similar problem. This means a solution for (n-1)! Must be known to give a
solution for n!, a solution to (n-1-1)! must be known before answering (n-1)! and so on. While
one goes to the bottom recurring, he/she will eventually reach to the number (n-(n-1)! Which is
1!. This factorial has a known solution in the factorial definition above, which is 1. This value
will be returned to the calling function which has passed a little bigger problem which is (n-(n-
2)! Which is 2! = 2 * 1! = 2. This value again will be returned to the a little bigger problem
which is 3! and so on. Look at the following diagram done for 3!.

This problem can be viewed as climbing down a ladder and picking up the value (i.e. 1! =
1, in this case) and climb back up the ladder. The known value or in this case 0! = 1! = 1, is
called the base of the recursion. The other part which states n! = n(n-1)! is called the recursion.

1|Page
Chapter Three Data Structures and Algorithms

n !=1if n=0∨1 [ BASE ]

n !=n ( n−1 ) [RECURSION ]

Testing the Recursive Factorial Function

1. #include<iostream.h>
2. long factorial(int n){
3. if(n<2) return 1;
4. return n*factorial(n-1);
5. }
6. void main(){
7. for(int i = 0; i < 10; i ++)
8. cout<<"\nfactorial("<<i<< ") = " <<factorial(i);
9. }

Output

factorial(0) = 1 factorial(5) = 120


factorial(1) = 1 factorial(6) = 720
factorial(2) = 2 factorial(7) = 5040
factorial(3) = 6 factorial(8) = 40320
factorial(4) = 24 factorial(9) = 362880

3.1.2. The Towers of Hanoi

The “towers of Hanoi” is a puzzle that was popular near the end of the 19 th century. The
puzzle consists of a board with three vertical pegs and a progression of desks of increasing
diameter. Each disk has a hole in its center. The disks are stacked on one peg so that each disk
rests on a larger one. The puzzle is how to transfer all the disks from one peg to another by
means of a sequence of individual moves, subjected to the restriction that no disk may be placed
upon a smaller disk.

The puzzle is defined in terms of n disks, where n is a positive integer. To describe a


solution, we label the three pegs A, B and C, and number the disks from smallest to largest 1,2,
…n.

In the simplest, nontrivial case, with n = 3 disks, it is easy to see the solution to the puzzle
is the following sequence of seven moves.

1. A  B 3. B  C
2. A  C

2|Page
Chapter Three Data Structures and Algorithms

4. A  B 6. C  A
5. C  A 7. A  B

A recursive solution for n disks can be formulated by generalizing the steps involved to solve
a big solution. This means in order to solve a 4 disk Tower of Hanoi problem, we must first
move the three disks from A to C then, move disk 4 from A to B then move the three disks from
C to B. in a more generalized form, to move n disks from A to B, move all but the last disk from
A to C; then move the last disk from A to B; then move all the other disks from C to B. A
recursive test run to solve tower of Hanoi of n disks outlines as follows.

1. #include<iostream.h>
2. void TowerOfHanoi(int n, char a, char b, char c){
3. if(n == 1) cout<<"\n"<<a<<" -> "<<b; // base
4. else{
5. TowerOfHanoi(n-1, a, c, b);
6. cout<<"\n"<<a<<" -> "<< b;
7. TowerOfHanoi(n-1, c, b, a);
8. }
9. }
10. void main(){
11. int n;
12. cout<<"enter the number of hanois";
13. cin>>n;
14. TowerOfHanoi(n, 'A', 'B', 'C');
15. }

The output of the above program varies with the input n, although it is similar with the solution
given for n = 3, indicating the moves of the top disks from one peg to another.

3.1.3. Fibonacci Numbers

Fibonacci numbers are useful in quite a few diverse areas of computing. They were
discovered by Leonardo Pisano (1170 – 1250),. He is best remembered for having written Liber
Abaci, a textbook on arithmetic which introduced the Hindu-Arabic numerals to Europe. One of
the exercises in this book was the famous rabbit problem.

A certain man put a pair of rabbits in a place surrounded on all sides by a wall. How
many pairs of rabbits can be produced from the pair in a year if it is supposed that
ever4y month each pair begets a new pair which from the second month on becomes
productive

3|Page
Chapter Three Data Structures and Algorithms

The solution is illustrated on the figure on the A


J
J F M p M
right. It assumes that the original new born pair of a e a r a
u
n
rabbits was put in the walled place during January and n b r i y
e
l
that it becomes productive during February. So that
first new pair is born during March. That new pair
then becomes productive during April, giving birth to
its first new pair during May. Meanwhile, the original
pair continues to produce another new pair during
each month.

So there are 0 pairs on January 1, one pair on


February 1, one pair on March 1, two pairs on April 1,
three pairs on May 1, five pairs on June 1, and there
will be eight pairs on July 1. These are Fibonacci
numbers:

F 0=0 , F1 =1, F 2=1 , F 3=2 , F 4=3 , F5=5 , F 6=8 , etc

The preceding numbers follow the Fibonacci recurrence equation:

F n=F n−1+ F n−2

because the number F n of pairs of rabbits after n months is precisely the number of F n−1of pairs that
were alive at the end of the previous month (none die) plus the number F n−2 that were alive at the end of
the month before that (the productive ones).

Now the equation in the problem was, how many pairs are produced on a year? The number of
pairs produced from the original pair would be the total number after 12 months or simply F 12.

F 12=F 11 + F 10=( 55+89 )=144

Testing the Recursive Fibonacci Function

1. #include<iostream.h>
2. int fib(int n){
3. if(n == 0) return 0;
4. if(n == 1) return 1;
5. else return fib(n-1) + fib(n-2);

4|Page
Chapter Three Data Structures and Algorithms

6. }
7. void main(){
8. for(int i = 0; i < 13; i ++)
9. cout<<"\nthe "<<i<<" --> "<<fib(i);
10. }

Complexity analysis for recursion

Let us look at the Fibonacci series example shown above. In order to find nth Fibonacci number
we must go through two recursive calls fn-1 and fn-2. The following calculation shows the complexity
analysis of a Fibonacci series recursion function

T(0) = 0 ......base case


T(n) = 2T(n-1) + 1

T(n) = 2T(n-1) + 1
= 2(2T(n-2) + 1) + 1
= 4(2T(n-3) + 1) + 1 + 2
:
:
= 2k T(n-k) + ∑ 2i
= 2k T(n-k) + 2k-1
= 2k T(n-k) + 2k - 1 using rn -1 / r -1

n-k = 0
n=k
T(n) = 2n T(n-n) + 2n - 1
= 2n T(0) + 2n - 1
= 0 + 2n - 1
= 2n - 1

O(2n) .....Time Complexity

5|Page

You might also like