0% found this document useful (0 votes)
4 views47 pages

Understanding Recursion and Factorials

Recursion

Uploaded by

Kurhula Maluleke
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views47 pages

Understanding Recursion and Factorials

Recursion

Uploaded by

Kurhula Maluleke
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

SU 6: Recursion

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?

2! = 2*1=2 [2!= 2*1!]


3! = 3*2*1 = 6 [3!= 3*2!]
4! = 4*3*2*1 = 24 [4!= 4*3!]
n!= n*(n-1)!
First example: Computing Factorial
factorial(0) = 1; - BASE CASE
factorial(n) = n*factorial(n-1); - REDUCED PROBLEM

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

Trace Recursive factorial


Executes factorial(4)

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

22
animation

Trace Recursive factorial

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)

return 1 Main method

23
animation

Trace Recursive factorial

factorial(4) Executes factorial(2)


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
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 method

24
animation

Trace Recursive factorial

factorial(4) Executes factorial(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

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)

return 1 Main method

25
animation

Trace Recursive factorial

factorial(4) Executes factorial(0)


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) Space Required


for factorial(0)
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)

return 1 Main method

26
animation

Trace Recursive factorial

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

return 2 * factorial(1) Space Required


for factorial(0)
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
Step 4: executes factorial(0) for factorial(3)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main method

27
animation

Trace Recursive factorial

factorial(4) returns factorial(0)


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) Space Required


for factorial(0)
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
Step 4: executes factorial(0) for factorial(3)
Step 5: return 1 Space Required
for factorial(4)
return 1 Main method

28
animation

Trace Recursive factorial

factorial(4) returns factorial(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
return 2 * factorial(1)
Step 3: executes factorial(1)
Step 6: return 1 Space Required
for factorial(1)

return 1 * factorial(0) Space Required


for factorial(2)
Step 4: executes factorial(0) Space Required
Step 5: return 1 for factorial(3)
Space Required
return 1 for factorial(4)
Main method

29
animation

Trace Recursive factorial

factorial(4) returns factorial(2)


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) Space Required
for factorial(2)

Step 4: executes factorial(0) Space Required


Step 5: return 1 for factorial(3)
Space Required
return 1 for factorial(4)
Main method

30
animation

Trace Recursive factorial

factorial(4) returns factorial(3)


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) Space Required
Step 5: return 1 for factorial(3)
Space Required
return 1 for factorial(4)
Main method

31
animation

Trace Recursive factorial


returns factorial(4)

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

fib(3) = fib(2) + fib(1) = (fib(1) + fib(0)) + fib(1) = (1 + 0)


+fib(1) = 1 + fib(1) = 1 + 1 = 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)

return fib(3) + fib(2)


11: call fib(2)
10: return fib(3)

1: call fib(3) 16: return fib(2)

return fib(2) + fib(1) return fib(1) + fib(0)


7: return fib(2) 8: call fib(1) 14: return fib(0)
2: call fib(2) 13: return fib(1) 12: call fib(1)

9: return fib(1) 15: return fib(0)


return fib(1) + fib(0) return 1 return 1 return 0
4: return fib(1) 5: call fib(0)

3: call fib(1)

return 1 6: return fib(0) return 0

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.

In general, to solve a problem using recursion, you break it


into subproblems. If a subproblem resembles the original
problem, you can apply the same approach to solve the
subproblem recursively. This subproblem is almost the
same as the original problem in nature with a smaller size.
37
Recursive Binary Search
1. Case 1: If the key is less than the middle element,
recursively search the key in the first half of the array.
2. Case 2: If the key is equal to the middle element, the
search ends with a match.
3. Case 3: If the key is greater than the middle element,
recursively search the key in the second half of the
array.

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

 Move the first n - 1 disks from A to C with the assistance of tower


B.
 Move disk n from A to B.
 Move n - 1 disks from C to B with the assistance of tower A.

43
Recursion vs. Iteration
Recursion is an alternative form of program
control. It is essentially repetition without a loop.

Recursion bears substantial overhead. Each time


the program calls a method, the system must assign
space for all of the method’s local variables and
parameters. This can consume considerable
memory and requires extra time to manage the
additional space.

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?

You might also like