Understanding Recursion and Factorials
Understanding Recursion and Factorials
Chapter 18
1
Objectives
To describe what a recursive method is and the benefits of using recursion
To develop recursive methods for recursive mathematical functions
To explain how recursive method calls are handled in a call stack
To solve problems using recursion
To implement a binary search using recursion
To understand how Tower of Hanoi problem uses recursion
To discover the relationship and difference between recursion and iteration
To know tail-recursive methods and why they are desirable
Your textbook is most useful for this chapter – the scope is determined by these
slides
2
What is recursion?
• Recursion is when a method calls itself with!
• Let’s expand to make it more understandable:
• A recursive method calls itself with a simpler
version of the original problem until the problem
is so simple that the answer is trivial (easy).
• Then a series of return statements is activated –
one for each recursive call – be patient, the detail
is explained later……
3
What is recursion?
• Key idea: In a recursive method there are always
2 parts:
1. The solution to the base case (easy problem) that
returns the answer and;
2. A call to the method itself with a reduced
(simplified) problem;
2 things to understand:
1. #1 is typically activated with an if-statement
2. #2 typically includes a formula to compute using
the returned result of the method.
4
First example: Computing
Factorial
Factorial – written with a “!” is a classic computer
science problem:
The rules:
0! = 1 Do you see the
1! = 1 pattern?
2! = 2*1=2
3! = 3*2*1 = 6
4! = 4*3*2*1 = 24
First example: Computing
Factorial
Factorial – written with a “!” is a classic
computer science problem:
The rules:
0! = 1 Do you see the
pattern?
1! = 1
2! = 2*1=2 [2!= 2*1!]
3! = 3*2*1 = 6 [3!= 3*2!]
4! = 4*3*2*1 = 24 [4!= 4*3!]
First example: Computing
Factorial
Factorial – written with a “!” is a classic
computer science problem:
The rules:
0! = 1 Do you see the
1! = 1 pattern?
n! = n * (n-1)!
0! = 1
8
Java Code
public static long factorial(int n) {
if (n == 0) // Base case
return 1;
else
return n * factorial(n - 1); // Recursive call
}
Java Code
public static long factorial(int n) {
if (n == 0) // Base case
return 1;
else
return n * factorial(n - 1); // Recursive call
}
}
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
n! = n * (n-1)!
0! = 1
ComputeFactorial Run
11
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4)
12
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
13
animation
Computing Factorial
factorial(0) = 1;
factorial(n) = n*factorial(n-1);
factorial(4) = 4 * factorial(3)
= 4 * 3 * factorial(2)
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))
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)))
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)))
17
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)
18
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
19
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)
20
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
[Link]/watch?v=PORo1ut9kMs
21
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)
22
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)
Step 4: executes factorial(0)
Step 5: return 1 Space Required
for factorial(4)
23
animation
24
animation
return 2 * factorial(1)
Space Required
Step 3: executes factorial(1) for factorial(1)
Step 6: return 1 Space Required
for factorial(2)
return 1 * factorial(0) Space Required
for factorial(3)
Step 4: executes factorial(0)
Step 5: return 1 Space Required
for factorial(4)
25
animation
26
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
27
animation
28
animation
29
animation
30
animation
31
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 method
32
factorial(4) Stack Trace
5 Space Required
for factorial(0)
4 Space Required
for factorial(1)
Space Required
for factorial(1)
3 Space Required
for factorial(2)
Space Required
for factorial(2)
Space Required
for factorial(2)
2 Space Required
for factorial(3)
Space Required
for factorial(3)
Space Required
for factorial(3)
Space Required
for factorial(3)
1 Space Required
for factorial(4)
Space Required
for factorial(4)
Space Required
for factorial(4)
Space Required
for factorial(4)
Space Required
for factorial(4)
6 Space Required
for factorial(1)
Space Required
for factorial(2)
7 Space Required
for factorial(2)
Space Required Space Required
for factorial(3) for factorial(3)
8 Space Required
for factorial(3)
Space Required Space Required Space Required
for factorial(4) for factorial(4) for factorial(4)
9 Space Required
for factorial(4)
33
Fibonacci Numbers
Fibonacci series: 0 1 1 2 3 5 8 13 21 34 55 89…
indices: 0 1 2 3 4 5 6 7 8 9 10 11
fib(0) = 0;
fib(1) = 1;
fib(index) = fib(index -1) + fib(index -2); index >=2
34
Fibonacci Numbers
public static long fib(long index) {
if (index == 0) // Base case
return 0;
else if (index == 1) // Base case
return 1;
else // Reduction and recursive calls
return fib(index - 1) + fib(index - 2);
}
Fibonnaci Numbers, cont.
fib(4)
17: return fib(4) 0: call fib(4)
3: call fib(1)
36
Characteristics of Recursion
All recursive methods have the following characteristics:
– One or more base cases (the simplest case) are used to stop
recursion.
– Every recursive call reduces the original problem, bringing it
increasingly closer to a base case until it becomes that case.
38
Recursive Binary Search
39
Tower of Hanoi
There are n disks labeled 1, 2, 3, . . ., n, and three
towers labeled A, B, and C.
No disk can be on top of a smaller disk at any
time.
All the disks are initially placed on tower A.
Only one disk can be moved at a time, and it
must be the top disk on the tower.
40
Tower of Hanoi, cont.
41
Solution to Tower of Hanoi
The Tower of Hanoi problem can be decomposed into three subproblems.
42
Solution to Tower of Hanoi
43
Recursion vs. Iteration
Recursion is an alternative form of program
control. It is essentially repetition without a loop.
44
Advantages of Using Recursion
Recursion is good for solving the problems that are
inherently recursive.
45
Tail Recursion
A recursive method is said to be tail recursive if
there are no pending operations to be performed on
return from a recursive call.
public static long factorial(int n) {
Non-tail recursive if (n == 0) // Base case
return 1;
else
return n * factorial(n - 1); // Recursive call
}
}
private static long factorial(int n, int result) {
Tail recursive if (n == 0)
return result;
else
return factorial(n - 1, n * result); // Recursive call
}
46
Summary
Are you able to:
Explain what recursion is?
Explain the base case and the reduced
problem?
Give recursive solutions to problems such as
Fibonacci, factorial, Hannoi and power?
Can you explain tail recursion?