Comp171 Recursion 1
Comp171 Recursion 1
Spring 2009
Recursion
Recursion / Slide 2
Recursion
In some problems, it may be natural to define
the problem in terms of the problem itself.
Recursion is useful for problems that can be
represented by a simpler version of the same
problem.
Example: the factorial function
6! = 6 * 5 * 4 * 3 * 2 * 1
We could write:
6! = 6 * 5!
Recursion / Slide 3
factorial function
The C++ equivalent of this definition:
int fac(int numb){
if(numb<=1)
return 1;
else
return numb * fac(numb-1);
}
factorial function
Assume the number typed is 3, that is, numb=3.
fac(3) :
3 <= 1 ? No.
fac(3) = 3 * fac(2)
fac(2) :
2 <= 1 ? No.
fac(2) = 2 * fac(1)
fac(1) :
1 <= 1 ? Yes.
return 1 int fac(int numb){
fac(2) = 2 * 1 = 2 if(numb<=1)
return fac(2) return 1;
fac(3) = 3 * 2 = 6 else
return fac(3) return numb * fac(numb-1);
}
fac(3) has the value 6
Recursion / Slide 6
factorial function
For certain problems (such as the factorial function), a
recursive solution often leads to short and elegant code.
Compare the recursive solution with the iterative solution:
Recursion
To trace recursion, recall that function calls operate as
a stack – the new function is put on top of the caller
Iteration
If we use iteration, we must be careful not to
create an infinite loop by accident:
Oops!
int result = 1;
while(result >0){
...
result++;
} Oops!
Recursion / Slide 9
Recursion
Similarly, if we use recursion we must be
careful not to create an infinite chain of
function calls:
int fac(int numb){ Oops!
return numb * fac(numb-1); No termination
}
condition
Or:
int fac(int numb){
if (numb<=1)
return 1;
else
return numb * fac(numb+1);
}
Oops!
Recursion / Slide 10
Recursion
Recursion
Recursive definition:
F(0) = 0;
F(1) = 1;
F(number) = F(number-1)+ F(number-2);
Recursion / Slide 13
int fib(int n)
{
int f[n+1];
f[0] = 0; f[1] = 1;
for (int i=2; i<= n; i++)
f[i] = f[i-1] + f[i-2];
return f[n];
}
Recursion / Slide 19
Binary search
Compare the search element with the
middle element of the array
If not equal, then apply binary search to
half of the array (if not empty) where the
search element would be.
Recursion / Slide 20
Binary Search
int main() {
const int array_size = 8;
int list[array_size]={1, 2, 3, 5, 7, 10, 14, 17};
int search_value;
return 0;
}
Recursion / Slide 22
int recur_fn(parameters){
if(stopping condition)
return stopping value;
// other stopping conditions if needed
return function of recur_fn(revised parameters)
}
Recursion / Slide 24
Towers of Hanoi
void hanoi(int from, int to, int num)
{
int temp = 6 - from - to; //find the temporary
//storage column
if (num == 1){
cout << "move 1 disc from " << from
<< " to " << to << endl;
}
else {
hanoi(from, temp, num - 1);
cout << "move disc " << num << " from " << from
<< " to " << to << endl;
hanoi(temp, to, num - 1);
}
}
Recursion / Slide 28
Towers of Hanoi
int main() {
int num_disc; //number of discs
hanoi(1,3,2) hanoi(2,1,2)
hanoi(1,2,1) hanoi(2,3,1)
hanoi(2,3,1) hanoi(3,1,1)
hanoi(3,2,2) hanoi(1,3,2)
hanoi(3,1,1) hanoi(1,2,1)
hanoi(1,2,1) hanoi(2,3,1)
Recursion / Slide 30
hanoi(1,2,1) hanoi(2,3,1)
Move disc 2 Move disc 2
from 1 to 3 from 2 to 1
hanoi(2,3,1) hanoi(3,1,1)
Move disc 3 Move disc 3
from 1 to 2 from 2 to 3
hanoi(3,1,1) hanoi(1,2,1)
Move disc 2 Move disc 2
from 3 to 2 from 1 to 3
hanoi(1,2,1) hanoi(2,3,1)
Basic Mathematics
Chapter 1 (1.2 and 1.3) Weiss
Recursion / Slide 32
Logarithms
Mathematical Foundation
•Sum of squares:
1 + 22 + 32 +………N2 = N(N + 1)(2N + 1)/6
(proof by induction)
Recursion / Slide 34
Proof By Induction
• Assume that the property holds for input size 1,…n. Show
that the property holds for input size n+1.
Proof: 1(1+1)/2 = 1
Thus the property holds for n = 1 (base case)
Sum of Squares
1(1+1)(2+1)/6 = 1
Thus the property holds for n = 1 (base case)
=(m+1)[m(2m+1)/6 +m+1]
= (m+1)[2m2 + m + 6m +6]/6
Fibonacci Numbers
F0 = 1, F1 = 1,
Fi = Fi-1 + Fi-2 ,
F2 = 2, F3 = 3, F4 = 5, F5 = 8
Recursion / Slide 40
F2 < (5/3 )2
Fk+2 = Fk + Fk+1 ,
= (5/3)k (5/3 + 1)