SlideShare a Scribd company logo
Data Structures
Recursion
Andres Mendez-Vazquez
May 6, 2015
1 / 19
Images/cinvestav-
Outline
1 Introduction
2 Recursive Method
Questions to Answer When Designing a Recursion
3 Design guidelines for successful recursion
Examples of Code with Innite Recursion
Example
2 / 19
Images/cinvestav-
What is recursion?
Fact
Sometimes, the best way to solve a problem is by solving a smaller version
of the exact same problem rst
Example
You can build a house by hiring a contractor.
The contractor in turn hires several subcontractors to complete
portions of the house.
Each subcontractor might hire other subcontractors to help.
3 / 19
Images/cinvestav-
What is recursion?
Fact
Sometimes, the best way to solve a problem is by solving a smaller version
of the exact same problem rst
Example
You can build a house by hiring a contractor.
The contractor in turn hires several subcontractors to complete
portions of the house.
Each subcontractor might hire other subcontractors to help.
3 / 19
Images/cinvestav-
Another Example
Example: Principal Idea You do your part until your friend nishes
and tells you
4 / 19
Images/cinvestav-
Recursive Method
Denition
A method that calls itself is a recursive method.
The invocation is a recursive call or recursive invocation.
The previous example can be seen in code as follow
5 / 19
Images/cinvestav-
Recursive Method
Denition
A method that calls itself is a recursive method.
The invocation is a recursive call or recursive invocation.
The previous example can be seen in code as follow
/∗∗ Counts down from a given positive integer .
@param integer an integer  0 ∗/
p u b l i c s t a t i c v o i d countDown ( i n t i n t e g e r )
{
System . out . p r i n t l n ( i n t e g e r ) ;
i f ( i n t e g e r  1)
countDown ( i n t e g e r − 1 ) ;
} // end countDown
5 / 19
Images/cinvestav-
Example with 3
Tracing the recursive call countDown(3)
//Client
public static void main(...)
{
countDown(3);
...
}
public static void countDown(3)
{
System.out.println(3);
countDown(3 – 1);
}
if (3  1)
public static void countDown(2)
{
System.out.println(2);
countDown(2 – 1);
}
if (2  1)
public static void countDown(1)
{
System.out.println(1);
}
if (1  1)
6 / 19
Images/cinvestav-
Outline
1 Introduction
2 Recursive Method
Questions to Answer When Designing a Recursion
3 Design guidelines for successful recursion
Examples of Code with Innite Recursion
Example
7 / 19
Images/cinvestav-
Questions to answer when designing a recursive solution
First
What part of the solution can you contribute directly?
Second
What smaller but identical problem has a solution that, when taken with
your contribution, provides the solution to the original problem?
Third
When does the process end? That is, what smaller but identical problem
has a known solution, and have you reached this problem, or base case?
8 / 19
Images/cinvestav-
Questions to answer when designing a recursive solution
First
What part of the solution can you contribute directly?
Second
What smaller but identical problem has a solution that, when taken with
your contribution, provides the solution to the original problem?
Third
When does the process end? That is, what smaller but identical problem
has a known solution, and have you reached this problem, or base case?
8 / 19
Images/cinvestav-
Questions to answer when designing a recursive solution
First
What part of the solution can you contribute directly?
Second
What smaller but identical problem has a solution that, when taken with
your contribution, provides the solution to the original problem?
Third
When does the process end? That is, what smaller but identical problem
has a known solution, and have you reached this problem, or base case?
8 / 19
Images/cinvestav-
For countDown
Answer 1
The method countDown displays the given integer rst.
Answer 2
The smaller problem is counting down from integer - 1.
Answer 3
The if statement asks if the process has reached the base case. In our case
integer = 1.
9 / 19
Images/cinvestav-
For countDown
Answer 1
The method countDown displays the given integer rst.
Answer 2
The smaller problem is counting down from integer - 1.
Answer 3
The if statement asks if the process has reached the base case. In our case
integer = 1.
9 / 19
Images/cinvestav-
For countDown
Answer 1
The method countDown displays the given integer rst.
Answer 2
The smaller problem is counting down from integer - 1.
Answer 3
The if statement asks if the process has reached the base case. In our case
integer = 1.
9 / 19
Images/cinvestav-
Design guidelines for successful recursion
First
The method must be given an input value.
Second
The method denition must contain logic that involves this input value and
leads to dierent cases.
Third
One or more of these cases should provide a solution that does not require
recursion. These are the base cases, or stopping cases.
Fourth
One or more cases must include a recursive invocation of the method using
a smaller argument.
10 / 19
Images/cinvestav-
Design guidelines for successful recursion
First
The method must be given an input value.
Second
The method denition must contain logic that involves this input value and
leads to dierent cases.
Third
One or more of these cases should provide a solution that does not require
recursion. These are the base cases, or stopping cases.
Fourth
One or more cases must include a recursive invocation of the method using
a smaller argument.
10 / 19
Images/cinvestav-
Design guidelines for successful recursion
First
The method must be given an input value.
Second
The method denition must contain logic that involves this input value and
leads to dierent cases.
Third
One or more of these cases should provide a solution that does not require
recursion. These are the base cases, or stopping cases.
Fourth
One or more cases must include a recursive invocation of the method using
a smaller argument.
10 / 19
Images/cinvestav-
Design guidelines for successful recursion
First
The method must be given an input value.
Second
The method denition must contain logic that involves this input value and
leads to dierent cases.
Third
One or more of these cases should provide a solution that does not require
recursion. These are the base cases, or stopping cases.
Fourth
One or more cases must include a recursive invocation of the method using
a smaller argument.
10 / 19
Images/cinvestav-
So, be careful
Innite recursion
A recursive method that does not check for a base case, or that misses the
base case, will execute forever. This situation is known as innite
recursion.
Which is known as
This situation is known as innite recursion.
11 / 19
Images/cinvestav-
So, be careful
Innite recursion
A recursive method that does not check for a base case, or that misses the
base case, will execute forever. This situation is known as innite
recursion.
Which is known as
This situation is known as innite recursion.
11 / 19
Images/cinvestav-
Outline
1 Introduction
2 Recursive Method
Questions to Answer When Designing a Recursion
3 Design guidelines for successful recursion
Examples of Code with Innite Recursion
Example
12 / 19
Images/cinvestav-
Example of code with innite recursion
Code
/∗∗ Counts down from a given positive integer .
@param integer an integer  0 ∗/
p u b l i c s t a t i c v o i d countDown ( i n t i n t e g e r )
{
System . out . p r i n t l n ( i n t e g e r ) ;
countDown ( i n t e g e r − 1 ) ;
} // end countDown
13 / 19
Images/cinvestav-
Recursive Methods That Return a Value
The nice stu about recursion
You can return values
For example
We can compute the sum 1 + 2 + 3 + ... + n
14 / 19
Images/cinvestav-
Recursive Methods That Return a Value
The nice stu about recursion
You can return values
For example
We can compute the sum 1 + 2 + 3 + ... + n
14 / 19
Images/cinvestav-
How the code will look
Partial Code
/∗∗ @param n an integer  0
@return the sum 1 + 2 + . . . + n ∗/
p u b l i c s t a t i c i n t sumOf ( i n t n )
{
i n t sum ;
// What do we put here?
r e t u r n sum ;
} // end sumOf
15 / 19
Images/cinvestav-
Debugging a recursive method
Something Notable
1 Does the method have at least one input value?
2 Does the method contain a statement that tests an input value and
leads to dierent cases?
3 Did you consider all possible cases? Does at least one of these cases
cause at least one recursive call?
4 Do these recursive calls involve smaller arguments, smaller tasks, or
tasks that get closer to the solution?
5 If these recursive calls produce or return correct results, will the
method produce or return a correct result?
6 Is at least one of the cases a base case that has no recursive call?
7 Are there enough base cases?
8 Does each base case produce a result that is correct for that case?
9 If the method returns a value, does each of the cases return a value?
16 / 19
Images/cinvestav-
Debugging a recursive method
Something Notable
1 Does the method have at least one input value?
2 Does the method contain a statement that tests an input value and
leads to dierent cases?
3 Did you consider all possible cases? Does at least one of these cases
cause at least one recursive call?
4 Do these recursive calls involve smaller arguments, smaller tasks, or
tasks that get closer to the solution?
5 If these recursive calls produce or return correct results, will the
method produce or return a correct result?
6 Is at least one of the cases a base case that has no recursive call?
7 Are there enough base cases?
8 Does each base case produce a result that is correct for that case?
9 If the method returns a value, does each of the cases return a value?
16 / 19
Images/cinvestav-
Debugging a recursive method
Something Notable
1 Does the method have at least one input value?
2 Does the method contain a statement that tests an input value and
leads to dierent cases?
3 Did you consider all possible cases? Does at least one of these cases
cause at least one recursive call?
4 Do these recursive calls involve smaller arguments, smaller tasks, or
tasks that get closer to the solution?
5 If these recursive calls produce or return correct results, will the
method produce or return a correct result?
6 Is at least one of the cases a base case that has no recursive call?
7 Are there enough base cases?
8 Does each base case produce a result that is correct for that case?
9 If the method returns a value, does each of the cases return a value?
16 / 19
Images/cinvestav-
Debugging a recursive method
Something Notable
1 Does the method have at least one input value?
2 Does the method contain a statement that tests an input value and
leads to dierent cases?
3 Did you consider all possible cases? Does at least one of these cases
cause at least one recursive call?
4 Do these recursive calls involve smaller arguments, smaller tasks, or
tasks that get closer to the solution?
5 If these recursive calls produce or return correct results, will the
method produce or return a correct result?
6 Is at least one of the cases a base case that has no recursive call?
7 Are there enough base cases?
8 Does each base case produce a result that is correct for that case?
9 If the method returns a value, does each of the cases return a value?
16 / 19
Images/cinvestav-
Debugging a recursive method
Something Notable
1 Does the method have at least one input value?
2 Does the method contain a statement that tests an input value and
leads to dierent cases?
3 Did you consider all possible cases? Does at least one of these cases
cause at least one recursive call?
4 Do these recursive calls involve smaller arguments, smaller tasks, or
tasks that get closer to the solution?
5 If these recursive calls produce or return correct results, will the
method produce or return a correct result?
6 Is at least one of the cases a base case that has no recursive call?
7 Are there enough base cases?
8 Does each base case produce a result that is correct for that case?
9 If the method returns a value, does each of the cases return a value?
16 / 19
Images/cinvestav-
Debugging a recursive method
Something Notable
1 Does the method have at least one input value?
2 Does the method contain a statement that tests an input value and
leads to dierent cases?
3 Did you consider all possible cases? Does at least one of these cases
cause at least one recursive call?
4 Do these recursive calls involve smaller arguments, smaller tasks, or
tasks that get closer to the solution?
5 If these recursive calls produce or return correct results, will the
method produce or return a correct result?
6 Is at least one of the cases a base case that has no recursive call?
7 Are there enough base cases?
8 Does each base case produce a result that is correct for that case?
9 If the method returns a value, does each of the cases return a value?
16 / 19
Images/cinvestav-
Debugging a recursive method
Something Notable
1 Does the method have at least one input value?
2 Does the method contain a statement that tests an input value and
leads to dierent cases?
3 Did you consider all possible cases? Does at least one of these cases
cause at least one recursive call?
4 Do these recursive calls involve smaller arguments, smaller tasks, or
tasks that get closer to the solution?
5 If these recursive calls produce or return correct results, will the
method produce or return a correct result?
6 Is at least one of the cases a base case that has no recursive call?
7 Are there enough base cases?
8 Does each base case produce a result that is correct for that case?
9 If the method returns a value, does each of the cases return a value?
16 / 19
Images/cinvestav-
Debugging a recursive method
Something Notable
1 Does the method have at least one input value?
2 Does the method contain a statement that tests an input value and
leads to dierent cases?
3 Did you consider all possible cases? Does at least one of these cases
cause at least one recursive call?
4 Do these recursive calls involve smaller arguments, smaller tasks, or
tasks that get closer to the solution?
5 If these recursive calls produce or return correct results, will the
method produce or return a correct result?
6 Is at least one of the cases a base case that has no recursive call?
7 Are there enough base cases?
8 Does each base case produce a result that is correct for that case?
9 If the method returns a value, does each of the cases return a value?
16 / 19
Images/cinvestav-
Debugging a recursive method
Something Notable
1 Does the method have at least one input value?
2 Does the method contain a statement that tests an input value and
leads to dierent cases?
3 Did you consider all possible cases? Does at least one of these cases
cause at least one recursive call?
4 Do these recursive calls involve smaller arguments, smaller tasks, or
tasks that get closer to the solution?
5 If these recursive calls produce or return correct results, will the
method produce or return a correct result?
6 Is at least one of the cases a base case that has no recursive call?
7 Are there enough base cases?
8 Does each base case produce a result that is correct for that case?
9 If the method returns a value, does each of the cases return a value?
16 / 19
Images/cinvestav-
Outline
1 Introduction
2 Recursive Method
Questions to Answer When Designing a Recursion
3 Design guidelines for successful recursion
Examples of Code with Innite Recursion
Example
17 / 19
Images/cinvestav-
Now, what if we solve a recursive function
What we want to solve
n choose k (combinations)
We have
n
k
=
n − 1
k
+
n − 1
k − 1
, with 1  k  n (1)
Base Case
n
1
= n (k = 1) ,
n
n
= 1(k = n) (2)
18 / 19
Images/cinvestav-
Now, what if we solve a recursive function
What we want to solve
n choose k (combinations)
We have
n
k
=
n − 1
k
+
n − 1
k − 1
, with 1  k  n (1)
Base Case
n
1
= n (k = 1) ,
n
n
= 1(k = n) (2)
18 / 19
Images/cinvestav-
Now, what if we solve a recursive function
What we want to solve
n choose k (combinations)
We have
n
k
=
n − 1
k
+
n − 1
k − 1
, with 1  k  n (1)
Base Case
n
1
= n (k = 1) ,
n
n
= 1(k = n) (2)
18 / 19
Images/cinvestav-
Base Code so you can start
Base Code
c l a s s C o m b i n a ti o n s {
p u b l i c s t a t i c i n t Co m b i n a t io n s ( i n t n , i n t k )
{
}
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
}
}
19 / 19

More Related Content

PPT
Dynamic pgmming
PDF
ADA complete notes
PDF
PDF
Design & Analysis of Algorithms Lecture Notes
PDF
Divide&Conquer & Dynamic Programming
PPTX
Introduction to dynamic programming
PPTX
Dynamic programming class 16
PDF
Lecture 2 role of algorithms in computing
Dynamic pgmming
ADA complete notes
Design & Analysis of Algorithms Lecture Notes
Divide&Conquer & Dynamic Programming
Introduction to dynamic programming
Dynamic programming class 16
Lecture 2 role of algorithms in computing

What's hot (19)

DOC
Datastructure notes
PDF
Main topic 3 problem solving and office automation
PDF
Algorithm hierarchy
PPTX
Our presentation on algorithm design
PPTX
Introduction to Dynamic Programming, Principle of Optimality
PDF
Programming with matlab session 3 notes
PPTX
Algorithmic research
PDF
Chapter 05 computer arithmetic
PPTX
Dynamic programming
PPTX
Dynamic Programming
PPTX
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
PPTX
Dynamic programming prasintation eaisy
PPTX
Dynamic Programming
PDF
The Goal and The Journey - Turning back on one year of C++14 Migration
PPT
Algorithm Design Presentation
PPTX
Dynamic programming
PPT
Tbs910 linear programming
PPTX
Chapter one
DOC
352735344 rsh-qam11-tif-10-doc
Datastructure notes
Main topic 3 problem solving and office automation
Algorithm hierarchy
Our presentation on algorithm design
Introduction to Dynamic Programming, Principle of Optimality
Programming with matlab session 3 notes
Algorithmic research
Chapter 05 computer arithmetic
Dynamic programming
Dynamic Programming
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Dynamic programming prasintation eaisy
Dynamic Programming
The Goal and The Journey - Turning back on one year of C++14 Migration
Algorithm Design Presentation
Dynamic programming
Tbs910 linear programming
Chapter one
352735344 rsh-qam11-tif-10-doc
Ad

Viewers also liked (19)

PPTX
Problem 8
PPTX
ELEMENTARY DATASTRUCTURES
DOC
Unit 4 jwfiles
PPT
Data representation UNIT-1
RTF
algorithm unit 1
DOC
algorithm Unit 2
PDF
07.2 Holland's Genetic Algorithms Schema Theorem
PPTX
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
PPT
Hashing
DOC
algorithm Unit 4
PPTX
Modified Genetic Algorithm for Solving n-Queens Problem
DOCX
Cs6402 design and analysis of algorithms impartant part b questions appasami
DOCX
CS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questio
PPTX
The n Queen Problem
PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
PPT
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
PPTX
8 queens problem using back tracking
PPT
Design and Analysis of Algorithms
Problem 8
ELEMENTARY DATASTRUCTURES
Unit 4 jwfiles
Data representation UNIT-1
algorithm unit 1
algorithm Unit 2
07.2 Holland's Genetic Algorithms Schema Theorem
Types Of Recursion in C++, Data Stuctures by DHEERAJ KATARIA
Hashing
algorithm Unit 4
Modified Genetic Algorithm for Solving n-Queens Problem
Cs6402 design and analysis of algorithms impartant part b questions appasami
CS 6402 – DESIGN AND ANALYSIS OF ALGORITHMS questio
The n Queen Problem
Cs6402 design and analysis of algorithms may june 2016 answer key
DESIGN AND ANALYSIS OF ALGORITHM (DAA)
4. Recursion - Data Structures using C++ by Varsha Patil
8 queens problem using back tracking
Design and Analysis of Algorithms
Ad

Similar to Preparation Data Structures 02 recursion (20)

PDF
Divide and Conquer Case Study
PPTX
Daa unit 1
PPT
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
PPT
Lecture 7
PPT
algo_vc_lecture8.ppt
PPTX
Approximation algorithms
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
PPT
Knightstour
PPT
Design and Analysis of algorithms unit 1 notes
PPT
Analysis of Algorithm
PDF
Introduction to Algorithm Design and Analysis.pdf
PPTX
Dynamic Programming: Smith-Waterman
PDF
N Queens problem
PDF
From jUnit to Mutationtesting
PPTX
3. CPT121 - Introduction to Problem Solving - Module 1 - Unit 3.pptx
PPTX
Simplex Algorithm
PDF
Tips & tricks for Quantitative Aptitude
PDF
DATA STRUCTURE
Divide and Conquer Case Study
Daa unit 1
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
Lecture 7
algo_vc_lecture8.ppt
Approximation algorithms
dynamic programming complete by Mumtaz Ali (03154103173)
Knightstour
Design and Analysis of algorithms unit 1 notes
Analysis of Algorithm
Introduction to Algorithm Design and Analysis.pdf
Dynamic Programming: Smith-Waterman
N Queens problem
From jUnit to Mutationtesting
3. CPT121 - Introduction to Problem Solving - Module 1 - Unit 3.pptx
Simplex Algorithm
Tips & tricks for Quantitative Aptitude
DATA STRUCTURE

More from Andres Mendez-Vazquez (20)

PDF
2.03 bayesian estimation
PDF
05 linear transformations
PDF
01.04 orthonormal basis_eigen_vectors
PDF
01.03 squared matrices_and_other_issues
PDF
01.02 linear equations
PDF
01.01 vector spaces
PDF
06 recurrent neural_networks
PDF
05 backpropagation automatic_differentiation
PDF
Zetta global
PDF
01 Introduction to Neural Networks and Deep Learning
PDF
25 introduction reinforcement_learning
PDF
Neural Networks and Deep Learning Syllabus
PDF
Introduction to artificial_intelligence_syllabus
PDF
Ideas 09 22_2018
PDF
Ideas about a Bachelor in Machine Learning/Data Sciences
PDF
Analysis of Algorithms Syllabus
PDF
20 k-means, k-center, k-meoids and variations
PDF
18.1 combining models
PDF
17 vapnik chervonenkis dimension
PDF
A basic introduction to learning
2.03 bayesian estimation
05 linear transformations
01.04 orthonormal basis_eigen_vectors
01.03 squared matrices_and_other_issues
01.02 linear equations
01.01 vector spaces
06 recurrent neural_networks
05 backpropagation automatic_differentiation
Zetta global
01 Introduction to Neural Networks and Deep Learning
25 introduction reinforcement_learning
Neural Networks and Deep Learning Syllabus
Introduction to artificial_intelligence_syllabus
Ideas 09 22_2018
Ideas about a Bachelor in Machine Learning/Data Sciences
Analysis of Algorithms Syllabus
20 k-means, k-center, k-meoids and variations
18.1 combining models
17 vapnik chervonenkis dimension
A basic introduction to learning

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Classroom Observation Tools for Teachers
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Lesson notes of climatology university.
PPTX
Cell Types and Its function , kingdom of life
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial disease of the cardiovascular and lymphatic systems
Final Presentation General Medicine 03-08-2024.pptx
Complications of Minimal Access Surgery at WLH
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Classroom Observation Tools for Teachers
102 student loan defaulters named and shamed – Is someone you know on the list?
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
STATICS OF THE RIGID BODIES Hibbelers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
GDM (1) (1).pptx small presentation for students
Abdominal Access Techniques with Prof. Dr. R K Mishra
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Lesson notes of climatology university.
Cell Types and Its function , kingdom of life
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

Preparation Data Structures 02 recursion

  • 2. Images/cinvestav- Outline 1 Introduction 2 Recursive Method Questions to Answer When Designing a Recursion 3 Design guidelines for successful recursion Examples of Code with Innite Recursion Example 2 / 19
  • 3. Images/cinvestav- What is recursion? Fact Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem rst Example You can build a house by hiring a contractor. The contractor in turn hires several subcontractors to complete portions of the house. Each subcontractor might hire other subcontractors to help. 3 / 19
  • 4. Images/cinvestav- What is recursion? Fact Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem rst Example You can build a house by hiring a contractor. The contractor in turn hires several subcontractors to complete portions of the house. Each subcontractor might hire other subcontractors to help. 3 / 19
  • 5. Images/cinvestav- Another Example Example: Principal Idea You do your part until your friend nishes and tells you 4 / 19
  • 6. Images/cinvestav- Recursive Method Denition A method that calls itself is a recursive method. The invocation is a recursive call or recursive invocation. The previous example can be seen in code as follow 5 / 19
  • 7. Images/cinvestav- Recursive Method Denition A method that calls itself is a recursive method. The invocation is a recursive call or recursive invocation. The previous example can be seen in code as follow /∗∗ Counts down from a given positive integer . @param integer an integer 0 ∗/ p u b l i c s t a t i c v o i d countDown ( i n t i n t e g e r ) { System . out . p r i n t l n ( i n t e g e r ) ; i f ( i n t e g e r 1) countDown ( i n t e g e r − 1 ) ; } // end countDown 5 / 19
  • 8. Images/cinvestav- Example with 3 Tracing the recursive call countDown(3) //Client public static void main(...) { countDown(3); ... } public static void countDown(3) { System.out.println(3); countDown(3 – 1); } if (3 1) public static void countDown(2) { System.out.println(2); countDown(2 – 1); } if (2 1) public static void countDown(1) { System.out.println(1); } if (1 1) 6 / 19
  • 9. Images/cinvestav- Outline 1 Introduction 2 Recursive Method Questions to Answer When Designing a Recursion 3 Design guidelines for successful recursion Examples of Code with Innite Recursion Example 7 / 19
  • 10. Images/cinvestav- Questions to answer when designing a recursive solution First What part of the solution can you contribute directly? Second What smaller but identical problem has a solution that, when taken with your contribution, provides the solution to the original problem? Third When does the process end? That is, what smaller but identical problem has a known solution, and have you reached this problem, or base case? 8 / 19
  • 11. Images/cinvestav- Questions to answer when designing a recursive solution First What part of the solution can you contribute directly? Second What smaller but identical problem has a solution that, when taken with your contribution, provides the solution to the original problem? Third When does the process end? That is, what smaller but identical problem has a known solution, and have you reached this problem, or base case? 8 / 19
  • 12. Images/cinvestav- Questions to answer when designing a recursive solution First What part of the solution can you contribute directly? Second What smaller but identical problem has a solution that, when taken with your contribution, provides the solution to the original problem? Third When does the process end? That is, what smaller but identical problem has a known solution, and have you reached this problem, or base case? 8 / 19
  • 13. Images/cinvestav- For countDown Answer 1 The method countDown displays the given integer rst. Answer 2 The smaller problem is counting down from integer - 1. Answer 3 The if statement asks if the process has reached the base case. In our case integer = 1. 9 / 19
  • 14. Images/cinvestav- For countDown Answer 1 The method countDown displays the given integer rst. Answer 2 The smaller problem is counting down from integer - 1. Answer 3 The if statement asks if the process has reached the base case. In our case integer = 1. 9 / 19
  • 15. Images/cinvestav- For countDown Answer 1 The method countDown displays the given integer rst. Answer 2 The smaller problem is counting down from integer - 1. Answer 3 The if statement asks if the process has reached the base case. In our case integer = 1. 9 / 19
  • 16. Images/cinvestav- Design guidelines for successful recursion First The method must be given an input value. Second The method denition must contain logic that involves this input value and leads to dierent cases. Third One or more of these cases should provide a solution that does not require recursion. These are the base cases, or stopping cases. Fourth One or more cases must include a recursive invocation of the method using a smaller argument. 10 / 19
  • 17. Images/cinvestav- Design guidelines for successful recursion First The method must be given an input value. Second The method denition must contain logic that involves this input value and leads to dierent cases. Third One or more of these cases should provide a solution that does not require recursion. These are the base cases, or stopping cases. Fourth One or more cases must include a recursive invocation of the method using a smaller argument. 10 / 19
  • 18. Images/cinvestav- Design guidelines for successful recursion First The method must be given an input value. Second The method denition must contain logic that involves this input value and leads to dierent cases. Third One or more of these cases should provide a solution that does not require recursion. These are the base cases, or stopping cases. Fourth One or more cases must include a recursive invocation of the method using a smaller argument. 10 / 19
  • 19. Images/cinvestav- Design guidelines for successful recursion First The method must be given an input value. Second The method denition must contain logic that involves this input value and leads to dierent cases. Third One or more of these cases should provide a solution that does not require recursion. These are the base cases, or stopping cases. Fourth One or more cases must include a recursive invocation of the method using a smaller argument. 10 / 19
  • 20. Images/cinvestav- So, be careful Innite recursion A recursive method that does not check for a base case, or that misses the base case, will execute forever. This situation is known as innite recursion. Which is known as This situation is known as innite recursion. 11 / 19
  • 21. Images/cinvestav- So, be careful Innite recursion A recursive method that does not check for a base case, or that misses the base case, will execute forever. This situation is known as innite recursion. Which is known as This situation is known as innite recursion. 11 / 19
  • 22. Images/cinvestav- Outline 1 Introduction 2 Recursive Method Questions to Answer When Designing a Recursion 3 Design guidelines for successful recursion Examples of Code with Innite Recursion Example 12 / 19
  • 23. Images/cinvestav- Example of code with innite recursion Code /∗∗ Counts down from a given positive integer . @param integer an integer 0 ∗/ p u b l i c s t a t i c v o i d countDown ( i n t i n t e g e r ) { System . out . p r i n t l n ( i n t e g e r ) ; countDown ( i n t e g e r − 1 ) ; } // end countDown 13 / 19
  • 24. Images/cinvestav- Recursive Methods That Return a Value The nice stu about recursion You can return values For example We can compute the sum 1 + 2 + 3 + ... + n 14 / 19
  • 25. Images/cinvestav- Recursive Methods That Return a Value The nice stu about recursion You can return values For example We can compute the sum 1 + 2 + 3 + ... + n 14 / 19
  • 26. Images/cinvestav- How the code will look Partial Code /∗∗ @param n an integer 0 @return the sum 1 + 2 + . . . + n ∗/ p u b l i c s t a t i c i n t sumOf ( i n t n ) { i n t sum ; // What do we put here? r e t u r n sum ; } // end sumOf 15 / 19
  • 27. Images/cinvestav- Debugging a recursive method Something Notable 1 Does the method have at least one input value? 2 Does the method contain a statement that tests an input value and leads to dierent cases? 3 Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? 4 Do these recursive calls involve smaller arguments, smaller tasks, or tasks that get closer to the solution? 5 If these recursive calls produce or return correct results, will the method produce or return a correct result? 6 Is at least one of the cases a base case that has no recursive call? 7 Are there enough base cases? 8 Does each base case produce a result that is correct for that case? 9 If the method returns a value, does each of the cases return a value? 16 / 19
  • 28. Images/cinvestav- Debugging a recursive method Something Notable 1 Does the method have at least one input value? 2 Does the method contain a statement that tests an input value and leads to dierent cases? 3 Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? 4 Do these recursive calls involve smaller arguments, smaller tasks, or tasks that get closer to the solution? 5 If these recursive calls produce or return correct results, will the method produce or return a correct result? 6 Is at least one of the cases a base case that has no recursive call? 7 Are there enough base cases? 8 Does each base case produce a result that is correct for that case? 9 If the method returns a value, does each of the cases return a value? 16 / 19
  • 29. Images/cinvestav- Debugging a recursive method Something Notable 1 Does the method have at least one input value? 2 Does the method contain a statement that tests an input value and leads to dierent cases? 3 Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? 4 Do these recursive calls involve smaller arguments, smaller tasks, or tasks that get closer to the solution? 5 If these recursive calls produce or return correct results, will the method produce or return a correct result? 6 Is at least one of the cases a base case that has no recursive call? 7 Are there enough base cases? 8 Does each base case produce a result that is correct for that case? 9 If the method returns a value, does each of the cases return a value? 16 / 19
  • 30. Images/cinvestav- Debugging a recursive method Something Notable 1 Does the method have at least one input value? 2 Does the method contain a statement that tests an input value and leads to dierent cases? 3 Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? 4 Do these recursive calls involve smaller arguments, smaller tasks, or tasks that get closer to the solution? 5 If these recursive calls produce or return correct results, will the method produce or return a correct result? 6 Is at least one of the cases a base case that has no recursive call? 7 Are there enough base cases? 8 Does each base case produce a result that is correct for that case? 9 If the method returns a value, does each of the cases return a value? 16 / 19
  • 31. Images/cinvestav- Debugging a recursive method Something Notable 1 Does the method have at least one input value? 2 Does the method contain a statement that tests an input value and leads to dierent cases? 3 Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? 4 Do these recursive calls involve smaller arguments, smaller tasks, or tasks that get closer to the solution? 5 If these recursive calls produce or return correct results, will the method produce or return a correct result? 6 Is at least one of the cases a base case that has no recursive call? 7 Are there enough base cases? 8 Does each base case produce a result that is correct for that case? 9 If the method returns a value, does each of the cases return a value? 16 / 19
  • 32. Images/cinvestav- Debugging a recursive method Something Notable 1 Does the method have at least one input value? 2 Does the method contain a statement that tests an input value and leads to dierent cases? 3 Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? 4 Do these recursive calls involve smaller arguments, smaller tasks, or tasks that get closer to the solution? 5 If these recursive calls produce or return correct results, will the method produce or return a correct result? 6 Is at least one of the cases a base case that has no recursive call? 7 Are there enough base cases? 8 Does each base case produce a result that is correct for that case? 9 If the method returns a value, does each of the cases return a value? 16 / 19
  • 33. Images/cinvestav- Debugging a recursive method Something Notable 1 Does the method have at least one input value? 2 Does the method contain a statement that tests an input value and leads to dierent cases? 3 Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? 4 Do these recursive calls involve smaller arguments, smaller tasks, or tasks that get closer to the solution? 5 If these recursive calls produce or return correct results, will the method produce or return a correct result? 6 Is at least one of the cases a base case that has no recursive call? 7 Are there enough base cases? 8 Does each base case produce a result that is correct for that case? 9 If the method returns a value, does each of the cases return a value? 16 / 19
  • 34. Images/cinvestav- Debugging a recursive method Something Notable 1 Does the method have at least one input value? 2 Does the method contain a statement that tests an input value and leads to dierent cases? 3 Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? 4 Do these recursive calls involve smaller arguments, smaller tasks, or tasks that get closer to the solution? 5 If these recursive calls produce or return correct results, will the method produce or return a correct result? 6 Is at least one of the cases a base case that has no recursive call? 7 Are there enough base cases? 8 Does each base case produce a result that is correct for that case? 9 If the method returns a value, does each of the cases return a value? 16 / 19
  • 35. Images/cinvestav- Debugging a recursive method Something Notable 1 Does the method have at least one input value? 2 Does the method contain a statement that tests an input value and leads to dierent cases? 3 Did you consider all possible cases? Does at least one of these cases cause at least one recursive call? 4 Do these recursive calls involve smaller arguments, smaller tasks, or tasks that get closer to the solution? 5 If these recursive calls produce or return correct results, will the method produce or return a correct result? 6 Is at least one of the cases a base case that has no recursive call? 7 Are there enough base cases? 8 Does each base case produce a result that is correct for that case? 9 If the method returns a value, does each of the cases return a value? 16 / 19
  • 36. Images/cinvestav- Outline 1 Introduction 2 Recursive Method Questions to Answer When Designing a Recursion 3 Design guidelines for successful recursion Examples of Code with Innite Recursion Example 17 / 19
  • 37. Images/cinvestav- Now, what if we solve a recursive function What we want to solve n choose k (combinations) We have n k = n − 1 k + n − 1 k − 1 , with 1 k n (1) Base Case n 1 = n (k = 1) , n n = 1(k = n) (2) 18 / 19
  • 38. Images/cinvestav- Now, what if we solve a recursive function What we want to solve n choose k (combinations) We have n k = n − 1 k + n − 1 k − 1 , with 1 k n (1) Base Case n 1 = n (k = 1) , n n = 1(k = n) (2) 18 / 19
  • 39. Images/cinvestav- Now, what if we solve a recursive function What we want to solve n choose k (combinations) We have n k = n − 1 k + n − 1 k − 1 , with 1 k n (1) Base Case n 1 = n (k = 1) , n n = 1(k = n) (2) 18 / 19
  • 40. Images/cinvestav- Base Code so you can start Base Code c l a s s C o m b i n a ti o n s { p u b l i c s t a t i c i n t Co m b i n a t io n s ( i n t n , i n t k ) { } p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { } } 19 / 19