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

DSA Recursion

Uploaded by

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

DSA Recursion

Uploaded by

Dibyashree Basu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Recursion

Introduction
• Recursion is the process of repeating items in a self similar way.
• For instance, when the surfaces of two mirrors are exactly parallel with
each other the nested images that occur are a form of infinite
recursion.
• The term has a variety of meanings specific to a variety of disciplines
ranging from linguistics to logic. The most common application of
recursion is in mathematics and computer science.

• Definition: Recursion refers to a method of defining functions in


which the function being defined is applied within its own definition.
Recursion
• A big difference between recursion and
iteration is the way that they end. While a
loop executes the block of code, checking
each time to see if it is at the end of the
sequence, there is no such sequential end
for recursive code.

• A recursive function could go on forever


without a condition to put it out of its
misery.
Recursion
• The power of recursion lies in the possibility of defining an infinite set
of objects by a finite statement.
• In the same manner, an infinite number of computations can be
described by a finite recursive program, even if the program contains
no explicit repetitions.
• Most high-level computer programming languages support recursion
by allowing a function to call itself within the program text.
• Some functional programming languages do not define any looping
constructs but rely on recursion to repeatedly call code.
• Computability theory has proven that these recursive-only languages
can solve the same kinds of problems even without the typical control
structures like “while” “until” and “for”.
Recursion
• An algorithm is recursive if it calls itself to do a part of its work.

• A recursive algorithm must have two parts:


vthe base case, which handles a simple input that can be solved without
resorting to a recursive call;
vthe recursive part which contains one or more recursive calls to the
algorithm.
Applications of Recursion
• Recursion is made for solving problems that can be broken
down into smaller, repetitive problems. It is especially good
for working on things that have many possible branches and are
too complex for an iterative approach.
• One good example of this would be searching through a file
system.

• Other applications: Recursion is used


• to compute the factorial function,
• to determine whether a word is a palindrome,
• to compute powers of a number,
• to draw a type of fractal,
• to solve the ancient Towers of Hanoi problem and many more
Example
• A classic example is the recursive definition for the factorial function:
Example: Factorial
Example: Factorial
Iterative Code for Recursion
Recursion Features
• Depth of recursion is the longest chain of procedure that calculate
function F(n) in recursive way.

• Recursion doesn’t differ from normal procedure when functions are


used.

• It means, that variables are local and are valid just inside the single
procedure.
Example
Example
Example
Example
Trace of the Program
Fibonacci Numbers
• In mathematics, things are often defined recursively. For example, the
Fibonacci numbers are often defined recursively.
• The Fibonacci numbers are defined as the sequence beginning with
two 1's, and where each succeeding number in the sequence is the
sum of the two preceding numbers.
Recursive Code
Recursive Code
Iterative Code
Iterative Code
Summary
• Recursive function always has:
• recursive part (contains one or more recursive calls)
• base case (handles a simple input that can be solved without resorting to a
recursive call)

• For Fibonacci numbers, the base case is when n == 0 and n==1, then
the program returns 1 without any further recursive calls.

• Note: Recursive programs must always have a base case!


Details of Recursive Calculations
• The computer will go through the following process to compute fib(3):
• 3 exceeds 1, so we need to compute and return fib(3 - 1) + fib(3 - 2).

• To compute this, we first need to compute fib(2).


2 exceeds 1, so we need to compute and return fib(2 - 1) + fib(2 - 2).

• To compute this, we first need to compute fib(1).


1 is less than or equal to 1, so we need to return 1.

• Now that we know fib(1), we need to compute fib(0). 0 is less than or equal to 1, so we
need to return 1.
Now we know fib(2 - 1) + fib(2 - 2) = 1 + 1 = 2. We return this.

• Now that we know fib(2) is 2, we need to compute fib(1). 1 is less than or equal to 1, so
I need to return 1.
We now know fib(3 - 1) + fib(3 - 2) = 2 + 1 = 3. We return this.
Tower of Hanoi
• The Towers of Hanoi puzzle begins with three poles and n rings, where all
rings start on the leftmost pole (Pole A).
• The rings each have a different size, and are stacked in order of decreasing
size with the largest ring at the bottom.
• The problem is to move the rings from the leftmost pole to the rightmost
pole (Pole C) in a series of steps.
• At each step the top ring on some pole is moved to another pole.

• There is one limitation on where rings may be moved: a ring can never be
moved on top of a smaller ring.
• “Towers of Hanoi” natural algorithm to solve this problem has multiple
recursive calls. It cannot be rewritten easily using while loops.
Tower of Hanoi Details
• Problem: You are given three posts labelled A, B, and C.
• On Post A there are n rings of different sizes, in the order of the largest ring
on the bottom to the smallest one on top. Posts B and C are empty.
• The object is to move the n rings from Post A to Post B by successively
moving a ring from one post to another post that is empty or has a larger
diameter
• Inductive proof that the puzzle has a solution:
• (basis) One ring can always be moved.
• (inductive step) Assume that we can move n–1 rings from any given post to
any other post. Since any of the rings 1 through n–1 can be placed on top
of ring n, all n rings can be moved as follows:
1. Move n–1 rings from Post A to Post B.
2. Move ring n from Post A to Post C.
3. Move n–1 rings from Post B to Post C.
Tower of Hanoi
Example:
• The solution for 3 disks takes 7 steps:
Recursive algorithm
• The proof by induction is constructive because it tells you how to
solve a problem with n rings in terms of solutions to problems with n–
1 rings; it corresponds directly to a recursive program.
• The procedure TOWER(n,x,y,z) moves n rings from Post x to Post y
(using Post z as a "scratch" post):
Tower of Hanoi
Recursive Code
Advantages & Disadvantages
• Advantages of the recursion
vConvenient way to control sequence of tasks
vSimple code
• Disadvantages of the recursion
vStack overflow may appear
vRecursion can lead to not efficient way of the algorithm

• Recommendation: Avoid the use of recursion if you are not sure


about problem size, use iterative procedure instead.
Practice Questions
1. Write recursive function to determine if an input is prime number.

2. Write recursive functions to assign the particular values to the


array, and printout array in reverse and in normal order.

3. Write recursive function to printout digits of the given number in


reverse order i.e. 2015 – 5 1 0 2

You might also like