SlideShare une entreprise Scribd logo
1  sur  24
Télécharger pour lire hors ligne
Chapitre 2
La Récursivité
1
1. Définition
• Une procédure (ou une fonction) est dite récursive si elle comporte un appel à elle
même. Une procédure non récursive ne peut comporter que des appels à d’autres
procédures.
Exemple : On peut définir le factoriel d'un nombre «n» positif de deux manières:
▪ définition non récursive (ou itérative)
▪ définition récursive
n ! = n * (n-1)!
// cette fonction renvoie n! (n est supposé supérieur ou égal à 0)
fonction factorielle(entier n): entier
début
retourne n*factorielle(n-1);
fin
n ! = n *n-1*n-2*…….*2*1
Exemple:
??
Point d’arrêt
• Puisqu'une fonction récursive s'appelle elle-même, il est impératif
qu'on prévoit une condition d'arrêt (ou point d’arrêt) à la récursion,
sinon le programme ne s'arrête jamais!
• On doit toujours tester en premier la condition d'arrêt, et ensuite, si
la condition n'est pas vérifiée, lancer un appel récursif.
• Exemple de la factorielle : si n ≠ 0, n! = n x (n-1)!, sinon n! = 1.
n ! = n * (n-1)! //appel récursif
et 0 ! = 1 // point d’arrêt
Exemple:
fonction factorielle(entier n): entier
Début
si (n = 0) alors
retourne 1
sinon
retourne n*factorielle(n-1)
finsi
fin
Point d’arrêt 6
0
0
0
Activité 1:
• Pour chacune des fonctions suivantes, l’exécuter à la main sur un
exemple, justifier si elle termine ou non en général et si oui dire ce
quelle fait ou calcule
7
//f1 est appelée pour n>=0
fonction f1 (entier n): entier
Début
si (n = 0) alors
retourne 1
sinon
retourne f1(n+1)
finsi
fin
8
//f2 est appelée pour n<0
fonction f2 (entier n): entier
Var res : entier
Début
si (n = 0) alors
retourne 0
Sinon
res  f2(n-1)
res  res+n
retourne res
finsi
fin
9
//f3 est appelée pour n>=0
fonction f3 (entier n): entier
Début
si (n <= 1) alors
retourne 1
Sinon
retourne
1+f3(n-2)
finsi
fin
Récursivité directe/croisée
• Un algorithme est dit récursif :
• lorsqu’il s’appelle lui-même dans sa définition :récursivité directe
• lorsqu’il s’appelle par l’intermédiaire d’autres algorithmes appelés par
lui-même :récursivité indirecte ou croisée
Récursivité directe/croisée
Récursivité directe:
• dans le cas d’une récursivité directe (ou simple), l'appel récursif d'un
sous-algorithme P se fait dans le sous-algorithme P lui-même :
Sous_Algorithme P ( )
. . .
DEBUT
. . .
P // appel récursif direct de P dans le corps de P
. . .
FIN
Récursivité directe/croisée
Récursivité croisée:
• Dans le cas d’une récursivité croisée, l'appel récursif d'un sous programme P se
fait dans le corps d'un ou de plusieurs sous-programmes appelés par P.
• La procédure P utilise pour sa définition la procédure Q qui, elle-même, utilise P .
• Exemple: La parité d’un entier naturel n peut être définie par :
Récursivité directe/croisée
• Les algorithmes correspondants s’écrivent :
Fonction PAIR (n : entier) : booléen
VAR
L : booléen
DEBUT
SI n = 0 alors L vrai
SINON L IMPAIR (n−1)
FIN SI
Retourne L
FIN PAIR
Fonction IMPAIR (n : entier) : booléen
VAR
L : booléen
DEBUT
SI n = 0 alors L faux
SINON L PAIR (n−1)
FIN SI
Retourne L
FIN IMPAIR
Récursivité directe/croisée
• Exécution de Pair(3):
Schéma général d’une fonction récursive
Fonction F (a1 : T1…) : T
VAR
rs: T
DEBUT
SI (test_Arret) alors
rs  …..
SINON
instructions
rs ……F(y1…)
instructions
FIN SI
Retourner (rs)
FIN
Activité 2:
• Ecrire un sous-programme récursif qui calcule la somme des n
premiers nombres positifs
• Indication:
➢Somme(n)=n + somme(n-1)
➢Somme(1)=1
• Faire le tournage à la main pour n=4
16
Activité 2:
17
Activité 2:
18
Activité 3:
Soit la procédure suivante :
Transformer cette procédure en une procédure récursive.
Procédure COMPTER ()
VAR
i: entier
Début
Pour i de 1 à 10 faire
Ecrire (i)
FinPour
Fin
Activité 3:
• Cette procédure peut être traduite en une procédure récursive avec
un paramètre entier;
l'instruction qui l'appellera dans l’algorithme sera compter(1)
Procédure COMPTER (i: entier)
VAR
Début
si (i <=10) alors
Ecrire(i)
COMPTER(i + 1)
Finsi
Fin
Activité 4:
• Ecrire un sous-programme récursif qui calcule la somme des n
premiers carrés (n>0). Par exemple, si n vaut 4, ce sous-programme
calculera 12 + 22 + 32 + 42
• Faire le tournage à la main de cet exemple
21
Activité 5:
• Ecrire un sous-programme récursif qui calcule la somme des éléments
positifs d’un tableau.
22
fonction SomPos(T:Tab, N:entier)
Debut
si (N>=1) alors
si (T[N]>0) alors
SomPosT[N]+SomPos(T,N-1)
sinon
SomPosSomPos(T,N-1)
finsi
finsi
Fin
fonction SomPos(T:Tab, N:entier,i:entier)
Debut
si (i<=N) alors
si (T[i]>0) alors
SomPosT[i]+SomPos(T,N,i+1)
sinon
SomPosSomPos(T,N,i+1)
finsi
finsi
Fin

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

rc.pdf

  • 2. 1. Définition • Une procédure (ou une fonction) est dite récursive si elle comporte un appel à elle même. Une procédure non récursive ne peut comporter que des appels à d’autres procédures. Exemple : On peut définir le factoriel d'un nombre «n» positif de deux manières: ▪ définition non récursive (ou itérative) ▪ définition récursive n ! = n * (n-1)! // cette fonction renvoie n! (n est supposé supérieur ou égal à 0) fonction factorielle(entier n): entier début retourne n*factorielle(n-1); fin n ! = n *n-1*n-2*…….*2*1
  • 4. Point d’arrêt • Puisqu'une fonction récursive s'appelle elle-même, il est impératif qu'on prévoit une condition d'arrêt (ou point d’arrêt) à la récursion, sinon le programme ne s'arrête jamais! • On doit toujours tester en premier la condition d'arrêt, et ensuite, si la condition n'est pas vérifiée, lancer un appel récursif. • Exemple de la factorielle : si n ≠ 0, n! = n x (n-1)!, sinon n! = 1. n ! = n * (n-1)! //appel récursif et 0 ! = 1 // point d’arrêt
  • 5. Exemple: fonction factorielle(entier n): entier Début si (n = 0) alors retourne 1 sinon retourne n*factorielle(n-1) finsi fin
  • 7. Activité 1: • Pour chacune des fonctions suivantes, l’exécuter à la main sur un exemple, justifier si elle termine ou non en général et si oui dire ce quelle fait ou calcule 7 //f1 est appelée pour n>=0 fonction f1 (entier n): entier Début si (n = 0) alors retourne 1 sinon retourne f1(n+1) finsi fin
  • 8. 8 //f2 est appelée pour n<0 fonction f2 (entier n): entier Var res : entier Début si (n = 0) alors retourne 0 Sinon res  f2(n-1) res  res+n retourne res finsi fin
  • 9. 9 //f3 est appelée pour n>=0 fonction f3 (entier n): entier Début si (n <= 1) alors retourne 1 Sinon retourne 1+f3(n-2) finsi fin
  • 10. Récursivité directe/croisée • Un algorithme est dit récursif : • lorsqu’il s’appelle lui-même dans sa définition :récursivité directe • lorsqu’il s’appelle par l’intermédiaire d’autres algorithmes appelés par lui-même :récursivité indirecte ou croisée
  • 11. Récursivité directe/croisée Récursivité directe: • dans le cas d’une récursivité directe (ou simple), l'appel récursif d'un sous-algorithme P se fait dans le sous-algorithme P lui-même : Sous_Algorithme P ( ) . . . DEBUT . . . P // appel récursif direct de P dans le corps de P . . . FIN
  • 12. Récursivité directe/croisée Récursivité croisée: • Dans le cas d’une récursivité croisée, l'appel récursif d'un sous programme P se fait dans le corps d'un ou de plusieurs sous-programmes appelés par P. • La procédure P utilise pour sa définition la procédure Q qui, elle-même, utilise P . • Exemple: La parité d’un entier naturel n peut être définie par :
  • 13. Récursivité directe/croisée • Les algorithmes correspondants s’écrivent : Fonction PAIR (n : entier) : booléen VAR L : booléen DEBUT SI n = 0 alors L vrai SINON L IMPAIR (n−1) FIN SI Retourne L FIN PAIR Fonction IMPAIR (n : entier) : booléen VAR L : booléen DEBUT SI n = 0 alors L faux SINON L PAIR (n−1) FIN SI Retourne L FIN IMPAIR
  • 15. Schéma général d’une fonction récursive Fonction F (a1 : T1…) : T VAR rs: T DEBUT SI (test_Arret) alors rs  ….. SINON instructions rs ……F(y1…) instructions FIN SI Retourner (rs) FIN
  • 16. Activité 2: • Ecrire un sous-programme récursif qui calcule la somme des n premiers nombres positifs • Indication: ➢Somme(n)=n + somme(n-1) ➢Somme(1)=1 • Faire le tournage à la main pour n=4 16
  • 19. Activité 3: Soit la procédure suivante : Transformer cette procédure en une procédure récursive. Procédure COMPTER () VAR i: entier Début Pour i de 1 à 10 faire Ecrire (i) FinPour Fin
  • 20. Activité 3: • Cette procédure peut être traduite en une procédure récursive avec un paramètre entier; l'instruction qui l'appellera dans l’algorithme sera compter(1) Procédure COMPTER (i: entier) VAR Début si (i <=10) alors Ecrire(i) COMPTER(i + 1) Finsi Fin
  • 21. Activité 4: • Ecrire un sous-programme récursif qui calcule la somme des n premiers carrés (n>0). Par exemple, si n vaut 4, ce sous-programme calculera 12 + 22 + 32 + 42 • Faire le tournage à la main de cet exemple 21
  • 22. Activité 5: • Ecrire un sous-programme récursif qui calcule la somme des éléments positifs d’un tableau. 22
  • 23. fonction SomPos(T:Tab, N:entier) Debut si (N>=1) alors si (T[N]>0) alors SomPosT[N]+SomPos(T,N-1) sinon SomPosSomPos(T,N-1) finsi finsi Fin
  • 24. fonction SomPos(T:Tab, N:entier,i:entier) Debut si (i<=N) alors si (T[i]>0) alors SomPosT[i]+SomPos(T,N,i+1) sinon SomPosSomPos(T,N,i+1) finsi finsi Fin