09 - Recursion
09 - Recursion
1 if n = 0
n!= (recursive solution)
(n-1)!*n if n > 0
1 if n = 0
n!= (closed form solution)
1*2*3*…*(n-1)*n if n > 0
Coding the factorial function
• Recursive implementation
int Factorial (int n)
{
if (n==0) // base case
return 1;
else
return n * Factorial(n-1); // decompose, solve, combine
}
n choose k (combinations)
• Given n objects, how many different sets of
size k can be chosen?
n n-1 n-1
= + , 1 < k < n (recursive solution)
k k k-1
n n!
= , 1 < k < n (closed-form solution)
k k!(n-k)!
with base cases:
n n
= n (k = 1), = 1 (k = n)
1 n
Coding n choose k (combinations)
int Combinations(int n, int k)
{
if(k == 1) // base case 1
return n;
else if (n == k) // base case 2
return 1;
else
return(Combinations(n-1, k) + Combinations(n-1, k-1));
}
How does the computer
implement recursion?
• It uses a stack called “run-time” stack to
keep track of the function calls.
• Each time a function is called recursively,
an “activation record” is created and stored
in the stack.
• When recursion returns, the corresponding
activation is popped out of the stack.
What happens during
a function call?
int a(int w)
{
return w+1;
}
int b(int x)
{
int z,y;
……………… // other statements
z = a(x) + y;
a() is called here!
return z;
}
What happens during
a function call? (cont.)
• An activation record is stored into the
run-time stack:
1) The system stops executing function b and starts
executing function a
2) Since it needs to come back to function b later, it
needs to store everything about function b that is
going to need (x, y, z, and the place to start executing
upon return)
3) Then, x from a is bounded to w from b
4) Control is transferred to function a
What happens during
a function call? (cont.)
• After function a is executed, the activation
record is popped out of the run-time stack
– All the old values of the parameters and variables in
function b are restored and the return value of function
a replaces a(x) in the assignment statement
Recursive Function Calls
• There is no difference between recursive and non-
recursive calls!
int f(int x)
{
int y;
if(x==0)
return 1;
else {
y = 2 * f(x-1);
return y+1;
}
}
f(3) 2*f(2)
2*f(1)
2*f(1)
=f(0)
=f(1)
=f(2)
=f(3)
Conclusion
found = false;
while( (first <= last) && !found) {
midPoint = (first + last) / 2;
if (item < info[midPoint])
last = midPoint - 1;
else if(item > info[midPoint])
first = midPoint + 1;
else {
found = true;
item = info[midPoint];
}
}
}
Binary Search Using Recursion
• What is the size factor?
The number of elements in (info[first] ... info[last])
template<class ItemType>
void SortedType<ItemType>::RetrieveItem
(ItemType& item,
bool& found)
{
found = BinarySearch(info, item, 0, length-1);
}
Binary Search Using Recursion
template<class ItemType>
(cont’d)
bool BinarySearch(ItemType info[], ItemType& item, int first, int last)
{
int midPoint;
if(first > last) // base case 1
return false;
else {
midPoint = (first + last)/2;
if(item < info[midPoint])
return BinarySearch(info, item, first, midPoint-1); // general case 1
else if (item == info[midPoint]) { // base case 2
item = info[midPoint];
return true;
}
else
return BinarySearch(info, item, midPoint+1, last); // general case 2
}
}
Recursive InsertItem (sorted list)
location
location
location
location
Recursive InsertItem (sorted list)
• What is the size factor?
The number of elements in the current list
What is the base case(s)?
1) If the list is empty, insert item into the empty list
2) If item < location->info, insert item at the front in
the current list
• What is the general case?
Insert(location->next, item)
Recursive InsertItem (sorted list)
template <class ItemType>
void SortedType<ItemType>::InsertItem(ItemType newItem)
{
Insert(listData, newItem);
}
= location;
location
location
Recursive DeleteItem (sorted list)
location
location
location
Deciding whether to use a
recursive solution ...
• The recursive version is shorter and simpler than
the non-recursive solution.
= C om b (5 , 3 ) C om b (5 , 4 )
+
= C om b (4 ,2 ) C om b (4 , 3 ) + C om b (4 , 3 ) + C om b (4 , 4 )
+
= 3 + , 1 ) + C om b (2 , 2 ) + +C om b (2 , 1 ) + C om b (2
+ C om b (2 + , 2) + 1 + +C om b (2 , 1 ) + C om b (2
+ , 2) + 1 + 1+
= 3 + 2 + 1 + 2 + 1 + 1 + 2 + 1 + 1 + 1
= 15