CSC438
FUNDAMENTALS OF DATA STRUCTURE
CHAPTER 6
RECURSION
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
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
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
• Base case
– Case in recursive definition in which the solution is
obtained directly
– Stops the recursion
RECURSIVE DEFINITIONS
• A recursive method contains:
i) Base case
– Case in recursive definition in which the solution is
obtained directly
– Stops the recursion
ii) General case
– Breaks problem into smaller versions of itself
– Case in recursive definition in which a smaller version
of itself is called
• Must eventually be reduced to a base case
RECURSIVE FACTORIAL
FUNCTION
public static int fact(int num)
{
if(num == 0)
return 1;
else
return num * fact(num – 1);
}
TRACING A RECURSIVE
METHOD
• Recursive method
– Has unlimited copies of itself
– Every recursive call has its
• own code
• own set of parameters
• own set of local variables
TRACING A RECURSIVE
METHOD
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
TYPES OF RECURSION
• Directly recursive: a method that calls itself
• Indirectly recursive: a method that calls another method
and eventually results in the original method call
• Tail recursive: a method in which the last statement
executed is the recursive call
• Infinite recursion: the case where every recursive call
results in another recursive call
DESIGNING RECURSIVE
METHODS
• Understand problem requirements
• Determine limiting conditions
• Identify base cases
DESIGNING RECURSIVE
METHODS
• Provide direct solution to each base case
• Identify general case(s)
• Provide solutions to general cases in terms of smaller
versions of itself
– Case in recursive definition in which a smaller version
of itself is called
– Must eventually be reduced to a base case
RECURSIVE SOLUTIONS
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
RECURSIVE SOLUTIONS
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.
RECURSIVE FACTORIAL
FUNCTION
public static int fact(int num)
{
if(num == 0) // base case
return 1;
else
// general or recursive case
return num * fact(num – 1);
}
RECURSIVE FACTORIAL
TRACE
RECURSIVE IMPLEMENTATION:
LARGEST VALUE IN ARRAY
public static int largest(int list[], int lowerIndex, int upperIndex)
{
int max;
if(lowerIndex == upperIndex) //the size of the sublist is 1
return list[lowerIndex];
else {
max = largest(list, lowerIndex + 1, upperIndex);
if(list[lowerIndex] >= max)
return list[lowerIndex];
else
return max;
}
}
EXECUTION OF LARGEST
(LIST, 0, 3)
RECURSIVE FIBONACCI
public static int rFibNum(int public static int fib(int n)
a, int b, int n) {
{ if(n == 1)// base case
if(n == 1) return 0;
return a; else if(n == 2)// base case
OR
else if(n == 2) return 1;
return b;
else
else return fib(n -1) + fib(n
return rFibNum(a, b, n -2); // general case
- 1) + rFibNum(a, b,
n - 2); }
}
EXECUTION OF
RFIBONACCI(2,3,5)
DECIMAL TO BINARY:
RECURSIVE ALGORITHM
public static void decToBin(int num)
{
if(num > 0)
{
decToBin(num/2, 2);
System.out.println(num % 2);
}
}
EXECUTION OF DECTOBIN(13)
TOWERS OF HANOI PROBLEM
WITH THREE DISKS
TOWERS OF HANOI PROBLEM
WITH THREE DISKS
TOWERS OF HANOI: THREE DISK
SOLUTION
TOWERS OF HANOI:
RECURSIVE ALGORITHM
public static void moveDisks(int count, int needle1, int needle3, int
needle2)
{
if(count > 0)
{
moveDisks(count - 1, needle1, needle2, needle3);
System.out.println("Move disk “ + count + “ from “ +
needle1 + “ to “ + needle3 + ".“);
moveDisks(count - 1, needle2, needle3, needle1);
}
}
8-QUEENS PUZZLE
• Place 8 queens on a chessboard (8 X 8 square board)
so that no two queens can attack each other. For any
two queens to be non-attacking, they cannot be in the
same row, same column, or same diagonals.
8x8 SQUARE BOARD
BACKTRACKING ALGORITHM
• Attempts to find solutions to a problem by constructing
partial solutions
• Makes sure that any partial solution does not violate the
problem requirements
• Tries to extend partial solution towards completion
BACKTRACKING ALGORITHM
• If it is determined that partial solution would not lead to
solution
– partial solution would end in dead end
– algorithm backs up by removing the most recently
added part and then tries other possibilities
4-QUEENS PUZZLE
SOLUTIONS TO 8-QUEEN
PUZZLE
RECURSION OR ITERATION?
• Two ways to solve particular problem
– Iteration
– Recursion
• Iterative control structures:
– uses looping to repeat a set of statements
– Uses less memory
• Tradeoffs between two options
– Sometimes recursive solution is easier and simpler
– Recursive solution is often slower
– Recursive uses up too much and memory
CHAPTER SUMMARY
• Recursive Definitions
• Recursive Algorithms
• Recursive methods
• Base and General cases
• Tracing recursive methods
• Designing recursive methods
• Varieties of recursive methods
• Recursion vs. Iteration
REFERENCES
• Carrano, F. & Savitch, W. 2005. Data Structures and
Abstractions with Java, 2nd ed. Prentice-Hall.
• Malik D.S, & Nair P.S., Data Structures Using Java,
Thomson Course Technology, 2003.
• Rada Mihalcea, CSCE 3110 Data Structures and
Algorithm Analysis notes, U of North Texas.
• Koffman E., Wolfgang P., Objects, Abstraction, Data
Structures And Design Using Java, JohnWiley&Sons,
2005.
• Weiss Mark Allen, Data Structures & Algorithm
Analysisin C++, Pearson Education International
Inc,2003.