CSC 385 - Data Structures and Algorithms
University of Illinois Springfield
College of Health, Science, and Technology
Recursion with Explicit Base Case
public static void countDown(int integer) {
//Explicit Base Case
if(integer == 1) {
System.out.println(integer);
} else {
System.out.println(integer);
countDown(integer - 1)
}
}We state what the stop condition is. Recursion stops when condition is true. integer == 1
Copyright ©2012 by Pearson Education, Inc. All rights reserved
The stack of activation records during the execution of the call countDown(3)
The stack of activation records during the execution of the call countDown(3)
What is wrong with the following recursive method?
Answer: There is no base case. The method will never terminate.
What is wrong with the following recursive method?
public static void countUp(int integer) {
if (integer > 0) {
countUp(integer + 1);
}
System.out.println(integer);
}Answer: The method does not progress twoards the base case for values greater than 0. The base case will never be reached.
public static void countUp(int integer) {
if (integer > 0) {
countUp(integer - 1);
}
System.out.println(integer);
}Tracing the execution of sumOf(3)
Tracing the execution of sumOf(3)


The initial configuration of the Towers of Hanoi for 3 disks
The smaller problems in a recursive solution for four disks
Pseudocode
Algorithm solveTowers(numberOfDisks, startPole, tempPole, endPole) {
if(numberOfDisks > 0) {
solveTowers(numberOfDisks - 1, startPole, endPole, tempPole)
Move disk from startPole to endPole
solveTowers(numberOfDisks - 1, tempPole, startPole, endPole)
}
}
Java Implementation
public static void solve(int disks, int start, int temp, int end) {
if(disks > 0) {
//move n - 1 from start to temp using end as auxiliary
solve(disks - 1, start, end, temp);
System.out.println("Move disk " + start + " to " + end);
//move n - 1 from temp to end using start as auxiliary
solve(disks - 1, temp, start, end);
}
}\[ \begin{align*} F(0) &= 0 \\ F(1) &= 1 \\ F(n) &= F(n - 1) + F(n - 2) \end{align*} \]
Algorithm Fibonacci(n) {
if n <= 1
return n
else
return Fibonacci(n - 1) + Fibonacci(n - 2)
}
| n | Fibonacii(n) |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 5 |
| 6 | 8 |
| 7 | 13 |
n can be the values 0 to any positive integerpublic static int fib(int n) {
int known[] = new int[n + 1];
for(int i = 0; i < known.length; i++) {
known[i] = -1;
}
return fib(n, known);
}
private static int fib(int n, int known[]) {
if(n <= 1) {
return n;
}
if(known[n] != -1) {
return known[n];
}
known[n] = fib(n - 1, known) + fib(n - 2, known);
return known[n];
}Below are the steps to take when analyzing the time requirement of a recursive algorithm
n indicating the input sizen.n = 1 the statement System.out.println(n) is executed one time for \(O(1)\)n > 1 both the println statement and the recursive method call are executed.
\[ \begin{align*} T(1) &= O(1) \\ T(n) &= T(n - 1) + O(1) \end{align*} \]
Expand to base case \[ \begin{align*} T(4) &= T(3) + O(1) \\ T(3) &= T(2) + O(1) \\ T(2) &= T(1) + O(1) \\ T(1) &= O(1) \end{align*} \]
Go back up substituting previous T(n) solution to right hand side \[ \begin{align*} T(1) &= O(1) \\ T(2) &= O(1) + O(1) = O(2) \\ T(3) &= O(2) + O(1) = O(3) \\ T(4) &= O(3) + O(1) = O(4) \end{align*} \]
\[ \begin{align*} T(n) &= T(n - 1) + O(1) \\ &= T(n - 2) + O(1) + O(1) \\ &= T(n - 3) + O(1) + O(1) + O(1) \\ \dots \\ &= ((n - 1) O(1)) + T(1) \\ &= O(n - 1) + O(1) \\ &= O(n) \end{align*} \]
\(T(1)\) is the base case*
\[ \begin{align*} T(n + 1) &= T(n) + O(1) \\ &= O(n) + O(1) \\ &= O(n + 1) ✓ \end{align*} \]
\[ \begin{align*} T(1) &= O(1) \\ T(n) &= T(n - 1) + T(n - 1) + O(1) \\ &= 2T(n - 1) + O(1) \end{align*} \]
Example of expansion
Reduce to base case
\[ \begin{align*} T(3) &= 2T(3 - 1) + O(1) = 2T(2) + O(1) \\ T(2) &= 2T(2 - 1) + O(1) = 2T(1) + O(1) \\ T(1) &= O(1) \end{align*} \]
\[ \begin{align*} T(2) &= 2T(1) + O(1) = 2(O(1)) + O(1) = 3O(1) \\ T(3) &= 2T(2) + O(1) = 2(3O(1)) + O(1) = 7O(1) \end{align*} \]
| n | T(n) |
|---|---|
| 1 | O(1) |
| 2 | 3 O(1) |
| 3 | 7 O(1) |
| 4 | 15 O(1) |
| 5 | 31 O(1) |
\[ \begin{align*} T(n + 1) &= 2T((n + 1) - 1) + 1 \\ &= 2T(n) + 1 \\ &= 2(2^n - 1) + 1 \\ &= 2^{n + 1} - 2 + 1 \\ &= 2^{n + 1} - 1 ✓ \end{align*} \]
n = 1 disk
Only 1 call
n = 2 disks
3 calls
n = 3 disks
7 calls
\[ \sum_{i=0}^n{2^i} \]
To learn more about recursion, click here

CSC 385 - Data Structures and Algorithms