Chapter 4 - Recursion
Chapter 4 - Recursion
TOPIC 3
Recursion
Zulaile Mabni
CHAPTER OBJECTIVES
▪ Learn about recursive definitions
▪ Explore the base case and the general case
of a recursive definition
▪ Discover what a recursive algorithm is
▪ Learn about recursive methods
▪ Explore how to use recursive methods to
implement recursive algorithms
2
RECURSIVE DEFINITIONS
▪ Recursion
▪ Process of solving a problem by reducing it to
smaller versions of itself.
▪ Recursive definition
▪ Definition in which a problem is expressed in
terms of a smaller version of itself.
▪ Has one or more base cases.
3
RECURSIVE DEFINITIONS
▪ Recursive algorithm
▪ Algorithm that finds the solution to a given
problem by reducing the problem to smaller
versions of itself.
▪ Has one or more base cases.
▪ Implemented using recursive methods.
▪ Recursive method
▪ Method that calls itself
4
RECURSIVE DEFINITIONS
▪ A recursive method contains:
▪ i) Base case
▪ Case in recursive definition in which the solution is
obtained directly.
▪ Stops the recursion.
5
Recursive method
▪ Has unlimited copies of itself
▪ Every recursive call has
▪ its own code
▪ own set of parameters
▪ own set of local variables
6
After completing recursive call:
▪ Control goes back to calling environment.
▪ Recursive call must execute completely
before control goes back to previous call.
▪ Execution in previous call begins from point
immediately following recursive call.
7
RECURSIVE DEFINITIONS
▪ Directly recursive: a method that calls itself
▪ Indirectly
recursive: a method that calls another
method and eventually results in the original method
call.
8
▪ Understand problem requirements
▪ Determine limiting conditions
▪ Identify base cases
9
▪ Provide direct solution to each base case
▪ Identify general case(s)
▪ Provide solutions to general cases in terms of smaller versions
of itself
10
▪ Examples:
▪ i) Factorial method
▪ The factorial of a positive integer is the number
multiplied by every positive integer less than itself
▪ Example:
▪ 3! = 3 * 2! = 6
▪ 2! = 2 * 1! = 2
▪ 1! = 1 * 0! = 1
▪ 0! = 1
11
ii) Fibonacci Number
▪ Fibonacci sequence:
▪ 1, 1, 2, 3, 5, 8, 13, 21, 34, ……
▪ Note:
▪ The third Fibonacci number is the sum of the first
two Fibonacci numbers.
▪ The fourth Fibonacci number in a sequence is
the sum of the second and third Fibonacci
numbers.
12
public static int fact(int num)
{
if(num == 0)//base case
return 1;
else
// general or recursive case
return num * fact(num – 1);
13
14
public static int rFibNum(int a, int b, int n)
{
if(n == 1)
return a;
else if(n == 2)
return b;
else
return rFibNum(a, b, n - 1) + rFibNum(a, b, n - 2);
}
Or
public static int fib(int n)
{
if(n == 1) // base case
return 0;
else if(n == 2) // base case
return 1;
else
return fib(n - 1) + fib(n - 2); // general case
}
15
16
▪ Two ways to solve particular problem
▪ Iteration
▪ Recursion
▪ Iteration:
▪ uses looping to repeat a set of statements
▪ Uses less memory
▪ Recursion
▪ Sometimes recursive solution is easier & simpler
▪ Recursive solution is often slower
▪ Uses up too much time and memory
17
public static int factorial(int n)
{
int fact = 1;
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}
18
CHAPTER SUMMARY
▪ Recursive Definitions
▪ Recursive Algorithms
▪ Recursive methods
▪ Base cases
▪ General cases
19
▪ Malik D.S, Nair P.S., Data Structures Using Java, Course
Technology, 2003.
20