ch02 Recursion
ch02 Recursion
3
Plan
• Introduction
• Example: factorial Function
• Analyse of the recursion method
• Classical Problems
• Exe rcises
4
Introdu ction
• An algorithm uses one of two methods Iterative method, also called
iterative function based on loops and terminate its execution when
exiting the loop.
• Recursive method, also called recursive function This is when a
function calls itself - One or more times to accomplish a given task.
• Motivation: Recursive functions simplify the implementation of
algorithms They provide an elegant and comprehensive
representation Iterative functions are more efficient in terms of
performance A recursive function can always be implemented in
iterative form. 5
Example: Factorial Function
• Write an iterative function and a recursive function to calculate the
factorial of a number.
• General Form:
4! =4 x 3 x 2 x 1 n ! =1 si n =0
= 4 x (3 x 2 x 1)
n ! =n * (n-1) ! if n > O
=4 x 3 !
n I is defined in terms b y i t s e l f
Given f( n)=n I and f( n - l )=(n-1) I Alo rs f( n)=n*f(n-1)
6
Example: factorial function
#include <iostream>
Using namespace std;
double factorialiter (int n ) { //iterative function
double facto=l;
for (int i=l;i<=n;i++)
facto=i*facto;
return facto;
} Output
double factorial (int n ){ //recursive function
if (n <= 1) // cas de base
O! =1
1! 1
return 1;
return n*factorial (n-1) ; I I cas recursif
2! =2
3! 6
} 4! 24
int main (){ 5 ! = 120
int n;
6 ! = 720
for (int n=O ; n<=lO ; n++) 7! 5040
cout<<n<< " ! "<< factorial (n )<<endl;
8 ! = 40320
return 0 ;
9! 362880
} 10 ! = 3628800
Example: factorial function
Representation of recursive calls
Recursive calls
1 2 3 4
factoria l (4) factoria l(3) factoria l(2) factoria l(l )
n=4
4*factorial(3); n = 3;
)
3*factorial (2); n = 2·I
)
2*factorial (1); n = 1·I
-
....
1 return
-
. J
. J
2 return 2*1; 1;
'
. J
6 return 3*2;
'
- 24 return 4*6;
8
Example: factorial function
Representation of recursive
calls
4! 4! final value = 24
4 * 3! 4 * 3!
J'
I I
Returns 3 !=3* 2=6
3 * 2! 3 * 2!
'\
Returns 2!=2*1=2
' I
2 * 1! 2 * 1!
II
1 1
calls e.g.
terms of itself .
• Find the base case(s) for which the solution(s) is known which
12
Power Fonction
13
Iterative and recursive power
function
double poweriter (int x, int n) { double
pow=l;
for (int i=l;i<=n;i++) pow=x*pow;
return pow;
}
//x"n=x*x"(n-1)
double power (int x, int n){ if (n == 0)
return 1;
return x*power (x,n-1);
}
14
Power function
Representation of recursive calls
Appels recursifs
1 2 3 4 5
return 3*3 = 9
return
return 3*9
J
= 27
'
3*27 = 81·
J
15
Power function Recursive
Call representation
34 34 final value = 81
', J\
3* 33 3* 33
'I
'\
3* 32 3* 32 Return 33=3*32=27
'I
I\
3* 31 3* 31 Return 32=3*3=9
'
\
J'
', Return 1
3 1
1
7
Binary search function
19
Towers of Hanoi
A B c
Move t
disk 1 from A to B
h
e
Move t
disk 2 from A to C
h 22
EXERCISES
2
3
Exercise1 : Write an iterative function and a
Recursive function that calculates
Sn=tab[0]+tab[l]+...+tab[n- 1] where n is the
number of elements.
Exercise 2: Write a function that displays the
binary equivalent of a decimal number
Exercise 3: Write a recursive function that
displays an array of n characters
2
4
Exercise 4: Write a recursive function that prints upside
down an array of n characters.
Exercise 5: Write a recursive function that determines the
maximum of the elements in an array b of n integers.
Exercise 6: Write a recursive function that calculates the nth
element of the Fibonacci series Fib(n)=n if n ≤ 1
Fib(n)=fib(n-1)+fib(n-2)
2
5