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

Recursion

The document discusses recursion, which is a method where a function calls itself within its own definition. It provides examples of recursive functions including factorial, Fibonacci series, and Tower of Hanoi. It explains direct and indirect recursion and covers finding recursive solutions, recursion vs iteration, and run time stack tracing for recursive functions.

Uploaded by

HASEEB AFZAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Recursion

The document discusses recursion, which is a method where a function calls itself within its own definition. It provides examples of recursive functions including factorial, Fibonacci series, and Tower of Hanoi. It explains direct and indirect recursion and covers finding recursive solutions, recursion vs iteration, and run time stack tracing for recursive functions.

Uploaded by

HASEEB AFZAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

DATA STRUCTURES &

ALGORITHMS
Recursion

Instructor: Engr. Laraib Siddiqui


Recurrence

2
Basic objective
▪ When we have a bigger problem whose solution is complex.

▪ We decompose the problem into smaller units until we reach to the smallest sub-
problem (base case) for which a solution is known.

▪ Then go back in reverse order and build upon the solutions of the sub-problems.

3
Recursion
A well defined mathematical function in which the function being defined is applied
within its own definition.

4
Recursive Functions
▪ Function may call itself

▪ Function may call other Function and

▪ the other function in turn again may call the calling Function

5
Direct & indirect Recursion
Recursion is said to be direct when functions calls itself directly and is said to be
indirect when it calls other function that in turn calls it

6
Other Types
Linear recursion : makes at most one recursive call each time it is invoked.

Binary recursion : makes two recursive calls.

Multiple recursion : method may make (potentially more than two) recursive calls.

7
Finding a recursive solution
▪ Each successive recursive call should bring you closer to a situation in which the
answer is known

▪ A case for which the answer is known (and can be expressed without recursion) is
called a base case

▪ Each recursive algorithm must have at least one base case, as well as the general
recursive case

8
Recursion vs. Iteration
The factorial of a positive integer
For example, 4! = 4·3·2·1 = 24.
Iterative Solution

Recursive Solution

9
Example
Factorial of n

10
Example - Factorial
fact(3) 3 > 1 , fact call itself
main()

3
6 fact(n) fact(2) 2 > 1 , fact call itself
fact() multiplies 3 with 2, received 3x2
from lower fact() and returns the
product to upper fact() 2 n=1, returns 1
2 fact(n) fact(1)
2x1

1
fact() multiplies 2 with 1, received from lower fact()
1 fact(n)
and returns the product to upper fact()
1
11
Example Fibonacci Series
fib(n)
5
if(n<2)
fib(5) return n
else
3 2 return ( fib(n-1) + fib(n-2) )
n is 1 and 1 it will return n , fib(4) + fib(3)
after adding 1+1 it will return 2
to the upper function call and 2 1 1 1
so on.. fib(3) + fib(2) fib(2) + fib(1)
1 1 1 0 1 0
fib(2) + fib(1) fib(1) + fib(0) fib(1) + fib(0)
1 0
n is 1 and 0 it will return n ,
fib(1) + fib(0) after adding 1+0 it will return 1
to the upper function call
12
Example Tower of Hanoi
Objective
To transfer entire tower to one of the other pegs.
Rules
Only one disk may e moved at a time.
Lager disks can not be placed on the smaller disk.

13
Example Tower of Hanoi
Step 1: Move top(N-1) disks from Beg to Aux peg.
Step 2: Move 1 disk from Beg to End peg.
Step 3: Move top(N-1) disks from Aux to End peg.

▪ T(N-1, Beg, End, Aux)


▪ T(1, Beg, Aux, End)
▪ T(N-1, Aux, Beg, End)

14
void TOH(int n,int A, int B, int C)
{
if(n>0)
Example Tower of Hanoi TOH(n-1, A,C,B);
printf(“Move a Disc from %d to %d”, A,C);
TOH(n-1, B, A, C);
TOH(3,1,2,3) }

TOH(2,1,3,2) 1-3 TOH(2,2,1,3)

TOH(1,1,2,3) 1-2 TOH(1,3,1,2) TOH(1,2,3,1) 2-3 TOH(1,1,2,3)

1-3 3-2 2-1 1-3

1-3 1-2 3-2 1-3 2-1 2-3 1-3


15
Example Tower of Hanoi
1-3 1-2 3-2 1-3 2-1 2-3 1-3

1-3 1-2

16
Example Tower of Hanoi
1-3 1-2 3-2 1-3 2-1 2-3 1-3

3-2 1-3

2-1 2-3
17
Example Tower of Hanoi
1-3 1-2 3-2 1-3 2-1 2-3 1-3

1-3

18
Run time stack tracing
x=3; push copy of f
Consider the function y= 2* f(2)
x=2; push copy of f
y= 2* f(1)
int f(int x) { x=1; push copy of f
y= 2* f(0)
int y; push copy of f

if(x==0) x=0;
y=?
return 1; Return 1;
else { pop copy of f
y = 2 * f(x-1); y= 2 *1 =2
return y+1= 3 pop copy of f
return y + 1;
y= 2 *3 =6
} return y+1= 7 pop copy of f
} y= 2 *7 =14
return y+1= 15 pop copy of f
19

You might also like