SlideShare a Scribd company logo
1 of 15
PRESENTATION ON RECURSION
Presented By:
NITESH MODI
COMPUTER SCIENCE ENGINEERING
UNIVERSITY ROLL NO:- 35000120060
Under the Guidance of Tanushree Garai Mam
RECURSION
 The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called as recursive function.
 A recursive function calls itself to solve a smaller version of its task until a final call is made
which does not require a call to itself.
 Every recursive function has two major cases:
1. Base Case, in which the problem is simple enough to be solved directly without making
any further calls to the same function.
2. Recursive Case, in which first the problem at hand is divided into simpler sub-parts.
Second the function calls itself but with sub-parts of the problem obtained in the first step.
Third, the result is obtained by combining the solutions of simpler sub-parts.
 Let us take an example of calculating factorial of a number. To calculate n!, we do n*(n-1)!
5!=5*4! = 5*4*3! = 5*4*3*2! = 5*4*3*2*1 = 120
Here, base case is when n=1, return 1
recursive case is factorial(n)=n*factorial(n-1)
int fact(n){
if(n==1) return 1;
else return(n*fact(n-1));
}
ITERATION VERSUS RECURSON
In simple terms, an iterative function is one that loops to repeat some part of the code, and a recursive
function is one that calls itself again to repeat the code. Using a simple for loop to display the numbers
from one to ten is an iterative process.
PROPERTY RECURSION ITERATION
Definition Function calls itself A set of instructions repeatedly executed
Application For Fuctions For Loops
Termination Through base case, where there will be no
function call.
When the termination condition for the
iterator ceases to be satisfied.
Usage Used when code size needs to be small, and
time complexity is not an issue.
Used when time complexity needs to be
balanced against an expanded code size.
Code Size Smaller code size Larger Code Size
Time Complexity Very high(generally exponential) time
complexity
Relatively lower time
complexity(generally polynomial-
logarithmic)
BASIC STEPS OF A RECURSIVE PROGRAM
 Step 1: Specify the base case which will stop the function from making a call to
itself.
 Step 2: Check to see whether the current value being processed matches with
the value of the base case. If yes, process and return the value.
 Step 3: Divide the problem into smaller or simpler sub-problems.
 Step 4: Call the function from the sub-problems.
 Step 5: Combine the results of the sub-problems.
 Step 6: Return the result of the entire problem.
The base case of a recursive function acts as the terminating condition. So, in the
absence of an explicitly defined base case, a recursive function would call itself
indefinitely.
TYPES OF RECURSION
Any recursive function can be characterized based on:
 Whether the function calls itself directly or indirectly
 Whether any instruction is pending at each recursive call
Based on this, types of recursion are:-
1. Direct Recursion 2. Indirect Recursion
3. Tail Recursion 4. Non-Tail Recursion
 Direct Recursion:- A function is called direct recursive if it calls the same function again.
int func(int n) {
if(n==0) return n;
else return (func(n-1)); }
 Indirect Recursion:- A function(fun) is called indirect recursive if it calls another function(fun2)
and then fun2 calls fun. So fun is called indirectly by itself.
fun(){
//some code
fun2();
//some code }
fun2(){
//somecode
fun(); }
Before starting tail and non-tail recursion, let us first read what is stack.
A stack is a linear data structure, which is based on the LIFO principle (Last In First Out).
If we consider a pile of plates, where one plate is kept over another wheneverwe remove the
plate the plate kept at last is removed first. Similarly if we replace plates with function calls,
then the function called at last is returned first. Recursion is based on this principle.
 Tail recursion:- A recursive function is said to be tail recursive if the recursive call is the last
thing done by the function.
void fun(int n){
if(n==0) return;
else printf(“%d ”,n);
return fun(n-1); }
int main(){
fun(3);
return 0; }
Here first fun(3) then fun(2) then fun(1) then fun(0) is called.
Output:- 3 2 1
 Non-Tail Recursion:- A recursive function is said to be non-tail recursive if the recursive call is not the
last statement to be executed by the function. After returning back, there is something left to evaluate.
void fun(int n){
if(n==0) return;
fun(n-1);
printf(“%d ”,n); }
int main() {
fun(3);
return 0; }
Here again first fun(3) is called then fun(2) then fun(1) then fun(0) which is return, so function gets
returned to the last function called i.e., fun(1). Now in fun(1) one statement(printf(“%d ”,n); ) is left to be
executed. So it executes first and prints ‘1’. Then function call returns t fun(2), then 2 is printed, similarly
3 is printed. Here the function calling worked on the principle of stack i.e., LIFO.
Output:- 1 2 3
TOWER OF HANOI
The tower of Hanoi is one of the applications of recursion. It says ‘if we can solve n-1 cases, then we can
easily solve the nth case’. In this problem, we have three poles given namely A,B,C. A number of rings is
mounted on ring A, we have to move all the rings to pole B using pole C. the main issue is that the smaller
disk must always come above the larger disk. Here this is the picture, where three rings are mounted on pole
1. We have to move all the rings to pole 3 using pole 2.
ALGORITHM OF TOWER OF HANOI
To summarize, the solution to our problem of moving n rings from A to B
using C as spare can be given as:
Base case: if n=1
 Move the ring from A to B using C as spare
Recursive case:
 Move n-1 rings from A to C using B as spare
 Move the one ring left on A to B using C as spare
 Move n-1 rings from C to B using A as spare
CODE OUTPUT
N-QUEENS PROBLEM
 Given a chessboard of n x n, the n-queen
problem involves placing n queens in such a way that they
cannot attack each other.
 The queens can attack each other if they are placed in:
• the same column
• the same row
• the same diagonal
In the given picture, there is 4*4 chessboard. The 4 queens
are arranged in such a way that none can attack each other.
ALGORITHM FOR N-QUEEN PROBLEM
Backtracking can be used to solve this​ problem.
1. Begin with the left-most column.
2. For every row in the column:
2.1. Try placing the queen such that it cannot attack the queen in the previous
columns.
2.2. If such a placement is possible, add this cell to the solution set and recursively
check if this leads to a solution by calling the function on the subsequent
column. If it does, return one.
2.3. Else, remove this cell from the solution set.
3. Backtrack to the previous column by returning zero if no solution exists after the
completion of step 2.
4. Stop the recursion when all the queens are placed.
CODE
0UTPUT
THANK YOU

More Related Content

Similar to 35000120060_Nitesh Modi_CSE Presentation on recursion.pptx

Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxPochupouOwo
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer scienceRiazul Islam
 
Recursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & StructureRecursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & Structurecogaxor346
 
Complexity Analysis of Recursive Function
Complexity Analysis of Recursive FunctionComplexity Analysis of Recursive Function
Complexity Analysis of Recursive FunctionMeghaj Mallick
 
Recursion | C++ | DSA
Recursion | C++ | DSARecursion | C++ | DSA
Recursion | C++ | DSASumit Pandey
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptxajajkhan16
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingJay Nagar
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and loopingxcoolanurag
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problemsJyotsna Suryadevara
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms NotesAndres Mendez-Vazquez
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programmingOye Tu
 

Similar to 35000120060_Nitesh Modi_CSE Presentation on recursion.pptx (20)

algo_vc_lecture8.ppt
algo_vc_lecture8.pptalgo_vc_lecture8.ppt
algo_vc_lecture8.ppt
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptx
 
Recursion
RecursionRecursion
Recursion
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
Recursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & StructureRecursion vs. Iteration: Code Efficiency & Structure
Recursion vs. Iteration: Code Efficiency & Structure
 
Complexity Analysis of Recursive Function
Complexity Analysis of Recursive FunctionComplexity Analysis of Recursive Function
Complexity Analysis of Recursive Function
 
Recursion | C++ | DSA
Recursion | C++ | DSARecursion | C++ | DSA
Recursion | C++ | DSA
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Recursion and looping
Recursion and loopingRecursion and looping
Recursion and looping
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Recursion.ppt
 Recursion.ppt Recursion.ppt
Recursion.ppt
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
 
Ada notes
Ada notesAda notes
Ada notes
 
L06
L06L06
L06
 
14. Recursion.pdf
14. Recursion.pdf14. Recursion.pdf
14. Recursion.pdf
 
Solution 3.
Solution 3.Solution 3.
Solution 3.
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 

Recently uploaded

GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 

Recently uploaded (20)

DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 

35000120060_Nitesh Modi_CSE Presentation on recursion.pptx

  • 1. PRESENTATION ON RECURSION Presented By: NITESH MODI COMPUTER SCIENCE ENGINEERING UNIVERSITY ROLL NO:- 35000120060 Under the Guidance of Tanushree Garai Mam
  • 2. RECURSION  The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function.  A recursive function calls itself to solve a smaller version of its task until a final call is made which does not require a call to itself.  Every recursive function has two major cases: 1. Base Case, in which the problem is simple enough to be solved directly without making any further calls to the same function. 2. Recursive Case, in which first the problem at hand is divided into simpler sub-parts. Second the function calls itself but with sub-parts of the problem obtained in the first step. Third, the result is obtained by combining the solutions of simpler sub-parts.  Let us take an example of calculating factorial of a number. To calculate n!, we do n*(n-1)! 5!=5*4! = 5*4*3! = 5*4*3*2! = 5*4*3*2*1 = 120 Here, base case is when n=1, return 1 recursive case is factorial(n)=n*factorial(n-1) int fact(n){ if(n==1) return 1; else return(n*fact(n-1)); }
  • 3. ITERATION VERSUS RECURSON In simple terms, an iterative function is one that loops to repeat some part of the code, and a recursive function is one that calls itself again to repeat the code. Using a simple for loop to display the numbers from one to ten is an iterative process. PROPERTY RECURSION ITERATION Definition Function calls itself A set of instructions repeatedly executed Application For Fuctions For Loops Termination Through base case, where there will be no function call. When the termination condition for the iterator ceases to be satisfied. Usage Used when code size needs to be small, and time complexity is not an issue. Used when time complexity needs to be balanced against an expanded code size. Code Size Smaller code size Larger Code Size Time Complexity Very high(generally exponential) time complexity Relatively lower time complexity(generally polynomial- logarithmic)
  • 4. BASIC STEPS OF A RECURSIVE PROGRAM  Step 1: Specify the base case which will stop the function from making a call to itself.  Step 2: Check to see whether the current value being processed matches with the value of the base case. If yes, process and return the value.  Step 3: Divide the problem into smaller or simpler sub-problems.  Step 4: Call the function from the sub-problems.  Step 5: Combine the results of the sub-problems.  Step 6: Return the result of the entire problem. The base case of a recursive function acts as the terminating condition. So, in the absence of an explicitly defined base case, a recursive function would call itself indefinitely.
  • 5. TYPES OF RECURSION Any recursive function can be characterized based on:  Whether the function calls itself directly or indirectly  Whether any instruction is pending at each recursive call Based on this, types of recursion are:- 1. Direct Recursion 2. Indirect Recursion 3. Tail Recursion 4. Non-Tail Recursion  Direct Recursion:- A function is called direct recursive if it calls the same function again. int func(int n) { if(n==0) return n; else return (func(n-1)); }  Indirect Recursion:- A function(fun) is called indirect recursive if it calls another function(fun2) and then fun2 calls fun. So fun is called indirectly by itself. fun(){ //some code fun2(); //some code } fun2(){ //somecode fun(); }
  • 6. Before starting tail and non-tail recursion, let us first read what is stack. A stack is a linear data structure, which is based on the LIFO principle (Last In First Out). If we consider a pile of plates, where one plate is kept over another wheneverwe remove the plate the plate kept at last is removed first. Similarly if we replace plates with function calls, then the function called at last is returned first. Recursion is based on this principle.  Tail recursion:- A recursive function is said to be tail recursive if the recursive call is the last thing done by the function. void fun(int n){ if(n==0) return; else printf(“%d ”,n); return fun(n-1); } int main(){ fun(3); return 0; } Here first fun(3) then fun(2) then fun(1) then fun(0) is called. Output:- 3 2 1
  • 7.  Non-Tail Recursion:- A recursive function is said to be non-tail recursive if the recursive call is not the last statement to be executed by the function. After returning back, there is something left to evaluate. void fun(int n){ if(n==0) return; fun(n-1); printf(“%d ”,n); } int main() { fun(3); return 0; } Here again first fun(3) is called then fun(2) then fun(1) then fun(0) which is return, so function gets returned to the last function called i.e., fun(1). Now in fun(1) one statement(printf(“%d ”,n); ) is left to be executed. So it executes first and prints ‘1’. Then function call returns t fun(2), then 2 is printed, similarly 3 is printed. Here the function calling worked on the principle of stack i.e., LIFO. Output:- 1 2 3
  • 8. TOWER OF HANOI The tower of Hanoi is one of the applications of recursion. It says ‘if we can solve n-1 cases, then we can easily solve the nth case’. In this problem, we have three poles given namely A,B,C. A number of rings is mounted on ring A, we have to move all the rings to pole B using pole C. the main issue is that the smaller disk must always come above the larger disk. Here this is the picture, where three rings are mounted on pole 1. We have to move all the rings to pole 3 using pole 2.
  • 9. ALGORITHM OF TOWER OF HANOI To summarize, the solution to our problem of moving n rings from A to B using C as spare can be given as: Base case: if n=1  Move the ring from A to B using C as spare Recursive case:  Move n-1 rings from A to C using B as spare  Move the one ring left on A to B using C as spare  Move n-1 rings from C to B using A as spare
  • 11. N-QUEENS PROBLEM  Given a chessboard of n x n, the n-queen problem involves placing n queens in such a way that they cannot attack each other.  The queens can attack each other if they are placed in: • the same column • the same row • the same diagonal In the given picture, there is 4*4 chessboard. The 4 queens are arranged in such a way that none can attack each other.
  • 12. ALGORITHM FOR N-QUEEN PROBLEM Backtracking can be used to solve this​ problem. 1. Begin with the left-most column. 2. For every row in the column: 2.1. Try placing the queen such that it cannot attack the queen in the previous columns. 2.2. If such a placement is possible, add this cell to the solution set and recursively check if this leads to a solution by calling the function on the subsequent column. If it does, return one. 2.3. Else, remove this cell from the solution set. 3. Backtrack to the previous column by returning zero if no solution exists after the completion of step 2. 4. Stop the recursion when all the queens are placed.
  • 13. CODE