Here are my slides for my preparation class for possible Master students in Electrical Engineering and Computer Science (Specialization in Computer Science)... for the entrance examination here at Cinvestav GDL
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
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
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
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
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