Substitution Method
The previous method used to
discover the run time of binary search is useful but, possibly tedious.
A much easier technique is called the substitution method in which
we use our experience in analyzing algorithms to guess the running
time of an algorithm and then determine whether or not our guess was correct.
As an example, suppose we guess that the running time of binary search
is O(log2n); that is, we guess that T(n) <= clog2n for all n >=
n0.
We prove that this is true using induction:
base step:
We cannot use n=1 because T(1) <= T(1/2) + k1 = c1 + k1 > 0 and
clog21 = 0.
We instead use 2 <= n <= 4 since then T(n) <= T(2) + k1 <=
T(1) + 2k1 <= T(1/2) + 3k1 <= c1 + 3k1 and clog2n >= c.
Thus, for 2 <= n <= 4, T(n) <= clog2n provided that c1 + 3k1
<= c.
induction step:
Assume that T(n) <= clog2n for all 2 <= n <= k for some constant
k >= 4.
We now show that T(k+1) <= clog2(k+1):
T(k+1) <= T((k+1)/2) + k1 by the recurrence inequality for T(n)
<=
clog2((k+1)/2) + k1 by our induction assumption -- (k+1)/2 <= k
=
clog2(k+1) - clog2(2) + k1 because log(a/b) = log(a) - log(b)
=
clog2(k+1) - c + k1
<=
clog2(k+1) when -c + k1 <= 0 (which is true whenever k1 <= c)
If we let c = c1 + 3k1 and n0=2 then the above induction proof holds.
Thus, T(n) is O(log2n).
|