Professional Documents
Culture Documents
Unit 5 Fun Pro I
Unit 5 Fun Pro I
Unit 5 Fun Pro I
Recursion
• Scope
5.1. Introduction
5.2. Principle of mathematical induction
5.3. Elemental recursion
5.3.1. Inductive and base cases
5.3.2. Execution of recursion
5.3.3. Types of recursion
Programming Fundamentals I
Unit 5. Recursion
• Recommende bibliography
- Muñoz Caro C., Niño A. y Vizcaíno Barceló A. Introducción a la
programación con orientación a objetos. Chapter 6. Prentice-
Hall, 2002. Reimpresión 2007
Programming Fundamentals I
5.1. Introduction
• Recursion is a powerful technique where a method is partially
defined itself.
Programming Fundamentals I
5.2. Principle of Induction (I)
Programming Fundamentals I
5.2. Principle of Induction (II)
• Example: Let the following theorem be:
For each integer N≥1, the addition of the first N integers given by:
N
å i =1 + 2 + 3 + ... + N
i =1
is equal to N(N+1)/2.
• Demonstration
a) Inductive hypothesis. We demonstrate that if the theorem is valid
for N then is valid for N+1:
N +1 N
å i = å i + ( N + 1 ) = N ( N + 1 ) / 2 + ( N + 1 ) = ( N + 1 )( N + 2 ) / 2
i =1 i =1
Q. E. D.
Programming Fundamentals I
5.2. Principle of Induction (III)
• Demonstration
b) Check if the theorem is valid for the first value, N=1:
1
We have:
å i =1
i =1
1
with the formula:
å i = N ( N + 1 ) / 2 = 1( 1 + 1 ) / 2 = 1
i =1
is valid for 1 and, therefore, for 2 y, therefore, for 3 and so on …
Programming Fundamentals I
5.3. Elemental recursion
• In programming recursion is implemented as a method which
calls itself.
Programming Fundamentals I
5.3.1. Base and Inductive Cases (I)
• Recursion is related to the principle of induction. There exists a
base case, where inductive calls are not executed. On the hand,
there exists an inductive case (also called recursive or general
case), where different versions of the method are called. These
versions receive simpler parameters which address to the base
case. Hence, in a recursive method:
Programming Fundamentals I
5.3.1. Base and Inductive Cases (II)
• To create a recursive method is necessary a recursive
definition of the problem.
åi
i =1
• Program:
Programming Fundamentals I
5.3.1. Base and Inductive Cases (III)
import java.util.*;
class Recursion {
public static void main(String [] args) {
int n, sum;
Scanner leer =new Scanner(System.in);
n=leer.nextInt ();
if (n>0) {
sum= s(n);
System.out.println(“Addition of the first "+n+" integers:“+ sum);
}
else {
System.out.println(“The number cannot be < 1");
}
} // End main
Programming Fundamentals I
5.3.1. Base and Inductive Cases (IV)
public static int s(int n) {
int value;
if (n==1) // base case
value = 1;
else
value = s(n-1)+n; // inductive case
return value;
}
}
Programming Fundamentals I
5.3.2. Execution of recursion (I)
• How does recursion work? Let’s see an example
Programming Fundamentals I
5.3.2. Execution of recursion (II)
• Program:
import java.util.*;
class RecursiveFactorial{
public static void main(String [ ] args) {
int n;
System.out.println(“Computing recursive factorial\n");
Scanner leer =new Scanner(System.in);
n=leer.nextInt ();
if (n >=0) {
System.out.println(“The factorial of "+n +" is:");
System.out.println(factorial(n));
}
else
System.out.println(“It cannot be computed if n<0");
} // End main
Programming Fundamentals I
5.3.2. Execution of recursion (III)
public static long factorial(int n) {
long nFact;
if (n==0){
nFact = 1; Base case
}
else{
nFact = factorial(n-1)*n; Inductive case
}
return nFact;
} //End class
Programming Fundamentals I
5.3.2. Execution of recursion (IV)
main public static long factorial(int n) {
long nFact;
n=3 if (n==0){
nFact = 1;
Returns 2*3=6 }
factorial else{
nFact = factorial(n-1)*n;
n=2 }
return nFact;
factorial Returns 1*2=2
}
n=1
n=0
Programming Fundamentals I
5.3.2. Execution of recursion (V)
• En forma de tabla
Forward Backward
Call n Value
1ª 3 6 Result
2ª 2 2
3ª 1 1
4ª Base case 0 1
Programming Fundamentals I
5.3.2. Execution of recursion (VI)
• General expression of a recursive algorithm:
Begin
Receive the value of the variable
Return result
End
Programming Fundamentals I
5.3.2. Execution of recursion (VII)
• VERY Important:
Programming Fundamentals I
5.3.2. Execution of recursion (VIII)
• Comparision between recursion and iteration:
– Analogies:
• Both involve repetition.
Programming Fundamentals I
5.3.2. Execution of recursion (IX)
• Comparision between recursion and iteration:
– Differences:
• Recursion has a disadvantage: the method is being
constantly called. This fact involves an extra cost in terms of
computational time and memory, because new variables are
defined for each invokation. The difference in terms of
efficiency depends on how grows the stack of data in the
recursive case.
Programming Fundamentals I
5.3.3. Types of recursion (I)
• The most simple classification is:
Programming Fundamentals I
5.3.3. Types of recursion (II)
• Illustration of indirect recursion:
ida
método 1 método 2 Llamada recursiva
vuelta
ida vuelta
ida
método 1 método 2 Llamada recursiva
vuelta
ida vuelta
ida
método 1 método 2 Caso
base
vuelta