09_recursion
09_recursion
Sections 17.117.2
These slides were adapted by Prof. Gheith Abandah from the Computer Engineering Department of the University 1
of Jordan for the Course: Computer Skills for Engineers (0907101)
Outline
• Introduction
• Example: Factorials
2
Motivations
• Recursion is a technique that leads to
elegant solutions to problems that are
difficult to program using simple loops.
• A recursive function is one that invokes itself.
• Suppose you want to find all the files under a
directory that contains a particular word.
How do you solve this problem? There are
several ways to solve this problem. An
intuitive solution is to use recursion by
searching the files in the subdirectories
recursively.
3
Outline
• Introduction
• Example: Factorials
4
Factorials in Math
0! = 1 -
1! = 1 1 x 0!
2! = 2 x1 2 x 1!
3! = 3 x 2 x 1 3 x 2!
4! = 4 x 3 x 2 x 1 4 x 3!
5! = 5 x 4 x 3 x 2 x 1 5 x 4!
6! = 6 x 5 x 4 x 3 x 2 x 1 6 x 5!
7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 7 x 6!
8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 8 x 7!
9! = 9 x 8 x 7 x 6 x5 x 4 x 3 x 2 x 9 x 8!
1
10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 10 x 9!
3x2x1
5
Computing Factorial
n! = n × (n - 1) × (n - 2) × ... × 2 ×
1
0! = 1;
n! = n × (n - 1)!; n > 0
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
ComputeFactorial Run
6
ComputeFactorial.cpp
#include <iostream>
using namespace std;
int main()
{
// Prompt the user to enter an integer
cout << "Please enter a non-negative integer: ";
int n;
cin >> n;
// Display factorial
cout << "Factorial of " << n << " is " << factorial(n);
return 0;
} Factorial of 4 is 24 7
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4)
8
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
9
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
10
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
11
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
12
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
13
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
14
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
=4*3*2
15
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
=4*3*2
=4*6
16
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * (2 * factorial(1))
= 4 * 3 * ( 2 * (1 * factorial(0)))
= 4 * 3 * ( 2 * ( 1 * 1)))
= 4 * 3 * ( 2 * 1)
=4*3*2
=4*6
= 24
17
animation
factorial(4)
Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack
return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
return 1 * factorial(0)
Step 4: executes factorial(0)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function
18
animation
factorial(4)
Step 0: executes factorial(4)
Step 9: return 24
Executes factorial(3)
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack
return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
return 1 * factorial(0) Space Required
for factorial(3)
19
animation
return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1 Space Required
for factorial(2)
20
animation
return 2 * factorial(1)
Space Required
Step 3: executes factorial(1)
Step 6: return 1 for factorial(1)
Space Required
for factorial(2)
return 1 * factorial(0) Space Required
21
animation
22
animation
factorial(4) returns 1
Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack
23
animation
24
animation
return 2 * factorial(1)
Step 3: executes factorial(1) Space Required
Step 6: return 1 for factorial(1)
Space Required
return 1 * factorial(0) for factorial(2)
Space Required
Step 4: executes factorial(0) for factorial(3)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function
25
animation
return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
Space Required
return 1 * factorial(0) for factorial(2)
Space Required
Step 4: executes factorial(0) for factorial(3)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function
26
animation
return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
return 1 * factorial(0)
Space Required
Step 4: executes factorial(0) for factorial(3)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function
27
animation
factorial(4)
Step 0: executes factorial(4)
Step 9: return 24
return 4 * factorial(3)
Step 1: executes factorial(3)
Step 8: return 6
return 3 * factorial(2)
Step 2: executes factorial(2)
Step 7: return 2 Stack
return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1
return 1 * factorial(0)
Step 4: executes factorial(0)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main function
28
factorial(4) Stack
Trace
29
Outline
• Introduction
• Example: Factorials
30