We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Tips for using substitution method (cont.)
= Sometimes our guess could be not strong enough,
" We can revise the guess by subtracting a lower-order term.
Example: T(n) = T({n/2]) + T([n/2]) + 1, we guess the solution is T(n) = O(n)
Substituting the guess, we have:
T(n) < eln/2] + c[n/2] +1 =cn+1
However, this term never implies T(n) < cn for any choice of ¢
Solution: try a even stronger hypothesis (by subtracting a lower-order term), e.g., T(n) < cn —d,
where d > 0 is a constant. Now we have Oo
(n) < (c[n/2| — d) + (cln/2|— a) +1
_-» Itholds as long as d > 1!
oe iain ai ~Substitution method
= The substitution method for solving recurrences comprises two steps:
1. Guess the form of the solution
2. Use mathematical induction to find the constants and show that the solution works
= The name “substitution” comes from the operation that we substitute the guessed solution for
the function when applying the inductive hypothesis to smaller values
* A powerful method, but we have to guess the form first.Substitution method: Example
1, if
27 (In/2\) ¥
subarray algorithm and merge sort, -
1
ifn > 1 Which s similar to the maximum
Consider a recurrence: T(n) =
fai eeaden i A speci function of 8m)
bepower of 2
= We use upper bound as an/example
Our guess: T(n) = O(n Ign)
* Itrequires us to prove that 7(n) < en «Ign for an appropriate choice of constant c > 0, and for
n>No —t
i J
We start by ass a charlie bound holds for all positive m )” e2t
* Now, we have done the first stepSubstitution method: Example (cont.)
" After substitution,
T(n) < 2(cln/2] Ig(In/2))) +n + Remove floors
< 2(c(n/2) Ig(n/2)) +
=cnlg(n)—cnlg2+n This inequality holds as long as
Pert
=cnlg(n)+(1-c)n 1)
The second step is to prove that T(n) < cnlgn holds for some specific boundary conditions,
the starting point of the induction.
What about the base-case, n = 1?
However, T(1) = 1 > cl «1g 1, which violates the solutionSubstitution method: Example (cont.)
By observation, when n > 3, the recurrence does not directly depend on T(1)
0 T(4) = 2T(2) + 4, but 7(3) = 27(|3/2]) + 3 = 271) +3
Thus, we consider n = 2 and n = 3 as the boundary conditions
T(2) = 27(1) +2 = 4,7(3) = 271) +3 =5
Aslongas T(2) = 4 2
For all n > 3, T(n) < cnlgn holds with the choice c > 1, as proven in the first step of induction.
= Altogether, 7'(n) < cn ign always holds with ¢ > 2 and n > mg = 2 (smallest boundary
condition) —_
= Therefore, T(n) = O(nlgn). Done!Tips for using substitution method (cont.)
Sometimes our guess could be not strong enough.
We can revise the guess by subtracting a lower-order term.
Example: 7(n) = T({n/2J) + T({n/2]) + 1, we guess the solution is T(n) = O(n)
Substituting the guess, we have:
T(n) < ¢ln/2| + c[n/2| +1 =en+1