Chapter 5. Recursion
Chapter 5. Recursion
The process in which a function calls itself directly or indirectly is called recursion and
the corresponding function is called as recursive function. Recursion is used to solve problems
involving iterations in reverse order. It solves a problem by reducing it to an instance of the
same problem with smaller input. Recursion is an alternative to each iteration in making a
function executes repeatedly.
Properties of Recursion:
A recursive function can go infinite liked a loop, to avoid infinite running of recursive function.
There are two properties that a recursive function must have:
1. Base Criteria: There must be at least one base criteria or conditions such that when this
condition is met the function stops calling itself recursively.
2. Progressive Call: The recursive calls should progress in such a way that each time a
recursion call is made, it comes closer to the base case.
The classic example of recursive programming involves computing factorials. The factorial of
a number is computed as that number times all of the numbers below it up to and including 1.
5!
=5*4!
=5*4*3!
=5*4*3*2!
=5*4*3*2*1!
=5*4*3*2*1*0!
=5*4*3*2*1
=5*4*3*2
=5*4*6
=5*24
Recursion Tree for fact (5) =120
Fact (5)
5 Fact (4)
4 Fact (3)
Fact (2)
3
2 Fact (1)
1 Fact (0)
CHAPTER 5 RECURSION
Difference between Iteration and Recursive function:
Iteration Recursion
It is a process of executing statements Recursion is a technique of defining
repeatedly, until some specific anything in terms of itself
condition is specified
Iteration involves four clear cut steps, There must be an base condition
initialization, condition, execution and inside the recursive function
updating specifying stopping condition
The value of control variable moves The function state converges towards
towards the value in condition the base case
Any recursive problem can be solved Not all problems has recursive
iteratively solution
An iteration does not use the stack so It is usually much slower because all
it's faster than recursion. function calls must be stored in a
stack to allow the return back to the
caller functions.
Iteration consumes less memory. Recursion uses more memory than
iteration.
E.g. E.g.
CHAPTER 5 RECURSION
Recursive Program Using Stack:
Recursive functions use something called “the call stack.” When a program calls a
function, that function goes on top of the call stack.
Stack is used to keep the successive generations of local variables, the parameters and the
returned values. This stack is maintained by the C system and lies inside and invisible to the
users.
Each time that a recursive function is entered, a new allocation of its variables is pushed on top
of the stack. Any reference to a local variables or parameter is through the current top of the
stack. When the function returns, the stack is popped, the top allocation is freed, and the
previous allocation becomes the current stack top to be used for referencing local variables.
Figure below shows a snapshots of the stack as execution of the fact function proceeds.
2
3 3 2
4 4 3 4 3
n x y
ii iii iv
i 0 ii ii
1 0
i
1 0
i
1 i
1 2 i
1 2
i
1 1
2 1
2 1 3 2 3 2
3 2
3 2 4 3 4 3
4 3
4 3
vi vii viii
v
ii ii
ii
ii i
i
i
i i i
i
i
3 2 2
4 3 4 3 6
ix x xi
ii ii
ii
i
i i
i i
i
CHAPTER 5 RECURSION
Recursion Tree:
Iteration
Recursion
Fibo (5)
Fibo (2) Fibo (1) Fibo (1) Fibo (0) Fibo (1) Fibo (0)
A B C
ii ii ii
i
i i
The objective to move all the disks from the peg A to peg C, using peg B as auxiliary. The rules
to be followed are:
Only the top disk on any peg may be moved to any other peg.
Only one disk can be moved among the towers at any given time.
Larger disk may never rest on a smaller one.
Recurrence Relation for TOH:
Base case : H1 =1 (for one disk)
Recursive case : 𝐻𝑛 = 𝐻𝑛−1 + 1 + 𝐻𝑛−1
Hn = 2Hn-1 + 1
= 2(2Hn-2 +1) + 1
= 4Hn-2 +2 + 1
= 4(2Hn-3 +1) + 2 + 1
= 2n-1 + 2n-2 + …+ 4 + 2 + 1 [∵ an = ar𝑛−1 ]
.
.
𝑎(𝑟 𝑛 −1)
=2n-1 [∵ 𝑆𝑛 = , 𝑟 ≠ 1]
(𝑟−1)
CHAPTER 5 RECURSION
Algorithm for TOH:
To move n disks from A to C, using B as auxiliary:
1. Declare and initialize necessary variables.
n = number of disks
A=’A’, B=’B’, C=’C’, for three pegs being used.
2. If n == 1,
Move the single disk from A to C and stop.
3. Otherwise
Move the top n-1 disks from A to B, using C as auxiliary.
Move the remaining disk from A to C.
Move the n-1 disks from B to C, using A as auxiliary.
4. stop
Algorithm for n=3 disks:
1. Move disk 1 from peg A to peg C
2. Move disk 2 from peg A to peg B
3. Move disk 1 from peg C to peg B
4. Move disk 3 from peg A to peg C
5. Move disk 1 from peg B to peg A
6. Move disk 2 from peg B to peg C
7. Move disk 1 from peg A to peg C
TOH (1, A, C, B)
TOH (1, A, B, C)
TOH ( 1, B, C, A)
TOH (3, B, C, A)
TOH (1, C, A, B)
TOH (1, B, A, C )
TOH (2, B, A, C )
TOH (1, B, C, A)
TOH (1, A, C, B)
TOH (4, A, C, B) TOH (1, A, B , C)
TOH (1, C, A, B)
TOH (1, B, C, A)
TOH (3, A, B, C) TOH (1, A, B, C)
TOH (1, A, C, B)
TOH (2, A, C, B)
TOH (1, A, B, C)
CHAPTER 5 RECURSION
Applications of Tower of Hanoi:
Application of Recursion:
The most important data structure ‘Tree’ doesn’t exist without recursion we can solve
that in iterative way also but that will be a very tough task.
The mathematical problem can’t be solved in general, but that can only be solved using
recursion up to a certain extent.
Sorting algorithms (Quick sort, Merge sort, etc.) uses recursion.
All the puzzle games (Chess, Candy crush, etc.) broadly uses recursion.
It is the backbone of searching, which is most important thing.
This is the backbone of AI.