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

Recursion

Recursion CSE 105

Uploaded by

tahsif.cse
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Recursion

Recursion CSE 105

Uploaded by

tahsif.cse
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

RECURSION

1
Repetitive Algorithm

Two approaches to writing repetitive algorithms


• Iteration
• Recursion
A recursive algorithm is one that invokes (makes reference to)
itself repeatedly until a certain condition (also known as
termination condition) matches
Usually recursion is organized in such a way that a subroutine
calls itself or a function calls itself
. Iterative algorithms use repetitive constructs like loops and
sometimes additional data structures like stacks to solve the given
problems.

2
Recursion Example
 While using recursion, programmers need to be careful to define an
exit condition from the function, otherwise it will go into an infinite
loop.
 Recursive functions are very useful to solve many mathematical
problems, such as calculating the factorial of a number, generating
Fibonacci series, etc.

3
Disadvantages
• Greatest space requirement as all function
will remain in stack
• Greatest time requirement as function
calls itself and return overhead

4
Advantages
• Provides a clean and simple way to write
code.
• Some problems are inherently recursive
like tree traversal , quick sort, merge sort
e.t.c

5
Recursion VS Iteration
• An Iterative algorithm will be faster than the Recursive algorithm
because of overheads like calling functions and registering stacks
repeatedly.
• Recursive algorithm uses a branching structure, while iterative
algorithm uses a looping construct.
• Many times the recursive algorithms are not efficient as they take
more space and time.
• Recursive algorithms are mostly used to solve complicated
problems when their application is easy and effective.
• recursion is like a selection structure, and which makes code
smaller and clean but in iteration code may be longer but it is faster
than recursive.

6
Factorial – A case study

• The factorial of a positive number is the product of the integral


values from 1 to the number:

n!= n*(n-1)*(n-2)……….3* 2* 1
4!= 4.3.2.1= 24
• Iterative Factorial Algorithm definition:

7
Factorial: Recursive Algorithm

•Recursive Factorial Algorithm definition:

8
Factorial (3): Decomposition and solution
•The recursive solution for a problem involves a two-way journey:

First we decompose the problem from the top to the bottom


Then we solve the problem from the bottom to the top

9
10
11
Designing recursive algorithms

• The rules for designing a recursive algorithm:

1. First, determine the base case.


2. Then determine the general case.
3. Combine the base case and the general cases into an
algorithm

12
Example-Problem GCD

• Determine the greatest common divisor (GCD) for two numbers.


• Euclidean algorithm: GCD (a,b) can be recursively found from the
formula

 a if b=0

GCD a, b   b if a =0
GCD b, a mod b  otherwise

13
Pseudocode Implementation

14
Example-Problem Fibonacci series

• Generation of the Fibonacci numbers series.


• Each next number is equal to the sum of the previous two numbers.
• A classical Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, …
• The series of n numbers can be generated using a recursive formula

 0 if n=0

Fibonacci n   1 if n=1
 Fibonacci n  1  Fibonacci n  2  otherwise

15
Try Yourself
• FUN1(5, 2)

16
Try Yourself
• Write a function that will print binary
equivalent of n
• For example if n is 21 then binary
equivalent is 10101

17
solution
• int convert(int dec)
• {
• if (dec == 0)
• {
• return 0;
• }
• else
• {
• return (dec % 2 + 10 * convert(dec / 2));
• }
• }

18
Fibonacci

19

You might also like