CHAPITRE IV:
RCURSIVIT
Mme AROUSSI
2016-2017
Dfinition
Types de la rcursivit
3
EXEMPLE
Exemple : la factorielle n!
Itratif Rcursif
n! = 1 * 2 * ... * n n! = n * (n-1)!
Dbut Dbut
FF*i;
retourner F;
4
Fin
VOLUTION DUN APPEL RCURSIF
L'excution d'un appel rcursif passe par deux phases, la
phase de descente et la phase de la remonte :
Dans la phase de descente, chaque appel rcursif fait son tour
un appel rcursif.
Facto (n: entier): entier
Dbut
retourner n*Facto (n-1);
Facto(4) 4 * Facto(3)
Fin
Facto(3) 3 * Facto(2)
Facto(2) 2 * Facto(1)
Facto(1) 1 * Facto(0)
5
Facto(4) 0 * Facto(-1)
VOLUTION DUN APPEL RCURSIF
CONDITION DARRT
Puisqu'un algorithme rcursif s'appelle lui-mme, il est
impratif qu'on prvoit une condition d'arrt la
rcursion, sinon le programme ne s'arrte jamais!
Exemple 1: la factorielle n!
Version prcdente Version correcte
24
Phase de la remonte
Facto(4) 4 * Facto(3)
6
Facto(3) 3 * Facto(2)
1
2 7
Facto(2) 2 * Facto(1)
EXERCICE 1
1. Som (n) calcule la somme des n premiers naturels
8. Fib (n) calcule la suite de Fibonacci (Un = Un-1 + Un-2 si n > 1, sinon
8
U0 = U1 = 1)
EXERCICE 1
1. La somme des n premiers
Itratif Rcursif
Dbut Dbut
Si b = 0 alors Si a = 0 alors
retourner (a) retourner (b)
Sinon Sinon
retourner (Som(a, b-1) +1) retourner (Som(a-1, b) +1)
Fin Fin
10
EXERCICE 1
3. Le produit de deux entiers a et b
Dbut Dbut
Si a = 0 ou b = 0 alors Si a = 0 ou b = 0 alors
retourner (0) retourner (0)
Sinon Sinon
retourner (Prod(a, b-1) +a) retourner (Prod(a-1, b) +b)
Fin Fin
11
EXERCICE 1
4. La puissance ab (a et b deux entiers positifs)
itratif Rcursif
Dbut Dbut
P1; Si b = 0 alors
retourner (1)
Pour i1 b faire
Sinon
PP*a; retourner (Puiss(a, b-1) *a)
retourner P; Fin
12
Fin
EXERCICE 1
5. Le quotient et le reste de la division de a par b (a et b
deux entiers positifs)
Dbut Dbut
Si ab alors Si ab alors
retourner (0) retourner (a)
Sinon Sinon
retourner (Quotient(a-b, b) +1) retourner (Reste(a-b, b))
13
Fin Fin
EXERCICE 1
7. Le Plus Grand Commun Diviseur entre a et b (a et b
deux entiers positifs)
Dbut
Si a=b alors retourner (a)
Sinon
Si a b alors retourner (PGCD(a, b-a))
Sinon retourner (PGCD(a-b, b))
14
Fin
EXERCICE 1
8. La suite de Fibonacci
Dbut
Si n = 0 ou n = 1 alors retourner (1)
Sinon
retourner (Fib (n-1)+Fib(n-2))
Fin
15
RCURSIVIT TERMINALE VS NON TERMINALE
Un module rcursif est dit terminal si aucun
traitement n'est effectu la remonte d'un appel
rcursif (sauf le retour du module).
Reste de la division
Reste(13,4)
a mod b = (a-b) mod b
Factoriel
Fact(4)
n! = n * (n-1)!
resultat 1
Procdure Facto ( n: entier, Var resultat: entier)
Dbut
Si (n > 1) alors
Facto (n-1, n * resultat);
Fin
Phase de descente
Phase de la remonte 21
RCURSIVIT TERMINALE VS NON TERMINALE
resultat0
Som (n:entier):entier Som (n: entier, var resultat:entier)
Si n = 0 alors retourner (0) Si n 0 alors
Sinon retourner (Som(n-1) +n) Som(n-1, resultat +n))
resultata
Som (a,b:entier):entier Som (a,b: entier, var
Si b = 0 alors retourner (a) resultat:entier)
Sinon retourner ( Som(a, b-1) +1) Si b 0 alors
Som(a, b-1, resultat+1)
resultat0
Prod (a,b:entier):entier Prod ( a,b;: entier, var
Si a = 0 ou b = 0 alors retourner (0) resultat:entier)
Sinon retourner (Prod(a, b-1) +a) Si a 0 et b 0 alors
Prod(a, b-1, resultat +a)) 22
RCURSIVIT TERMINALE VS NON TERMINALE
Fonction non terminale Procdure terminale
resultat1
Puissance ( a, b:entier):entier Puissance (a, b: entier, var
Si b = 0 alors retourner (1) resultat:entier)
Sinon retourner (Puiss(a, b-1) *a) Si b 0 alors
Puiss(a, b-1, resultat *a))
resultat0
Quotient( a,b:entier):entier Quotient ( a,b: entier, var
Si ab alors retourner (0) resultat:entier)
Sinon Si ab alors
retourner (Quotient(a-b, b) +1)) Quotient(a-b, b, resultat +1))
Fib (n:entier): entier
Si n = 0 ou n = 1 alors retourner (1)
Sinon retourner ( Fib (n-1)+Fib(n-2)) IMPOSSIBLE
Comb (p, n: entier): entier
Si p = 0 ou p = n alors retourner (1) 23
Sinon
retourner (Comb(p,n-1)+Comb(p-1, n-1))
TYPES DE RCURSIVIT
27
TYPES DE RCURSIVIT
28
EXERCICE 2
Soit Tab un tableau dentiers de taille n sur lequel on
veut raliser les oprations suivantes :
5 8 1 10
Diviser
5 8 1 10
Rgner
5 8 1 10
8 10
32
Combiner 10
EXERCICE 2
Soit Tab un tableau dentiers de taille n sur lequel on
veut raliser les oprations suivantes :
Sinon
7 3 18 13 7
7 3 18 13 7
13 7
3 7
7 13
7 13 18
35
3 7 7 13 18
EXERCICE 2 : RECHERCHE DICHOTOMIQUE
Soit Tab un tableau tri (ordre croissant) n lments.
La recherche par dichotomie compare llment rechercher x avec
llment en position m situ au milieu du sous-tableau :
si Tab[m] = x : on a trouv llment en position m
si Tab[m] > x : il est impossible que x se trouve aprs la position
m dans le tableau. Il reste traiter uniquement la moiti
infrieure du tableau
De mme si Tab[m] < x : il reste traiter uniquement la moiti
suprieure du tableau
On continue ainsi la recherche jusqu trouver llment ou bien
aboutir sur un tableau de taille nulle, x n'est pas prsent et la
recherche s'arrte.
36
EXERCICE 2 : RECHERCHE DICHOTOMIQUE
Si (borneinf<=bornesup) alors
Sinon
Si (Tab[mil]>x) Alors
Sinon
Sinon 37
Retourner (Faux)
EXERCICE 2 : TRI PAR FUSION
PARADIGME DIVISER POUR RGNER
38
EXERCICE 2 : TRI PAR FUSION
Tri_Fusion (T: tableau, debut, fin : entier)
Debut
Si (debut<fin) alors
FSI 39
Fin
EXERCICE 2 : TRI PAR FUSION
PROCDURE INTERCLASSER
Procdure Interclasser(T: tableau, debut, milieu, fin: entier)
Debut
Tmp: tableau temporaire du taille fin-debut+1
i 0; i1 debut, i2 milieu + 1;
Tant que (i1milieu) et (i2 fin) faire
Si (T[i1]<T[i2]) alors Tmp[i]T[i1]; i1++;
Sinon Tmp [i]T[i2]; i2++;
i++;
Tant que (i1milieu) faire Tmp[i]T[i1]; i1++; i++;
Tant que (i2fin) faire Tmp[i]T[i2]; i2++; i++;
Pour idebut fin faire T[i]=tmp[i-debut]; // recopier le tableau 40
Fin
CONCEPTION DUN ALGORITHME RCURSIF
42
COMPLEXIT DES ALGORITHMES RCURSIFS
Pour calculer la solution gnrale de cette quation, on
peut procder par substitution :
43
O (T) = O (n)
COMPLEXIT DES ALGORITHMES RCURSIFS
THORME 1 DE RSOLUTION DE LA RCURRENCE
Exemple : Factorielle
44
O (T) = O (n)
COMPLEXIT DES ALGORITHMES RCURSIFS
THORME 1 DE RSOLUTION DE LA RCURRENCE
O (T) = O(2n) 45
COMPLEXIT DES ALGORITHMES DIVISER POUR RGNER
47
COMPLEXIT DES ALGORITHMES DIVISER POUR RGNER
THORME 2 DE RSOLUTION DE LA RCURRENCE
48
EXERCICE 2 : RECHERCHE MAXIMUM
Sinon
T(n) = 2 T(n/2) + c
a = 2 , b = 2, k = 0 a > bk
T(n) = O(n) 50
COMPLEXIT DES ALGORITHMES DIVISER POUR RGNER
THORME 2 DE RSOLUTION DE LA RCURRENCE
51
EXERCICE 2 : RECHERCHE DICHOTOMIQUE
Si (borneinf<=bornesup) alors
Sinon
Si (Tab[mil]>x) Alors
Sinon
Sinon 52
Retourner (Faux)
COMPLEXIT DES ALGORITHMES DIVISER POUR RGNER
THORME 2 DE RSOLUTION DE LA RCURRENCE
T(n) = T(n/2) + c
a = 1 , b = 2, k = 0 a = bk
T(n) = O(log(n))
53
COMPLEXIT DES ALGORITHMES DIVISER POUR RGNER
THORME 2 DE RSOLUTION DE LA RCURRENCE
54
EXERCICE 2 : TRI PAR FUSION
Tri_Fusion (T: tableau, debut, fin : entier)
Debut
Si (debut<fin) alors
FSI 55
Fin
EXERCICE 2 : TRI PAR FUSION
PROCDURE INTERCLASSER
Procdure Interclasser(T: tableau, debut, milieu, fin: entier)
Debut
Tmp: tableau temporaire du taille fin-debut+1
i 0; i1 debut, i2 milieu + 1;
Tant que (i1milieu) et (i2 fin) faire
Si (T[i1]<T[i2]) alors Tmp[i]T[i1]; i1++;
Sinon Tmp [i]T[i2]; i2++;
i++;
Tant que (i1milieu) faire Tmp[i]T[i1]; i1++; i++;
Tant que (i2fin) faire Tmp[i]T[i2]; i2++; i++;
Pour idebut fin faire T[i]=tmp[i-debut]; // recopier le tableau 56
Fin
COMPLEXIT DES ALGORITHMES DIVISER POUR RGNER
THORME 2 DE RSOLUTION DE LA RCURRENCE
T(n) = 2 T(n/2) + n
a = 2 , b = 2, k = 1 a = bk
58
COMPLEXIT DES ALGORITHMES DIVISER POUR RGNER
THORME 2 DE RSOLUTION DE LA RCURRENCE
59
COMPLEXIT DES ALGORITHMES DIVISER POUR RGNER
THORME 3 DE RSOLUTIONS DE RCURRENCE
61
SOURCES DE CE COURS
Frdric Vivien, Algorithmique avance, cole Normale Suprieure de Lyon, 2002.,
pp. 93. Disponible sur http://perso.ens-lyon.fr/frederic.vivien/Enseignement/Algo-
2001-2002/Cours.pdf
62