SlideShare a Scribd company logo
Recursion
Data Structure
Submitted By:-
Dheeraj Kataria
A more complicated case of recursion is found in definitions in which a
function is not only defined in terms of itself but it is also used as one
of the parameters.
Example:









4if))2(2(
,4if
,0if0
)(
nnhh
nn
n
nh
h(1)=h(2+h(2))=h(14)=14
h(2)=h(2+h(4))=h(12)=12
h(3)=h(2+h(6))=h(2+6)=h(8)=8
h(4)=h(2+h(8))=h(2+8)=h(10)=10
Recursion
• Nested Recursion
The Ackermann function









otherwise))1,(,1(
,0,0if)1,1(
,0if1
),(
mnAnA
mnnA
nm
mnA
This function is interesting because of its remarkably rapid growth. It
grows so fast that it is guaranteed not to have a representation by a
formula that uses arithmetical operations such as addition,
multiplication, and exponentiation.
Recursion
• Nested Recursion
The Ackermann function









otherwise))1,(,1(
,0,0if)1,1(
,0if1
),(
mnAnA
mnnA
nm
mnA
A(0,0)=0+1=1,A(0,1)=2,A(1,0)=A(0,0)=1,
A(1,1)=A(0,A(1,0))=A(0,1)=3
A(1,2)=A(0,A(1,1))=A(0,2)=4
A(2,1)=A(1,A(2,0))=A(1,A(1,0))=A(1,1)=5
A(3,m)=A(2,A(3,m-1))=A(2,A(2,A(3,m-2)))=…=2m+3-3
32),4(
162...
2
mA
Recursion
• Nested Recursion
Logical simplicity and readability are used as an argument
supporting the use of recursion. The price for using recursion is slowing
down execution time and storing on the run-time stack more things
than required in a non-recursive approach.
Example: The Fibonacci numbers









.2if)1()2(
,2if1
,1if1
)(
iiFiF
i
i
iF
void Fibonacci(int n)
{
If (n<2) return 1;
else
return Fibonacci(n-1)+Fibonacci(n-2);
}
Recursion
• Excessive Recursion
Many repeated computations
Recursion
• Excessive Recursion
Recursion
• Excessive Recursion
void IterativeFib(int n)
{
if (n < 2)
return n;
else
{
int i = 2, tmp, current = 1, last = 0;
for ( ; i<=n; ++i)
{
tmp= current;
current += last;
last = tmp;
}
return current;
}
}
Recursion
• Excessive Recursion
Recursion
• Excessive Recursion
We can also solve this problem by using a formula discovered by
A. De Moivre.
The characteristic formula is :-
5
)
2
51
()
2
51
(
)(
nn
nf




Can be neglected
when n is large
Recursion
• Excessive Recursion
The value of is approximately -0.618034)
2
51
(

 Suppose you have to make a series of decisions,
among various choices, where
• You don’t have enough information to know what to
choose
• Each decision leads to a new set of choices
• Some sequence of choices (possibly more than
one) may be a solution to your problem
 Backtracking is a methodical way of trying out various
sequences of decisions, until you find one that “works”
Recursion
• Backtracking
Backtracking allows us to systematically try all
available avenues from a certain point after
some of them lead to nowhere. Using
backtracking, we can always return to a
position which offers other possibilities for
successfully solving the problem.
Recursion
• Backtracking
Recursion
• Backtracking
 Place 8 queens on an 8 by 8 chess board so
that no two of them are on the same row,
column, or diagonal
The Eight Queens Problem
Recursion
• Backtracking
The Eight Queens Problem
The Eight Queens Problem
Pseudo code of the backtracking algorithm
PutQueen(row)
for every position col on the same row
if position col is available
{
place the next queen in position col;
if (row < 8)
PutQueen(row+1);
else success;
remove the queen from position col; /* backtrack */
}
Recursion
• Backtracking
The Eight Queens Problem
Natural Implementation
1
Initialization
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Q
The first queen
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
0
1
0
1
1
1
1
1
0
1
1
0
1
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
1
0
1
1
0
1
1
1
1
1
0
1
0
1
1
1
1
1
1
0
Q
The second queen
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
0
Q
0
0
0
0
0
0
0
0
0
0
1
1
1
1
0
0
1
0
0
1
1
1
0
0
1
1
0
0
1
1
0
0
1
1
1
0
0
1
0
0
1
1
1
1
0
0
Recursion
• Backtracking
The Eight Queens Problem
Natural Implementation
Q
The third queen
0
0
0
0
0
0
0
0
0
0
1
1
0
1
1
0
Q
0
0
0
0
0
0
0
0
0
0
1
1
1
1
0
0
Q
0
0
0
0
0
0
0
0
1
0
0
1
1
0
0
0
1
1
0
0
1
0
0
0
1
1
1
0
0
Q
The fourth queen
0
0
0
0
0
0
0
0
0
0
Q
0
0
0
0
0
Q
0
0
0
0
0
0
0
0
0
0
1
0
1
1
0
0
Q
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
1
0
0
Q
The 5th & 6th queen
0
0
0
0
0
0
0
0
0
0
Q
0
0
0
0
0
Q
0
0
0
0
0
0
0
0
0
0
Q
0
0
0
0
0
Q
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Q
0
0
Have to backtrack now!This would be queen now.
Recursion
• Backtracking
The Eight Queens Problem
Natural Implementation
The setting and resetting part would be the most time-consuming
part of this implementation.
However, if we focus solely on the queens, we can consider the
chessboard from their perspective. For the queens, the board is not
divided into squares, but into rows, columns, and diagonals.
Recursion
• Backtracking
The Eight Queens Problem
Simplified data structure
A 4 by 4 chessboard Row-column = constant
for each diagonal
Recursion
• Backtracking
The Eight Queens Problem
Recursion
• Backtracking
The Eight Queens Problem
Recursion
• Backtracking
The Eight Queens Problem
Recursion
• Backtracking
Recursion
• Backtracking
The Eight Queens Problem
Recursion
• Backtracking
The Eight Queens Problem
• Usually recursive algorithms have less code,
therefore algorithms can be easier to write and
understand - e.g. Towers of Hanoi. However,
avoid using excessively recursive algorithms even
if the code is simple.
• Sometimes recursion provides a much simpler
solution. Obtaining the same result using iteration
requires complicated coding - e.g. Quicksort,
Towers of Hanoi, etc.
Why Recursion?
Why Recursion?
• Recursive methods provide a very natural
mechanism for processing recursive data
structures. A recursive data structure is a
data structure that is defined recursively –
e.g. Linked-list, Tree.
 Functional programming languages such as
Clean, FP, Haskell, Miranda, and SML do
not have explicit loop constructs. In these
languages looping is achieved by recursion.
• Recursion is a powerful problem-solving
technique that often produces very clean
solutions to even the most complex
problems.
• Recursive solutions can be easier to
understand and to describe than iterative
solutions.
Why Recursion?
• By using recursion, you can often write
simple, short implementations of your
solution.
• However, just because an algorithm can
be implemented in a recursive manner
doesn’t mean that it should be
implemented in a recursive manner.
Why Recursion?
Limitations of
Recursion
• Recursive solutions may involve extensive
overhead because they use calls.
• When a call is made, it takes time to build a
stackframe and push it onto the system stack.
• Conversely, when a return is executed, the
stackframe must be popped from the stack
and the local variables reset to their previous
values – this also takes time.
Limitations of
Recursion
• In general, recursive algorithms run slower
than their iterative counterparts.
• Also, every time we make a call, we must
use some of the memory resources to
make room for the stackframe.
Limitations of
Recursion
• Therefore, if the recursion is deep, say,
factorial(1000), we may run out of memory.
• Because of this, it is usually best to
develop iterative algorithms when we are
working with large numbers.
Main disadvantage of
programming recursively
• The main disadvantage of programming
recursively is that, while it makes it easier
to write simple and elegant programs, it
also makes it easier to write inefficient
ones.
• when we use recursion to solve problems we are
interested exclusively with correctness, and not
at all with efficiency. Consequently, our simple,
elegant recursive algorithms may be inherently
inefficient.
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA

More Related Content

What's hot

The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
Sukrit Gupta
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
Acad
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
Monika Choudhery
 
Backtracking
BacktrackingBacktracking
Backtracking
Vikas Sharma
 
Backtracking
BacktrackingBacktracking
Backtracking
Sally Salem
 
N Queens problem
N Queens problemN Queens problem
N Queens problem
Arkadeep Dey
 
Backtracking
Backtracking  Backtracking
Backtracking
Vikas Sharma
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
Monika Choudhery
 
algorithm Unit 5
algorithm Unit 5 algorithm Unit 5
algorithm Unit 5
Monika Choudhery
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
Mohanlal Sukhadia University (MLSU)
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
Monika Choudhery
 
44 randomized-algorithms
44 randomized-algorithms44 randomized-algorithms
44 randomized-algorithms
AjitSaraf1
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
Monika Choudhery
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1
chidabdu
 
Recursion
RecursionRecursion
Recursion
Malainine Zaid
 
Signal Processing Assignment Help
Signal Processing Assignment HelpSignal Processing Assignment Help
Signal Processing Assignment Help
Matlab Assignment Experts
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
Programming Homework Help
 
3 recursion
3 recursion3 recursion
3 recursion
Nguync91368
 
Skyline queries
Skyline queriesSkyline queries
Skyline queries
Victor Torras
 

What's hot (20)

The n Queen Problem
The n Queen ProblemThe n Queen Problem
The n Queen Problem
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Backtracking
BacktrackingBacktracking
Backtracking
 
N Queens problem
N Queens problemN Queens problem
N Queens problem
 
Backtracking
Backtracking  Backtracking
Backtracking
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
algorithm Unit 5
algorithm Unit 5 algorithm Unit 5
algorithm Unit 5
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
44 randomized-algorithms
44 randomized-algorithms44 randomized-algorithms
44 randomized-algorithms
 
algorithm Unit 3
algorithm Unit 3algorithm Unit 3
algorithm Unit 3
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1
 
Recursion
RecursionRecursion
Recursion
 
Signal Processing Assignment Help
Signal Processing Assignment HelpSignal Processing Assignment Help
Signal Processing Assignment Help
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
3 recursion
3 recursion3 recursion
3 recursion
 
Skyline queries
Skyline queriesSkyline queries
Skyline queries
 

Viewers also liked

4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
Ch 7 recursion
Ch 7 recursionCh 7 recursion
Ch 7 recursion
Chaffey College
 
Recursion
RecursionRecursion
Recursion
Asif Ali Raza
 
1 Recur
1 Recur1 Recur
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
Abdul Rehman
 
Tail recursion
Tail recursionTail recursion
Tail recursion
Rumman Ansari
 
Recursion Pattern Analysis and Feedback
Recursion Pattern Analysis and FeedbackRecursion Pattern Analysis and Feedback
Recursion Pattern Analysis and Feedback
Sander Mak (@Sander_Mak)
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
Abdullah Al-hazmy
 

Viewers also liked (10)

4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
 
Ch 7 recursion
Ch 7 recursionCh 7 recursion
Ch 7 recursion
 
Recursion
RecursionRecursion
Recursion
 
1 Recur
1 Recur1 Recur
1 Recur
 
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
 
Tail recursion
Tail recursionTail recursion
Tail recursion
 
Recursion Pattern Analysis and Feedback
Recursion Pattern Analysis and FeedbackRecursion Pattern Analysis and Feedback
Recursion Pattern Analysis and Feedback
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 

Similar to Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA

Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
مدخل إلى تعلم الآلة
مدخل إلى تعلم الآلةمدخل إلى تعلم الآلة
مدخل إلى تعلم الآلة
Fares Al-Qunaieer
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
AkashSingh625550
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1
Puja Koch
 
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
Time series representations for better data mining
Time series representations for better data miningTime series representations for better data mining
Time series representations for better data mining
Peter Laurinec
 
Backtracking Algorithm.pptx
Backtracking Algorithm.pptxBacktracking Algorithm.pptx
Backtracking Algorithm.pptx
sangeeta194160
 
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory AccessAccelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
Igor Sfiligoi
 
Chap 4 local_search
Chap 4 local_search Chap 4 local_search
Chap 4 local_search
Rakhi Gupta
 
DeepLearningLecture.pptx
DeepLearningLecture.pptxDeepLearningLecture.pptx
DeepLearningLecture.pptx
ssuserf07225
 
lec10svm.ppt
lec10svm.pptlec10svm.ppt
lec10svm.ppt
TheULTIMATEALLROUNDE
 
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
Universitat Politècnica de Catalunya
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
Narayan Sau
 
[Paper Reading] Attention is All You Need
[Paper Reading] Attention is All You Need[Paper Reading] Attention is All You Need
[Paper Reading] Attention is All You Need
Daiki Tanaka
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
LavanyaJ28
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
Arumugam90
 
Divide and Conquer Case Study
Divide and Conquer Case StudyDivide and Conquer Case Study
Divide and Conquer Case Study
KushagraChadha1
 
Topological Sort Algorithm.pptx
Topological Sort Algorithm.pptxTopological Sort Algorithm.pptx
Topological Sort Algorithm.pptx
MuhammadShafi89
 
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
Tech in Asia ID
 
Case Study of Convolutional Neural Network
Case Study of Convolutional Neural NetworkCase Study of Convolutional Neural Network
Case Study of Convolutional Neural Network
NamHyuk Ahn
 

Similar to Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA (20)

Recursion and Sorting Algorithms
Recursion and Sorting AlgorithmsRecursion and Sorting Algorithms
Recursion and Sorting Algorithms
 
مدخل إلى تعلم الآلة
مدخل إلى تعلم الآلةمدخل إلى تعلم الآلة
مدخل إلى تعلم الآلة
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Dsoop (co 221) 1
Dsoop (co 221) 1Dsoop (co 221) 1
Dsoop (co 221) 1
 
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
 
Time series representations for better data mining
Time series representations for better data miningTime series representations for better data mining
Time series representations for better data mining
 
Backtracking Algorithm.pptx
Backtracking Algorithm.pptxBacktracking Algorithm.pptx
Backtracking Algorithm.pptx
 
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory AccessAccelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
Accelerating Key Bioinformatics Tasks 100-fold by Improving Memory Access
 
Chap 4 local_search
Chap 4 local_search Chap 4 local_search
Chap 4 local_search
 
DeepLearningLecture.pptx
DeepLearningLecture.pptxDeepLearningLecture.pptx
DeepLearningLecture.pptx
 
lec10svm.ppt
lec10svm.pptlec10svm.ppt
lec10svm.ppt
 
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
Optimization for Neural Network Training - Veronica Vilaplana - UPC Barcelona...
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
 
[Paper Reading] Attention is All You Need
[Paper Reading] Attention is All You Need[Paper Reading] Attention is All You Need
[Paper Reading] Attention is All You Need
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
 
Divide and Conquer Case Study
Divide and Conquer Case StudyDivide and Conquer Case Study
Divide and Conquer Case Study
 
Topological Sort Algorithm.pptx
Topological Sort Algorithm.pptxTopological Sort Algorithm.pptx
Topological Sort Algorithm.pptx
 
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
 
Case Study of Convolutional Neural Network
Case Study of Convolutional Neural NetworkCase Study of Convolutional Neural Network
Case Study of Convolutional Neural Network
 

More from Dheeraj Kataria

Information Security- Threats and Attacks presentation by DHEERAJ KATARIA
Information Security- Threats and Attacks presentation by DHEERAJ KATARIAInformation Security- Threats and Attacks presentation by DHEERAJ KATARIA
Information Security- Threats and Attacks presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
Microprocessor Protected Mode Memory addressing By DHEERAJ KATARIA
Microprocessor Protected Mode Memory addressing By DHEERAJ KATARIAMicroprocessor Protected Mode Memory addressing By DHEERAJ KATARIA
Microprocessor Protected Mode Memory addressing By DHEERAJ KATARIA
Dheeraj Kataria
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
E facilities of Municipal Corporation of Delhi By DHEERAJ KATARIA
E facilities of Municipal Corporation of Delhi By DHEERAJ KATARIAE facilities of Municipal Corporation of Delhi By DHEERAJ KATARIA
E facilities of Municipal Corporation of Delhi By DHEERAJ KATARIA
Dheeraj Kataria
 
Matrix presentation By DHEERAJ KATARIA
Matrix presentation By DHEERAJ KATARIAMatrix presentation By DHEERAJ KATARIA
Matrix presentation By DHEERAJ KATARIA
Dheeraj Kataria
 
Heritage and tourism in india by DHEERAJ KATARIA
Heritage and  tourism in india by DHEERAJ KATARIAHeritage and  tourism in india by DHEERAJ KATARIA
Heritage and tourism in india by DHEERAJ KATARIA
Dheeraj Kataria
 

More from Dheeraj Kataria (6)

Information Security- Threats and Attacks presentation by DHEERAJ KATARIA
Information Security- Threats and Attacks presentation by DHEERAJ KATARIAInformation Security- Threats and Attacks presentation by DHEERAJ KATARIA
Information Security- Threats and Attacks presentation by DHEERAJ KATARIA
 
Microprocessor Protected Mode Memory addressing By DHEERAJ KATARIA
Microprocessor Protected Mode Memory addressing By DHEERAJ KATARIAMicroprocessor Protected Mode Memory addressing By DHEERAJ KATARIA
Microprocessor Protected Mode Memory addressing By DHEERAJ KATARIA
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
E facilities of Municipal Corporation of Delhi By DHEERAJ KATARIA
E facilities of Municipal Corporation of Delhi By DHEERAJ KATARIAE facilities of Municipal Corporation of Delhi By DHEERAJ KATARIA
E facilities of Municipal Corporation of Delhi By DHEERAJ KATARIA
 
Matrix presentation By DHEERAJ KATARIA
Matrix presentation By DHEERAJ KATARIAMatrix presentation By DHEERAJ KATARIA
Matrix presentation By DHEERAJ KATARIA
 
Heritage and tourism in india by DHEERAJ KATARIA
Heritage and  tourism in india by DHEERAJ KATARIAHeritage and  tourism in india by DHEERAJ KATARIA
Heritage and tourism in india by DHEERAJ KATARIA
 

Recently uploaded

MVC Interview Questions PDF By ScholarHat
MVC Interview Questions PDF By ScholarHatMVC Interview Questions PDF By ScholarHat
MVC Interview Questions PDF By ScholarHat
Scholarhat
 
FIRST AID PRESENTATION ON INDUSTRIAL SAFETY by dr lal.ppt
FIRST AID PRESENTATION ON INDUSTRIAL SAFETY by dr lal.pptFIRST AID PRESENTATION ON INDUSTRIAL SAFETY by dr lal.ppt
FIRST AID PRESENTATION ON INDUSTRIAL SAFETY by dr lal.ppt
ashutoshklal29
 
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Alvaro Barbosa
 
React Interview Question PDF By ScholarHat
React Interview Question PDF By ScholarHatReact Interview Question PDF By ScholarHat
React Interview Question PDF By ScholarHat
Scholarhat
 
3. Maturity_indices_of_fruits_and_vegetable.pptx
3. Maturity_indices_of_fruits_and_vegetable.pptx3. Maturity_indices_of_fruits_and_vegetable.pptx
3. Maturity_indices_of_fruits_and_vegetable.pptx
UmeshTimilsina1
 
Mail Server Configuration Using App passwords in Odoo 17
Mail Server Configuration Using App passwords in Odoo 17Mail Server Configuration Using App passwords in Odoo 17
Mail Server Configuration Using App passwords in Odoo 17
Celine George
 
C Interview Questions PDF By Scholarhat.pdf
C Interview Questions PDF By Scholarhat.pdfC Interview Questions PDF By Scholarhat.pdf
C Interview Questions PDF By Scholarhat.pdf
Scholarhat
 
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
ALBERTHISOLER1
 
DepEd School Calendar 2024-2025 DO_s2024_008
DepEd School Calendar 2024-2025 DO_s2024_008DepEd School Calendar 2024-2025 DO_s2024_008
DepEd School Calendar 2024-2025 DO_s2024_008
Glenn Rivera
 
SD_Integrating 21st Century Skills in Classroom-based Assessment.pptx
SD_Integrating 21st Century Skills in Classroom-based Assessment.pptxSD_Integrating 21st Century Skills in Classroom-based Assessment.pptx
SD_Integrating 21st Century Skills in Classroom-based Assessment.pptx
elwoodprias1
 
Parkinson Disease & Anti-Parkinsonian Drugs.pptx
Parkinson Disease & Anti-Parkinsonian Drugs.pptxParkinson Disease & Anti-Parkinsonian Drugs.pptx
Parkinson Disease & Anti-Parkinsonian Drugs.pptx
AnujVishwakarma34
 
Node JS Interview Question PDF By ScholarHat
Node JS Interview Question PDF By ScholarHatNode JS Interview Question PDF By ScholarHat
Node JS Interview Question PDF By ScholarHat
Scholarhat
 
MATATAG CURRICULUM sample lesson exemplar.docx
MATATAG CURRICULUM sample lesson exemplar.docxMATATAG CURRICULUM sample lesson exemplar.docx
MATATAG CURRICULUM sample lesson exemplar.docx
yardenmendoza
 
11. Post harvest quality, Quality criteria and Judgement.pptx
11. Post harvest quality, Quality criteria and Judgement.pptx11. Post harvest quality, Quality criteria and Judgement.pptx
11. Post harvest quality, Quality criteria and Judgement.pptx
UmeshTimilsina1
 
View Inheritance in Odoo 17 - Odoo 17 Slides
View Inheritance in Odoo 17 - Odoo 17  SlidesView Inheritance in Odoo 17 - Odoo 17  Slides
View Inheritance in Odoo 17 - Odoo 17 Slides
Celine George
 
Demonstration module in Odoo 17 - Odoo 17 Slides
Demonstration module in Odoo 17 - Odoo 17 SlidesDemonstration module in Odoo 17 - Odoo 17 Slides
Demonstration module in Odoo 17 - Odoo 17 Slides
Celine George
 
2024 Winter SWAYAM NPTEL & A Student.pptx
2024 Winter SWAYAM NPTEL & A Student.pptx2024 Winter SWAYAM NPTEL & A Student.pptx
2024 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
BỘ ĐỀ THI HỌC SINH GIỎI CÁC TỈNH MÔN TIẾNG ANH LỚP 9 NĂM HỌC 2023-2024 (CÓ FI...
BỘ ĐỀ THI HỌC SINH GIỎI CÁC TỈNH MÔN TIẾNG ANH LỚP 9 NĂM HỌC 2023-2024 (CÓ FI...BỘ ĐỀ THI HỌC SINH GIỎI CÁC TỈNH MÔN TIẾNG ANH LỚP 9 NĂM HỌC 2023-2024 (CÓ FI...
BỘ ĐỀ THI HỌC SINH GIỎI CÁC TỈNH MÔN TIẾNG ANH LỚP 9 NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
7. Post Harvest Entomology and their control.pptx
7. Post Harvest Entomology and their control.pptx7. Post Harvest Entomology and their control.pptx
7. Post Harvest Entomology and their control.pptx
UmeshTimilsina1
 
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
mansk2
 

Recently uploaded (20)

MVC Interview Questions PDF By ScholarHat
MVC Interview Questions PDF By ScholarHatMVC Interview Questions PDF By ScholarHat
MVC Interview Questions PDF By ScholarHat
 
FIRST AID PRESENTATION ON INDUSTRIAL SAFETY by dr lal.ppt
FIRST AID PRESENTATION ON INDUSTRIAL SAFETY by dr lal.pptFIRST AID PRESENTATION ON INDUSTRIAL SAFETY by dr lal.ppt
FIRST AID PRESENTATION ON INDUSTRIAL SAFETY by dr lal.ppt
 
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
Benchmarking Sustainability: Neurosciences and AI Tech Research in Macau - Ke...
 
React Interview Question PDF By ScholarHat
React Interview Question PDF By ScholarHatReact Interview Question PDF By ScholarHat
React Interview Question PDF By ScholarHat
 
3. Maturity_indices_of_fruits_and_vegetable.pptx
3. Maturity_indices_of_fruits_and_vegetable.pptx3. Maturity_indices_of_fruits_and_vegetable.pptx
3. Maturity_indices_of_fruits_and_vegetable.pptx
 
Mail Server Configuration Using App passwords in Odoo 17
Mail Server Configuration Using App passwords in Odoo 17Mail Server Configuration Using App passwords in Odoo 17
Mail Server Configuration Using App passwords in Odoo 17
 
C Interview Questions PDF By Scholarhat.pdf
C Interview Questions PDF By Scholarhat.pdfC Interview Questions PDF By Scholarhat.pdf
C Interview Questions PDF By Scholarhat.pdf
 
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
Brigada Eskwela 2024 PowerPoint Update for SY 2024-2025
 
DepEd School Calendar 2024-2025 DO_s2024_008
DepEd School Calendar 2024-2025 DO_s2024_008DepEd School Calendar 2024-2025 DO_s2024_008
DepEd School Calendar 2024-2025 DO_s2024_008
 
SD_Integrating 21st Century Skills in Classroom-based Assessment.pptx
SD_Integrating 21st Century Skills in Classroom-based Assessment.pptxSD_Integrating 21st Century Skills in Classroom-based Assessment.pptx
SD_Integrating 21st Century Skills in Classroom-based Assessment.pptx
 
Parkinson Disease & Anti-Parkinsonian Drugs.pptx
Parkinson Disease & Anti-Parkinsonian Drugs.pptxParkinson Disease & Anti-Parkinsonian Drugs.pptx
Parkinson Disease & Anti-Parkinsonian Drugs.pptx
 
Node JS Interview Question PDF By ScholarHat
Node JS Interview Question PDF By ScholarHatNode JS Interview Question PDF By ScholarHat
Node JS Interview Question PDF By ScholarHat
 
MATATAG CURRICULUM sample lesson exemplar.docx
MATATAG CURRICULUM sample lesson exemplar.docxMATATAG CURRICULUM sample lesson exemplar.docx
MATATAG CURRICULUM sample lesson exemplar.docx
 
11. Post harvest quality, Quality criteria and Judgement.pptx
11. Post harvest quality, Quality criteria and Judgement.pptx11. Post harvest quality, Quality criteria and Judgement.pptx
11. Post harvest quality, Quality criteria and Judgement.pptx
 
View Inheritance in Odoo 17 - Odoo 17 Slides
View Inheritance in Odoo 17 - Odoo 17  SlidesView Inheritance in Odoo 17 - Odoo 17  Slides
View Inheritance in Odoo 17 - Odoo 17 Slides
 
Demonstration module in Odoo 17 - Odoo 17 Slides
Demonstration module in Odoo 17 - Odoo 17 SlidesDemonstration module in Odoo 17 - Odoo 17 Slides
Demonstration module in Odoo 17 - Odoo 17 Slides
 
2024 Winter SWAYAM NPTEL & A Student.pptx
2024 Winter SWAYAM NPTEL & A Student.pptx2024 Winter SWAYAM NPTEL & A Student.pptx
2024 Winter SWAYAM NPTEL & A Student.pptx
 
BỘ ĐỀ THI HỌC SINH GIỎI CÁC TỈNH MÔN TIẾNG ANH LỚP 9 NĂM HỌC 2023-2024 (CÓ FI...
BỘ ĐỀ THI HỌC SINH GIỎI CÁC TỈNH MÔN TIẾNG ANH LỚP 9 NĂM HỌC 2023-2024 (CÓ FI...BỘ ĐỀ THI HỌC SINH GIỎI CÁC TỈNH MÔN TIẾNG ANH LỚP 9 NĂM HỌC 2023-2024 (CÓ FI...
BỘ ĐỀ THI HỌC SINH GIỎI CÁC TỈNH MÔN TIẾNG ANH LỚP 9 NĂM HỌC 2023-2024 (CÓ FI...
 
7. Post Harvest Entomology and their control.pptx
7. Post Harvest Entomology and their control.pptx7. Post Harvest Entomology and their control.pptx
7. Post Harvest Entomology and their control.pptx
 
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
11EHS Term 3 Week 1 Unit 1 Review: Feedback and improvementpptx
 

Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA

  • 2. A more complicated case of recursion is found in definitions in which a function is not only defined in terms of itself but it is also used as one of the parameters. Example:          4if))2(2( ,4if ,0if0 )( nnhh nn n nh h(1)=h(2+h(2))=h(14)=14 h(2)=h(2+h(4))=h(12)=12 h(3)=h(2+h(6))=h(2+6)=h(8)=8 h(4)=h(2+h(8))=h(2+8)=h(10)=10 Recursion • Nested Recursion
  • 3. The Ackermann function          otherwise))1,(,1( ,0,0if)1,1( ,0if1 ),( mnAnA mnnA nm mnA This function is interesting because of its remarkably rapid growth. It grows so fast that it is guaranteed not to have a representation by a formula that uses arithmetical operations such as addition, multiplication, and exponentiation. Recursion • Nested Recursion
  • 5. Logical simplicity and readability are used as an argument supporting the use of recursion. The price for using recursion is slowing down execution time and storing on the run-time stack more things than required in a non-recursive approach. Example: The Fibonacci numbers          .2if)1()2( ,2if1 ,1if1 )( iiFiF i i iF void Fibonacci(int n) { If (n<2) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); } Recursion • Excessive Recursion
  • 8. void IterativeFib(int n) { if (n < 2) return n; else { int i = 2, tmp, current = 1, last = 0; for ( ; i<=n; ++i) { tmp= current; current += last; last = tmp; } return current; } } Recursion • Excessive Recursion
  • 10. We can also solve this problem by using a formula discovered by A. De Moivre. The characteristic formula is :- 5 ) 2 51 () 2 51 ( )( nn nf     Can be neglected when n is large Recursion • Excessive Recursion The value of is approximately -0.618034) 2 51 ( 
  • 11.  Suppose you have to make a series of decisions, among various choices, where • You don’t have enough information to know what to choose • Each decision leads to a new set of choices • Some sequence of choices (possibly more than one) may be a solution to your problem  Backtracking is a methodical way of trying out various sequences of decisions, until you find one that “works” Recursion • Backtracking
  • 12. Backtracking allows us to systematically try all available avenues from a certain point after some of them lead to nowhere. Using backtracking, we can always return to a position which offers other possibilities for successfully solving the problem. Recursion • Backtracking
  • 13. Recursion • Backtracking  Place 8 queens on an 8 by 8 chess board so that no two of them are on the same row, column, or diagonal The Eight Queens Problem
  • 15. The Eight Queens Problem Pseudo code of the backtracking algorithm PutQueen(row) for every position col on the same row if position col is available { place the next queen in position col; if (row < 8) PutQueen(row+1); else success; remove the queen from position col; /* backtrack */ } Recursion • Backtracking
  • 16. The Eight Queens Problem Natural Implementation 1 Initialization 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Q The first queen 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 1 0 Q The second queen 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 Q 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 0 0 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 0 0 1 0 0 1 1 1 1 0 0 Recursion • Backtracking
  • 17. The Eight Queens Problem Natural Implementation Q The third queen 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 Q 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 Q 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 0 Q The fourth queen 0 0 0 0 0 0 0 0 0 0 Q 0 0 0 0 0 Q 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 Q 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 Q The 5th & 6th queen 0 0 0 0 0 0 0 0 0 0 Q 0 0 0 0 0 Q 0 0 0 0 0 0 0 0 0 0 Q 0 0 0 0 0 Q 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Q 0 0 Have to backtrack now!This would be queen now. Recursion • Backtracking
  • 18. The Eight Queens Problem Natural Implementation The setting and resetting part would be the most time-consuming part of this implementation. However, if we focus solely on the queens, we can consider the chessboard from their perspective. For the queens, the board is not divided into squares, but into rows, columns, and diagonals. Recursion • Backtracking
  • 19. The Eight Queens Problem Simplified data structure A 4 by 4 chessboard Row-column = constant for each diagonal Recursion • Backtracking
  • 20. The Eight Queens Problem Recursion • Backtracking
  • 21. The Eight Queens Problem Recursion • Backtracking
  • 22. The Eight Queens Problem Recursion • Backtracking
  • 25. • Usually recursive algorithms have less code, therefore algorithms can be easier to write and understand - e.g. Towers of Hanoi. However, avoid using excessively recursive algorithms even if the code is simple. • Sometimes recursion provides a much simpler solution. Obtaining the same result using iteration requires complicated coding - e.g. Quicksort, Towers of Hanoi, etc. Why Recursion?
  • 26. Why Recursion? • Recursive methods provide a very natural mechanism for processing recursive data structures. A recursive data structure is a data structure that is defined recursively – e.g. Linked-list, Tree.  Functional programming languages such as Clean, FP, Haskell, Miranda, and SML do not have explicit loop constructs. In these languages looping is achieved by recursion.
  • 27. • Recursion is a powerful problem-solving technique that often produces very clean solutions to even the most complex problems. • Recursive solutions can be easier to understand and to describe than iterative solutions. Why Recursion?
  • 28. • By using recursion, you can often write simple, short implementations of your solution. • However, just because an algorithm can be implemented in a recursive manner doesn’t mean that it should be implemented in a recursive manner. Why Recursion?
  • 29. Limitations of Recursion • Recursive solutions may involve extensive overhead because they use calls. • When a call is made, it takes time to build a stackframe and push it onto the system stack. • Conversely, when a return is executed, the stackframe must be popped from the stack and the local variables reset to their previous values – this also takes time.
  • 30. Limitations of Recursion • In general, recursive algorithms run slower than their iterative counterparts. • Also, every time we make a call, we must use some of the memory resources to make room for the stackframe.
  • 31. Limitations of Recursion • Therefore, if the recursion is deep, say, factorial(1000), we may run out of memory. • Because of this, it is usually best to develop iterative algorithms when we are working with large numbers.
  • 32. Main disadvantage of programming recursively • The main disadvantage of programming recursively is that, while it makes it easier to write simple and elegant programs, it also makes it easier to write inefficient ones. • when we use recursion to solve problems we are interested exclusively with correctness, and not at all with efficiency. Consequently, our simple, elegant recursive algorithms may be inherently inefficient.