16 - Recursion 1
16 - Recursion 1
After today's class you'll receive an email from the MILES lab
with a link to a short survey. This is the last of the end-of-class
surveys! Please take few minutes to fill it out.
WHAT ARE WE GOING TO DO TODAY?
Recursive algorithms
reverseList
sortList
towerOfHanoi
power
RECURSIVE – DEFINITION
When debugging your code look for the following common mistakes:
You forget to handle the base case, or one of the base cases.
The recursive step does not reduce the problem to a smaller one, hence the
recursion does not converge (i.e. the base case cannot be reached)
On the bright side, if you write a program that has an infinite recursion, then a
StackOverflowError will be raised. A buggy recursive program fails faster
than one with an infinite iteration.
EXAMPLE 5 – TOWER OF HANOI
Problem: Move n disks from start tower to finish tower such that:
- you can have a smaller disk on top of bigger disk (but you can’t
have a bigger disk onto a smaller disk)
EXAMPLE - n=1
start finish
start finish
EXAMPLE - n=2
start finish
from A to C
start finish
EXAMPLE - n=2
from A to B
start finish
from C to B
start finish
HOW SHOULD WE MOVE 5 DISKS FROM A TO B?
start finish
tower(1,A,B,C)
tower(4,C,B,A)
CORRECTNESS
Claim: the tower( ) algorithm is correct, namely it moves the blocks from start to
finish without breaking the two rules (one at a time, and can’t put bigger one onto
smaller one).
Proof: (sketch)
Base case: tower(1,*,*,*) is correct.
Induction step:
for any k > 1, assume tower(k,*,*,*) is correct
Prove tower(k+1,*,*,*) is correct.
HOW MANY MOVES?
move from
start to finish
Answer: 1
HOW MANY MOVES?
move move
move from A to C
move from C to B
HOW MANY MOVES?
move move
Answer: 1 + 2 + 4 = 20 + 21 + 22
HOW MANY MOVES?
move move
Answer: 1 + 2 + 4 + … + 2𝑛−1 = 2𝑛 − 1
RECURSION AND ITERATION
Definition of power
𝑥𝑛 = 𝑥 ⋅ 𝑥 ⋯ 𝑥
𝑛 times
Inductive definition:
Base clause:
𝑥0 = 1
Inductive clause:
𝑥 𝑛 = 𝑥 ⋅ 𝑥 𝑛−1
POWER (𝑥𝑛) – ITERATIVE 1
power(x, n) {
int result =1;
for(int i=1; i<=n; i++) {
result = result * x;
}
return result;
}
POWER (𝑥𝑛) – RECURSIVE
power(x, n) {
if(n==0) {
return 1;
} else {
return x * power(x,n-1);
}
}
POWER() – CAN WE DO BETTER?
18 9 9
𝑥 = 𝑥 ∗𝑥
9 4 4
𝑥 = 𝑥 ∗𝑥 ∗𝑥
𝑥4 = 𝑥2 ∗ 𝑥2
2
𝑥 = 𝑥∗𝑥
POWER (𝑥𝑛) – RECURSIVE 2
power( x, n) {
if (n == 0)
return 1;
else if (n == 1)
return x;
else{
tmp = power(x, n/2);
if (n%2==0)
return tmp*tmp; // one multiplication
else
return tmp*tmp*x; // two multiplications
}
}
A SIMILAR IDEA CAN BE IMPLEMENTED ITERATIVELY
power(x, n) {
result = 1;
pow = x; Let 𝑛 = 𝑎𝑘−1 , … , 𝑎1 , 𝑎0 2
if(n%2 == 1)
result = x;
n = n/2;
while(n != 0) { // log 2 (𝑛) − 1 iterations
pow = pow * pow; // 1 multiplication
if(n%2 == 1)
result = result * pow; // 1 multiplication
n = n/2;
}
return result;
}
POWER (𝑥𝑛) – ITERATIVE 2
power(x, n) {
result = 1;
pow = x; Let 𝑛 = 𝒂𝒌−𝟏 , … , 𝒂𝟏 , 𝒂𝟎 2
if(n%2 == 1)
result = x;
n = n/2;
while(n != 0) { // log 2 (𝑛) − 1 iterations
pow = pow * pow; // 1 multiplication
if(n%2 == 1)
result = result * pow; // 1 multiplication
n = n/2;
}
return result;
}
POWER (𝑥𝑛) – ITERATIVE 2
power(x, n) {
result = 1;
pow = x; Let 𝑛 = 𝒂𝒌−𝟏 , … , 𝒂𝟏 , 𝑎0 2
if(n%2 == 1)
result = x;
n = n/2;
while(n != 0) { // log 2 (𝑛) − 1 iterations
pow = pow * pow; // 1 multiplication
if(n%2 == 1)
result = result * pow; // 1 multiplication
n = n/2;
}
return result;
}
EXAMPLE: 𝑥243
𝑛 = (243)10 = (11110011)2
𝑛 = (243)10 = (11110011)2
𝑛 = (243)10 = (11110011)2
A: 𝑂(log 𝑛)
OBSERVATIONS
We can recursively define also data structures. Consider the following class
defining a Spell recursively. Each spell has a title and an optional list of subspells
within it. Write a recursive method reciteSpell() that takes a Spell as input
and prints the title of each spell and its subspells in the order they appear.
:
}
Coming next:
Binary Search