Lec-17 RecursionUpdated
Lec-17 RecursionUpdated
Applied Algorithms
(Lecture 17)
Recursion
Fall-23
Recursion
• Basic problem solving technique is to
divide a problem into smaller subproblems
• These subproblems may also be further
divided into much smaller subproblems
• When the subproblems are small enough
to solve directly the process stops
• A recursive algorithm is a problem
solution that has been expressed in terms
of two or more easier to solve
subproblems
What is recursion?
• A procedure that is defined in terms of
itself
• In a computer language a function that
calls itself
Recursion
Examples:
• A phrase is a "palindrome" if the 1st and last letters are the same,
and what's inside is itself a palindrome (or empty or a single letter)
• rotor
• rotator
• 12344321
Recursion
struct TreeNode {
int data;
TreeNode *left;
TreeNode * right;
};
A mathematical look
• We are familiar with
f(x) = 3x+5
• How about
f(x) = 3x+5 if x > 10 or
f(x) = f(x+2) -3 otherwise
Calculate f(5)
f(x) = 3x+5 if x > 10 or
f(x) = f(x+2) -3 otherwise
• f(5) = f(7)-3
• f(7) = f(9)-3
• f(9) = f(11)-3
• f(11) = 3(11)+5
= 38
But we have not determined what f(5) is yet!
Calculate f(5)
f(x) = 3x+5 if x > 10 or
f(x) = f(x+2) -3 otherwise
• f(5) = f(7)-3 = 29
• f(7) = f(9)-3 = 32
• f(9) = f(11)-3 = 35
• f(11) = 3(11)+5
= 38
Working backwards we see that f(5)=29
Series of calls
f(5)
f(7)
f(9)
f(11)
Recursion
Recursive Definition
of the Factorial Function
1, if n = 0
n! =
n * (n-1)! if n > 0
5! = 5 * 4! = 5 * 24 = 120
4! = 4 * 3! = 4 * 3! = 4 * 6 = 24
3! = 3 * 2! = 3 * 2! = 3 * 2 = 6
2! = 2 * 1! = 2 * 1! = 2 * 1 = 2
1! = 1 * 0! = 1 * 0! = 1
Recursive Definition
of the Fibonacci Numbers
fib(1) = 1
fib(2) = 1
1, n <= 2
fib(3) = 2 fib(n) = fib(n-1) + fib(n-2), n > 2
fib(4) = 3
fib(5) = 5
...
fib(3) = 1 + 1 = 2
fib(4) = 2 + 1 = 3
fib(5) = 2 + 3 = 5
Recursive Definition
int BadFactorial(n){
int x = BadFactorial(n-1);
if (n == 1)
return 1;
else
return n*x;
}
What is the value of BadFactorial(2)?
1. One (ore more) base cases that are not recursive, i.e. we
can directly give a solution:
if (n==1)
return 1;
• It's silly to use the recursive function algorithm to evaluate t(m), since
this would cause precisely the type of inefficiency we're trying to
eliminate.
Counting Digits
• Recursive definition
digits(n) = 1 if (–9 <= n <= 9)
1 + digits(n/10) otherwise
• Example
digits(321) =
1 + digits(321/10) = 1 +digits(32) =
1 + [1 + digits(32/10)] = 1 + [1 + digits(3)] =
1 + [1 + (1)] =
3
int numberofDigits(int n) {
if ((-10 < n) && (n < 10))
return 1
else
return 1 +
numberofDigits(n/10);
}
Recursion
• If you want to compute f(x) but can’t
compute it directly
• Assume you can compute f(y) for any
value of y smaller than x
• Use f(y) to compute f(x)
• For this to work, there has to be at least
one value of x for which f(x) can be
computed directly (e.g. these are called
base cases)
Selection Sort
void selectionSort
(double[] A, int lo, int hi)
{
// A[0]..A[lo-1] contain smallest
// values in A, in ascending order
if (lo < hi) {
swap(A, lo, findMiniumum(A, lo, hi);
selectionSort(A, lo + 1, hi);
}
}
Stacks
• Every recursive function can be
implemented using a stack and iteration.
• Every iterative function which uses a stack
can be implemented using recursion.
Disadvantages
• May run slower.
– Compilers
– Inefficient Code
Advantages
• More natural.
• Easier to prove correct.
• Easier to analysis.
• More flexible.