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

Introduction To Data Structure - Recursion

The document discusses recursion, including definitions of recursion, types of recursion (direct, indirect, linear, binary, non-linear, tail), advantages and disadvantages of recursion compared to iteration, and differences between recursion and iteration. Recursion is defined as a process where a function calls itself, and must have a base case and reduce the problem size with each call.

Uploaded by

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

Introduction To Data Structure - Recursion

The document discusses recursion, including definitions of recursion, types of recursion (direct, indirect, linear, binary, non-linear, tail), advantages and disadvantages of recursion compared to iteration, and differences between recursion and iteration. Recursion is defined as a process where a function calls itself, and must have a base case and reduce the problem size with each call.

Uploaded by

Shourya Rohilla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CS 281

Introduction to Data Structure


Dr. Anand Kumar Mishra

Dr. Anand Kumar Mishra


Recursion
“Intelligence is the ability to avoid doing work, yet getting the work done” -Linus
Torvalds

Dr. Anand Kumar Mishra


Recursion
• There are two approaches for writing repetitive algorithms
• One uses iteration/loop and
• the other uses recursion
• Iteration is one of the categories of control structures
• It allows for the processing of some action many times. Iteration is also
known as looping and repetition
• The math term "to iterate" means to perform the statement parts of the loop
• Many problems/tasks require the use of repetitive algorithms.

Dr. Anand Kumar Mishra


Recursion
• Recursion is defined as defining anything in terms of itself
• With most programming languages, this can be done with either:
• i) Looping control structures, specifically the for loop (an iterative approach)
• ii) The recursive calling of a function
• Both approaches provide repetition, and either can be converted to the
other's approach
• Definition:
Recursion is a repetitive process in which a function calls itself either directly or
indirectly.
• Recursion is a technique that allows us to break down a problem into
one or more sub-problems that are similar in form to the original
problem.
Dr. Anand Kumar Mishra
Recursion
• In recursion, each time when the recursive function is called, the
current state including all the local variables, formal parameters, and
return address are pushed into the stack
• A block of memory of contiguous locations set aside for this purpose.
• When the return statement is encountered, the control comes back
to the previous function call, by restoring the state that resides on the
stack.

Dr. Anand Kumar Mishra


Recursion Essentials
• There are two key requirements to make sure that the recursion is
successful:
• A recursion definition must always have certain criteria, called base criteria or
base case for which the function stop calling itself
• Every recursive call must simplify the computation in some way, which is
known as an inductive clause
• In other words, each time a function does call itself (directly or indirectly), it must be
closer to the base criteria that terminates the recursion.

Dr. Anand Kumar Mishra


Infinite Regress
• When the recursion does not terminate, we have an infinite regress.
• Infinite regress often occurs when:
• i) A base criterion is omitted or
• ii) When a base criterion is never reached
• A circular definition may have no base criteria and define the value of
a function in terms of that value itself, rather than on other values of
the function
• Such a situation would lead to an infinite regress
• Note: If infinite recursion occurs in a program then you may get a
runtime error message, e.g. “Stack overflow”.
Dr. Anand Kumar Mishra
Depth of Recursion
• Suppose P is a recursive function. During the execution of a program,
which contains P a level number is assigned
• The original execution of P is assigned level 1 and each time P is
executed because of the recursive call, its level is one more than the
level of execution that had made the recursive call
• The depth of the recursive function P, with given set of arguments,
refers to the maximum level number of P during its execution

Dr. Anand Kumar Mishra


Recursion Tree
• A recursion tree is a tree that is generated by tracing the execution of
a recursive function
• A recursion tree shows the relationship between calls to the function
• Recursion tree is a pictorial representation of recursion call, which is
in the forms of a tree, where at each level nodes are expanded
• Each node represents how recursive function calls are generated
• Descendants of a function call are further recursive calls
• Calls in the tree with no descendants involve evaluation of the base
case(s).

Dr. Anand Kumar Mishra


Dr. Anand Kumar Mishra
Types of Recursion
• Whether the function calls itself or not (direct or indirect/mutual
recursion).
• How many internal recursive calls are made there within the body
(linear, binary, and non-linear recursion)?
• Whether there are pending operations or not at each recursive call
(tail or non-tail recursion)
• Recursion is mainly two types depending on whether a function calls
itself or not.
• i) Direct Recursion
• ii) Indirect Recursion or Mutual Recursion
Dr. Anand Kumar Mishra
Direct Recursion
• In direct recursion, a function calls itself from the body of the
function. [Examples: Factorial, Fibonacci, GCD, etc.]
• A function is directly recursive if it contains an explicit call to itself.
• The format of direct recursion is as follows:

Dr. Anand Kumar Mishra


Indirect Recursion
• In indirect recursion, two functions call one another directly
• A function is indirectly recursive if it contains a call to another
function, which ultimately calls it
• Indirect recursion also is known as mutual recursion. The format of
indirect recursion is as follows:
• Examples:
• Recursive Descent Compilation, Hilbert Curves, etc.

Dr. Anand Kumar Mishra


Linear Recursion
• In linear recursion, a single recursion call is performed
• A recursive function in which only one internal recursive call is made
within the body is called linearly recursive. The format of linear
recursion is as follows:
• Examples:
• Factorial, GCD, Binary Search, etc.

Dr. Anand Kumar Mishra


Binary Recursion
• In binary recursion, two recursion calls are performed. A function that
makes two internal calls to itself is said to be binary recursive. The
format of binary recursion is as follows:

Dr. Anand Kumar Mishra


Binary Recursion
• Examples: Fibonacci sequence, Quick sort, Merge sort, Binary Tree
algorithms, General Divide–and– conquer algorithms.

Dr. Anand Kumar Mishra


Non-linear recursion
• In non-linear recursion, more than two recursion call are performed.
A function using a number of more than two internal recursive calls
within the body of the function is non-linear recursive
• Nonlinear recursion also is known as multiple recursion. The format
of non-linear recursion is as follows:

Dr. Anand Kumar Mishra


Non-linear recursion
• Examples: Non-attacking 8-queens, Sample Generation, Combination
generation, Permutation generation, etc.

Dr. Anand Kumar Mishra


Tail Recursion
• A special form of recursion where the last operation of a function is a
recursive call
• That is a recursive function is said to be tail recursive if there are no
pending operations to be performed on return from a recursive call.

Dr. Anand Kumar Mishra


The factorial
function can be
written in a tail-
recursive way:

Here fact_aux is a recursive function, not fact.


Dr. Anand Kumar Mishra
Advantages of Recursion
• Recursion functions can be written easily. We can write a simple
version of programs using recursion
• There are some complex problems such as
• Tower of Hanoi, Non-attacking Queen, etc. can be easily understood and
implements using recursion
• The recursive definitions can be translated into recursive function
easily.

Dr. Anand Kumar Mishra


Disadvantages of Recursion
• Recursion consumes more storage space because each time a new
recursive call is made; a new memory space is allocated to each
automatic variable used by the recursive function
• The computer may run out of memory if base criteria are not checked
• Recursion is not efficient in terms of speed and execution time, as
function call require storing the current state of the function onto the
stack, jump to execute recursion call, restoring state from the stack.

Dr. Anand Kumar Mishra


Differences between iteration and recursion

Iteration Recursion
• Iteration is a process of • Recursion is a technique that
executing a block of statements breaks down a problem into one
repeatedly until some specific or more subproblems that are
condition similar to the original problem
• The iterative process is more • Recursion is not efficient in
efficient in terms of storage terms of storage space and
space and execution time. execution time.
• Any recursive problem can be • Not all problems have a
solved iteratively. recursive solution

Dr. Anand Kumar Mishra


Differences between iteration and recursion

Iteration Recursion
• Iteration process sometimes not • Recursive functions are easier to
easy to implement. Complicated implement and maintain.
problems are difficult to solve in Complicated problems are
iteratively; e.g. tower of Hanoi solved easily.
problem, eight queen problem • A recursive function must have
etc. base criteria for which the
• Iteration has four steps: function does not call itself. Each
initialization, condition checking, time a function does call itself
execution statements and (directly or indirectly); it must be
updating. closer to the base criteria.
Dr. Anand Kumar Mishra
When should we use iteration, and when use
recursion?
• There are three factors to consider:
1. Iterative functions are typically faster than their recursive alternatives.
Therefore, if speed were an issue, you would normally use iteration
2. If the stack limit is too constraining then you will prefer iteration over
recursion
3. Some procedures are very naturally programmed recursively, and all but
unmanageable iteratively. Here, the choice is clear.

Dr. Anand Kumar Mishra


Factorial
• Factorial of a number n is the product of the positive integer from 1
to n.

Dr. Anand Kumar Mishra


Dr. Anand Kumar Mishra
• The above function obtains the factorial of a given number n in a recursive manner. If the number is one then factorial is
computed otherwise for one the function returns 1
• The above function fact is a recursive function because it calls itself; namely, factorial (n) is calling fact (n -1).

Dr. Anand Kumar Mishra


Therefore, if the function factorial is called by a value say 4, the function
call factorial (4) invokes itself with the value 3. The function call factorial (3)
invokes factorial (2), the factorial (2) invokes factorial (1). Then we have no
further function call because it has reached the base condition.

Therefore, in Stack first fact (3) is pushed, then fact (2) and at last, the fact
(1) is pushed. Moreover, these function calls are processed according to
the sequence they are popped from the stack.

Dr. Anand Kumar Mishra


Fibonacci sequence
• Fibonacci sequence is a series of positive number in a manner that
the next term of the series is the addition of two previous terms: 0 1
1 2 3 5 8 13 21 34….

Dr. Anand Kumar Mishra


The recursion tree for obtaining the Fibonacci number of a given number 5. Finally, the
function adding all values of the leaf nodes. FIB (5) = FIB (2) + FIB (2) + FIB (1) + FIB (2) +
FIB (1) = 1 + 1 + 0 + 1 + 0 = 3 Therefore, the function calculated 5th term of the Fibonacci
series, which is 3.
Dr. Anand Kumar Mishra

You might also like