DSA LAte PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

GOVT.

POST GRADUATE COLLEGE


OF SCIENCE

Name Aqsa Tabassum


Roll # 2212
To Respected Sir Rizwan
Assignment
DSA. Recursion
zTHE FACT:
Each recursive definition requires
base cases in order to prevent
infinite recursion. RECURSION

A classic example of a
function that is implemented Recursion is a very important concept in
recursively is the factorial functional programming. The fundamental part
function, which finds the product of recursion is self-reference - functions calling
of all positive integers below a themselves.
specified number. For example,
5! (5 factorial) is 5 * 4 * 3 * 2 * 1 It is used to solve problems that can be
(120). To implement this broken up into easier sub-problems of the same
recursively, notice that 5! = 5 * 4! type. The method we write to solve a specific
_ 4! = 4 * 3! _ 3! = 3 * 2! and so problem in that way is known as recursive
on. Generally, n! = n * (n-1)! programing.
Furthermore, 1! = 1. This is
known as the base case, as it can For example, the problem of adding (or
be calculated without multiplying) n consecutive integers can be
performing any more factorials. reduced to a problem of adding (or multiplying)
Below is a recursive n-1consecutive integers:
implementation of the factorial 1 + 2 + 3 +... + n = n + [1 + 2 + 3 + ... + (n-1)]
function.
1 * 2 * 3 *... * n = n * [1 * 2 * 3 * ... * (n-1)]
These statements in recursive programing
can be written as,

Sum R(n) = n + um R(n-1)

Times R(n) = n * times R(n-1)

***************
CONDITIONS:
TYPES OF
All recursive algorithms must
obey three important laws: RECURSION
1. A recursive algorithm A recursive function is a function that calls itself,
must have a base case. meaning it uses its own previous terms in calculating
2. A recursive algorithm subsequent terms. This is the technical definition. Here is a
must change its state and real world math problem,
move toward the base
case.
3. A recursive algorithm
must call itself,
recursively.

In computing, recursion
termination is when certain Two types are common For Recursive
conditions are met, and a Function:
recursive algorithm stops calling
itself and begins to return values. A function is recursive if it makes a call
This happens only if, with every to itself directly or indirectly. If a function f ()
recursive call, the recursive calls itself within from its own body, it is called
algorithm changes its state and recursive and called DIRECT recursion.
moves toward the base case.
One can view this mathematically in a directed
call graph.

A ---|
^ |
| |
|-----|

void A () {
A ();
return;
}

A () is a recursive function since it directly calls


itself.
Secondly, if a function f () calls another
function g () that ultimately calls back to
f (), then it can also be considered a recursive
function and IDIRECT recursion.

A ---------> B
^ |
| |
| |
|---- C <----|

This can be viewed in the following three


functions:

void C () {
A ();
return;
}

void B () {
C ();
return;
}

void A () {
B ();
return;
}

Following variants of recursion tell us


making recursive calls in different ways
depending upon the problem.

Linear Recursion
Tail recursion
Binary Recursion
Multiple REcursion
APPLICATIONS

In Games:
Towers of Hanoi, recursive listing of
directories or folders and Russian Matryoshka dolls are
some of them.

In math:
The most common application of recursion
is in mathematics and computer science, where a function
being defined is applied within its own definition.

In programming:
It is used to solve problems that can be
broken up into easier sub-problems of the same type.

***************
IMPLIMENTATION
Recursive Binary Search
output
Recursive Factorial
output

Recursion Using Stack

You might also like