When an algorithm contains a recursive call to itself, its
running time can often be described by a recurrence. A
recurrence is an equation or inequality that describes a
function in terms of its value on smaller inputs.
For example, the merge sort that we looked at in the first lecture
can be expressed as a recurrence:
To solve recurrences, we will be focusing on the following methods:
Substitution method: guess a bound and then use
mathematical induction to prove our guess correct.
Iteration method: convert the recurrence into a summation
and then rely on techniques for bounding summations to solve the
recurrence.
use mathematical induction to find the constants and show the
solution works.
For example, let's take the recurrence
, and try to find an upper bound for it. We guess that
the solution is
. Now we need to prove that
for some
. Assuming that this bound
holds for
, we substitute into the equation:
where the last step holds as long as
.
We now need to prove that our solution holds for the boundary
conditions. Asymptotic notation only requires us to prove for
, hence we can choose
and
. Substituting in, it
is clear that the solution holds for both of these.
The iteration method does not require guessing the answer, but it
may require more algebra than the substitution method. The idea is
to expand (iterate) the recurrence and express it as a summation of
terms dependent only on
and the initial conditions. Consider
our example recurrence again.
The first step is expanding the recurrence into a series. A
convenient "trick" to do this is to reshuffle the recurrence such
that the "work done" part "
" is in front and the recursion
part "
" at the back.
Next expand the recursion part by substituting
into
.
Simplify.
Repeat substituting and simplifying until an observable pattern
forms.
It has become apparent that the sequence will continue as a series
of
additions and stop at
as defined earlier "
". The next step is to work out the number of
additions in the series. This turns out to be the number of times
can be divided by 2 until the value is 1. Let
be this
number.
Knowing that there are
number of
additions in the
series, we can solve the recurrence.
Now,
was chosen because it was defined above that
is a
constant if
. However, 1 is a constant and if the problem size
is a constant, the runtime can also be expected to be some constant!
Thus, the series could have ended at
if we wanted to. While
this would have changed the number of
additions in the series,
the final solution will still be the same. Assume that we stopped
the series at
, and that the runtime of
is
.
Then...
Substituting this into the recurrence gives us the same result as
before.
File translated from
TEX
by
TTM,
version 3.67. On 31 Mar 2006, 18:12.