SlideShare a Scribd company logo
RecursionWeek8.ppt
Recursion
Recursion
You should be able to identify the base
case(s) and the general case in a
recursive definition
• To be able to write a recursive
algorithm for a problem involving only
simple variables.
A Recursive call
• A function call in which the function being
called is the same as the one making the
call.
• Put differently it is a function call to
itself.
• When a function calls itself this is said to
be a recursive call.
• The word recursive means: having the
characteristics of coming up again or
repeating.
• Can be used instead of iteration or looping
Efficiency considerations
• Recursive solutions are generally less
efficient than iterative solutions.
• Some problems that we will see lend
themselves to recursion
• Some languages do not allow recursion e.g.
Fortran, Basic and Cobol
• Lisp on the other hand is especially suited
for recursive algorithms
• Initially we will look at recursive algorithms
on simple variables we will then look at
recursion on structured data
What is recursion?
• The simplest use of the recursive
algorithm is to compute a number
raised to a given power, e.g. 2^3 is
2x2x2, 4^5 is 4x4x4x4x4 and so on.
• y^n, yxyxyxyxyxyx…. (n times)
• y^n is yxy^(n-1)
• y^n is yxyxy^(n-2)
• etc
The recursive definition
• This is the important bit.
• We can write x^n as x * x^(n-1). Hence
something is defined in terms of a smaller
version of itself
• Base case: the case for which the solution
can be stated non recursively
• General case: the case for which the
solution is expressed in terms of a smaller
version of itself
See program power.cpp
demo
• Run power.cpp
• See also power*.cpp in the cpteach directory included in
the week 8 directory
• See all Visual Basic illustrations in this directory go t
examples directory
Some comments on the
program
• Each recursive call to Power can be
thought of as creating its own copy of
the parameters x and n.
• x remains the same for each call but
the value of n decreases each time.
• Lets consider the case with number
=2 and exponent = 3, so we are
computing 2^3 which is 8
Tracing through the Power
program
•Call 1. Power is called by main, with number
equal to 2 and exponent equal to 3.
•Because n does not equal 1 Power is called
recursively with x and n-1as arguments.
•Call 2: x is equal to 2 and n is equal to 2.
Because n does not equal 1 the function Power is
called recursively.
•Call 3: x = 2 and n = 1. As n=1, the value of x is
returned. This call to the function has terminated
Infinite recursion
• If the function Power was called with n
negative then the condition n==1 would
never be satisfied since starting at a value
less than zero and continually subtracting
1 would mean that a value of 1 is never
obtained.
• I have altered this now so that negative
powers are calculated
Execution of Power(2,3)
X=2, n=1
X=2, n=2
X=2, n=3
Call 3
Call 2
Call 1
Power(2,3)
Recursive algorithms with
Simple Variables
• Lets look at a special mathematical
function important in so many areas
of mathematics. It is the factorial
function defined as
• n! = nx(n-1)x(n-2)x……1
• E.g. 4! = 4*3*2*1 = 24
Run Factorial demo
Factorial.cpp
Tracing through the Factorial
function
n=0
n=1
n=2
n=3
n=4
Call 5
Call 4
Call 3
Call 2
Call 1
Iterative solution
Writing the code without using recursion gives:
int Factorial(int n)
{
int factor;
int count;
Factor = 1;
for (count=2;count<=n;count++)
Factor=Factor*count;
return Factor;
}
The Towers of Hanoi
• Very old problem demonstrating
recursion.
• Only one disc can be moved at a time
• After each move, all discs must be on
the poles
• No disc may be placed on top of a
disc which is smaller than itself
Hanoi.cpp
• Discuss Hanoi.cpp and demonstrate

More Related Content

PDF
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
PPTX
Recursion
PPTX
Recursion and Sorting Algorithms
PPT
Recursion
PPT
Recursion
PPTX
Unit-I Recursion.pptx
PPT
Data Structures- Part5 recursion
PPT
Lecture9 recursion
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
Recursion
Recursion and Sorting Algorithms
Recursion
Recursion
Unit-I Recursion.pptx
Data Structures- Part5 recursion
Lecture9 recursion

Similar to RecursionWeek8.ppt (20)

PPTX
recursion_esccccccccccc1111122222333.pptx
PPT
Lec-6 Recursion of Data Structures & Algorithms
PPTX
Recursion in Data Structure
PPT
FUNDAMETAL ALG.ppt
PDF
Recursion.pdf
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
PPTX
9781337102087 ppt ch15
PPT
14 recursion
PPT
Recursion
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
PPT
Savitch Ch 14
PDF
6-Python-Recursion.pdf
PPTX
Recursion part 1
PPTX
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
PPTX
Recursion
PPTX
Introduction to Recursion
PPTX
06 Recursion in C.pptx
PPT
Ap Power Point Chpt8
PDF
CPSC 125 Ch 2 Sec 4
PPT
recursive problem_solving
recursion_esccccccccccc1111122222333.pptx
Lec-6 Recursion of Data Structures & Algorithms
Recursion in Data Structure
FUNDAMETAL ALG.ppt
Recursion.pdf
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
9781337102087 ppt ch15
14 recursion
Recursion
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
Savitch Ch 14
6-Python-Recursion.pdf
Recursion part 1
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
Recursion
Introduction to Recursion
06 Recursion in C.pptx
Ap Power Point Chpt8
CPSC 125 Ch 2 Sec 4
recursive problem_solving

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PPTX
Lesson notes of climatology university.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Pre independence Education in Inndia.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Types and Its function , kingdom of life
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
GDM (1) (1).pptx small presentation for students
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Computing-Curriculum for Schools in Ghana
Lesson notes of climatology university.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Supply Chain Operations Speaking Notes -ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pre independence Education in Inndia.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
human mycosis Human fungal infections are called human mycosis..pptx
Pharma ospi slides which help in ospi learning
Cell Types and Its function , kingdom of life
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
VCE English Exam - Section C Student Revision Booklet
Abdominal Access Techniques with Prof. Dr. R K Mishra
GDM (1) (1).pptx small presentation for students
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Classroom Observation Tools for Teachers
Pharmacology of Heart Failure /Pharmacotherapy of CHF

RecursionWeek8.ppt

  • 3. Recursion You should be able to identify the base case(s) and the general case in a recursive definition • To be able to write a recursive algorithm for a problem involving only simple variables.
  • 4. A Recursive call • A function call in which the function being called is the same as the one making the call. • Put differently it is a function call to itself. • When a function calls itself this is said to be a recursive call. • The word recursive means: having the characteristics of coming up again or repeating. • Can be used instead of iteration or looping
  • 5. Efficiency considerations • Recursive solutions are generally less efficient than iterative solutions. • Some problems that we will see lend themselves to recursion • Some languages do not allow recursion e.g. Fortran, Basic and Cobol • Lisp on the other hand is especially suited for recursive algorithms • Initially we will look at recursive algorithms on simple variables we will then look at recursion on structured data
  • 6. What is recursion? • The simplest use of the recursive algorithm is to compute a number raised to a given power, e.g. 2^3 is 2x2x2, 4^5 is 4x4x4x4x4 and so on. • y^n, yxyxyxyxyxyx…. (n times) • y^n is yxy^(n-1) • y^n is yxyxy^(n-2) • etc
  • 7. The recursive definition • This is the important bit. • We can write x^n as x * x^(n-1). Hence something is defined in terms of a smaller version of itself • Base case: the case for which the solution can be stated non recursively • General case: the case for which the solution is expressed in terms of a smaller version of itself
  • 8. See program power.cpp demo • Run power.cpp • See also power*.cpp in the cpteach directory included in the week 8 directory • See all Visual Basic illustrations in this directory go t examples directory
  • 9. Some comments on the program • Each recursive call to Power can be thought of as creating its own copy of the parameters x and n. • x remains the same for each call but the value of n decreases each time. • Lets consider the case with number =2 and exponent = 3, so we are computing 2^3 which is 8
  • 10. Tracing through the Power program •Call 1. Power is called by main, with number equal to 2 and exponent equal to 3. •Because n does not equal 1 Power is called recursively with x and n-1as arguments. •Call 2: x is equal to 2 and n is equal to 2. Because n does not equal 1 the function Power is called recursively. •Call 3: x = 2 and n = 1. As n=1, the value of x is returned. This call to the function has terminated
  • 11. Infinite recursion • If the function Power was called with n negative then the condition n==1 would never be satisfied since starting at a value less than zero and continually subtracting 1 would mean that a value of 1 is never obtained. • I have altered this now so that negative powers are calculated
  • 12. Execution of Power(2,3) X=2, n=1 X=2, n=2 X=2, n=3 Call 3 Call 2 Call 1 Power(2,3)
  • 13. Recursive algorithms with Simple Variables • Lets look at a special mathematical function important in so many areas of mathematics. It is the factorial function defined as • n! = nx(n-1)x(n-2)x……1 • E.g. 4! = 4*3*2*1 = 24
  • 15. Tracing through the Factorial function n=0 n=1 n=2 n=3 n=4 Call 5 Call 4 Call 3 Call 2 Call 1
  • 16. Iterative solution Writing the code without using recursion gives: int Factorial(int n) { int factor; int count; Factor = 1; for (count=2;count<=n;count++) Factor=Factor*count; return Factor; }
  • 17. The Towers of Hanoi • Very old problem demonstrating recursion. • Only one disc can be moved at a time • After each move, all discs must be on the poles • No disc may be placed on top of a disc which is smaller than itself