Recursion C++
Recursion C++
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 4
factorial function
The C++ equivalent of this definition:
int fac(int numb){
if(numb<=1)
return 1;
else
return numb * fac(numb-1);
}
recursion means that a function calls itself
Recursion / Slide 6
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 7
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
Recursion
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 10
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 11
Recursion
Recursion
Example:
After 1 month there will be 2 pairs of rabbits;
after 2 months, there will be 3 pairs;
after 3 months, there will be 5 pairs (since the following month the
original pair and the pair born during the first month will both produce a
new pair and there will be 5 in all).
Recursion / Slide 14
Recursive definition:
F(0) = 0;
F(1) = 1;
F(number) = F(number-1)+ F(number-2);
Recursion / Slide 16
Recursion / Slide 17
int fib(int n)
{
int f[100];
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 23
Fibonacci Numbers
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 25
{ //cout << "bsearch(data, "<<first<< ", last "<< ", "<<value << "); "<<endl;
int middle = (first + last) / 2;
if (data[middle] == value)
return middle;
else if (first >= last)
return -1;
else if (value < data[middle])
return bsearchr(data, first, middle-1, value);
else
return bsearchr(data, middle+1, last, value);
}
Recursion / Slide 26
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 27
int recur_fn(parameters){
if(stopping condition)
return stopping value;
// other stopping conditions if needed
return function of recur_fn(revised parameters)
}
Recursion / Slide 29
Think Recursively
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 disc 1 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 36
Towers of Hanoi
int main() {
int num_disc; //number of discs
Eight Queens
queens[0] 0
queens[1] 4
queens[2] 7
queens[3] 5
queens[4] 2
queens[5] 6
queens[6] 1
queens[7] 3
Recursion / Slide 38
bool empty(int t[], int row, int col) {
for( int j = 0; j < row; j++) {
if (t[j] == col) //same column
return false;
if (abs(t[j] - col) == (row - j)) //on cross
line
return false;
}
return true;
}
bool queens(int t[], int row, int col) {
if (row == SIZE) // found one answer
return true;
for (col = 0; col <SIZE; col++)
{
t[row] = col;
if (empty(t, row, col) && queens(t, row +1, 0))
return true;
}
return false;
}
void print(int t[]){
// print solution
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++) {
if (j == t[i])
cout << " Q ";
else
cout << " _ ";
}
cout << endl;
}
}
int main() {
int t[SIZE]={0};
for (int i= 0; i <SIZE; i++){
t[0] = i; //on first row, Queen on different
column
cout << endl << endl <<"i is: " << i << endl;
if (queens(t, 1,0))
print(t);
}
}