CSS-312 Week#07
CSS-312 Week#07
Algorithm
Recursion
Recurrence relations
Solving Recurrence relation
Recursion
Have you ever seen a set
of Russian dolls? At first,
you see just one figurine,
usually painted wood, that
looks something like this:
Recursion
•You can remove the top
half of the first doll, and
what do you see inside?
Another, slightly smaller,
Russian d
Recursion
You can remove that doll
and separate its top and
bottom halves. And you
see yet another, even
smaller, doll:
Recursion
And once more:
Recursion
And you can keep going.
Eventually you find the
teeniest Russian doll. It is
just one piece, and so it
does not open:
Recursion
We started with one big Russian doll, and we saw smaller and smaller
Russian dolls, until we saw one that was so small that it could not
contain another.
What do Russian dolls have to do with algorithms? Just as one Russian
doll has within it a smaller Russian doll, which has an even smaller
Russian doll within it, all the way down to a tiny Russian doll that is too
small to contain another, we'll see how to design an algorithm to solve
a problem by solving a smaller instance of the same problem, unless
the problem is so small that we can just solve it directly. We call this
technique recursion.
Recursion
Recursion is the process of defining a problem (or the solution to a
problem) in terms of (a simpler version of) itself.
For example, we can define the operation "find your way home" as:
If you are at home, stop moving.
Take one step toward home.
"find your way home".
Recursion
end
Loops and Tail Recursion
Some recursive algorithms are very similar to loops. These
algorithms are called "tail recursive" because the last
statement in the algorithm is to "restart" the algorithm.
Tail recursive algorithms can be directly translated into loops.
The factorial function
For our first example of recursion, let's look at how to compute the
factorial function. We indicate the factorial of n by n!, !. It's just the
product of the integers 1 through n. For example, 5! equals 1.2.3.4.5, or
120.
Let's look at an example: computing 5!.
The factorial function
You can compute 5! as 5.4!.
Now you need to solve the subproblem of computing
4!, which you can compute as 4.3!.
Now you need to solve the subproblem of computing
3!, which is 3.2!.
Now 2!, which is 2.1!.
The factorial function
Now you need to compute 1!. You could say that 1! equals
1, because it's the product of all the integers from 1
through 1. Or you can apply the formula that 1!= 1.0!. Let's
do it by applying the formula.
We defined 0! to equal 1.
Now you can compute 1! = 1.0!= 1.
The factorial function
Now you need to compute 1!. You could say that 1! equals
1, because it's the product of all the integers from 1
through 1. Or you can apply the formula that 1!= 1.0!. Let's
do it by applying the formula.
We defined 0! to equal 1.
Now you can compute 1! = 1.0!= 1.
Having computed 1! = 11!=11, !, equals, 1, you can
compute 2! = 2⋅1! =2.
The factorial function
Having computed 2! = 2, you can compute 3! = 3⋅2!=6.
Having computed 3! = 6, you can compute 4! = 4⋅3!=24.
Finally, having computed 4! = 24, you can finish up by
computing 5! = 5⋅4! =120.
Working of Recursion in C++
void recurse()
{
... .. ...
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
... .. ...
}
Working of
Recursion in C++
Working of
Factorial
Program
Recurrence Relations
T(n) = Time required to solve a problem of size n
Recurrence relations are used to determine the running time of
recursive programs – recurrence relations themselves are recursive
T(0) = time to solve problem of size 0
Base Case
T(n) = time to solve problem of size n
Recursive Case
Solving Recurrence Relations
Realize that linear finite history constant coefficient recurrences always
can be solved.
Guess a solution and prove by induction.
Try back substituting until you know what is going on.
Recursion Trees
See if you can use the Master theorem to provide an instant asymptotic
solution
Solving Recurrence Relations
long power(long x, long n)
if (n == 0)
return 1;
else
return x * power(x, n-1);
T(0) = c1 for some constant c1
T(n) = c2 + T(n − 1) for some constant c2
Solving Recurrence Relations
T(0) = c1
T(n) = T(n − 1) + c2
If we knew T(n − 1), we could solve T(n).
T(n) = T(n − 1) + c2
Solving Recurrence Relations
T(0) = c1
T(n) = T(n − 1) + c2
If we knew T(n − 1), we could solve T(n).
T(n) = T(n − 1) + c2 T(n − 1) = T(n − 2) + c2
= T(n − 2) + c2 + c2
= T(n − 2) + 2c2
Solving Recurrence Relations
T(0) = c1
T(n) = T(n − 1) + c2
If we knew T(n − 1), we could solve T(n).
T(n) = T(n − 1) + c2 T(n − 1) = T(n − 2) + c2
= T(n − 2) + c2 + c2
= T(n − 2) + 2c2 T(n − 2) = T(n − 3) + c2
= T(n − 3) + c2 + 2c2
= T(n − 3) + 3c2
Solving Recurrence Relations
T(0) = c1
T(n) = T(n − 1) + c2
If we knew T(n − 1), we could solve T(n).
T(n) = T(n − 1) + c2 T(n − 1) = T(n − 2) + c2
= T(n − 2) + c2 + c2
= T(n − 2) + 2c2 T(n − 2) = T(n − 3) + c2
= T(n − 3) + c2 + 2c2
= T(n − 3) + 3c2 T(n − 3) = T(n − 4) + c2
= T(n − 4) + 4c2
Solving Recurrence Relations
T(0) = c1
T(n) = T(n − 1) + c2
If we knew T(n − 1), we could solve T(n).
T(n) = T(n − 1) + c2 T(n − 1) = T(n − 2) + c2
= T(n − 2) + c2 + c2
= T(n − 2) + 2c2 T(n − 2) = T(n − 3) + c2
= T(n − 3) + c2 + 2c2
= T(n − 3) + 3c2 T(n − 3) = T(n − 4) + c2
= T(n − 4) + 4c2
=...
= T(n − k) + kc2