Lecture 7
Lecture 7
Introduction
In this module, we study recursive algorithms and related concepts. We show how
recursion ties in with induction. That is, the correctness of a recursive algorithm is proved by
induction.
Contents
𝑛 × (𝑛 − 1)! , 𝑛>1
𝑛! = {
1, 𝑛=1
The input parameter of this function is 𝑛, and the return value is type integer. When
𝑛 = 1, the function returns 1. When 𝑛 > 1, the function calls itself (called a recursive
call) to compute (𝑛 − 1)!. It then multiplies the result by 𝑛, which becomes 𝑛!.
The above program is written in a very basic way for clarity, separating the recursive
call from the subsequent multiplication. These two steps may be combined, as follows.
Induction Base: From line 1, we see that the function works correctly for 𝑛 = 1.
Hypothesis: Suppose the function works correctly when it is called with 𝑛 = 𝑚,
for some 𝑚 ≥ 1.
Induction step: Then, let us prove that it also works when it is called with
𝑛 = 𝑚 + 1. By the hypothesis, we know the recursive call works correctly for
𝑛 = 𝑚 and computes 𝑚!. Subsequently, it is multiplied by 𝑛 = 𝑚 + 1, thus
computes (𝑚 + 1)!. And this is the value correctly returned by the program.
2
Prof. D. Nassimi, NJIT, 2015 Recursive Algorithms & Recurrences
Below is the recursive program (pseudocode). It is assumed that the array type is dtype,
declared earlier.
𝑆 = ∑ 𝐴[𝑖]
𝑖=0
3
Prof. D. Nassimi, NJIT, 2015 Recursive Algorithms & Recurrences
The above simple problems could be easily solved without recursion. They were
presented recursively only for pedagogical purposes. The next example problem,
however, truly needs the power of recursion. It would be very difficult to solve the
problem without recursion.
Only one disk may be moved at a time in a restricted manner, from the top of one
tower to the top of another tower. If we think of each tower as a stack, this means
the moves are restricted to a pop from one stack and push onto another stack.
A larger disk must never be placed on top of a smaller disk.
The recursive algorithm for moving n disks from tower A to tower B works as follows. If
𝑛 = 1, one disk is moved from tower A to tower B. If 𝑛 > 1,
1 Recursively move the top 𝑛 − 1 disks from 𝐴 𝑡𝑜 𝐶. The largest disk remains on
tower 𝐴 by itself.
2 Move a single disk from 𝐴 𝑡𝑜 𝐵.
3 Recursively move back 𝑛 − 1 disks from 𝐶 𝑡𝑜 𝐵.
4
Prof. D. Nassimi, NJIT, 2015 Recursive Algorithms & Recurrences
(b) After recursively moving the top 3 disks from Tower 0 to Tower 2
5
Prof. D. Nassimi, NJIT, 2015 Recursive Algorithms & Recurrences
Below is the recursive algorithm. The call Towers (𝐴, 𝐵, 𝐶, 𝑛) moves n disks from tower
A to B, using C as temporary storage.
Towers (𝐴, 𝐵, 𝐶, 𝑛) {
1 if (𝑛 == 1) {
2 MoveOne (𝐴, 𝐵);
3 return};
4 Towers (𝐴, 𝐶, 𝐵, 𝑛 − 1);
5 MoveOne (𝐴, 𝐵);
6 Towers (𝐶, 𝐵, 𝐴, 𝑛 − 1);
}
Proof of Correctness
The above algorithm has a single move appearing twice in the code, once for 𝑛 = 1 and
once for 𝑛 > 1. This repetition may be avoided by making 𝑛 = 0 as the termination
criteria for recursion.
Towers (𝐴, 𝐵, 𝐶, 𝑛) {
if (𝑛 == 0) return;
Towers (𝐴, 𝐶, 𝐵, 𝑛 − 1);
MoveOne (𝐴, 𝐵);
Towers (𝐶, 𝐵, 𝐴, 𝑛 − 1);
}