Substitution Method
Substitution Method
Substitution Method
Divide-and conquer is a
general algorithm design
paradigm:
Divide: divide the input data S in
two or more disjoint subsets S1,
S2 , …
Recur: solve the subproblems
recursively
Conquer: combine the solutions
for S1, S2, …, into a solution for S
The base case for the
recursion are subproblems of
constant size
Analysis can be done using
recurrence equations
Analysis of Algorithms 1
Iterative Substitution
In the iterative substitution, or “plug-and-chug,” technique, we
iteratively apply the recurrence equation to itself and see if we can
find a pattern: T ( n ) 2T ( n / 2) bn
2( 2T ( n / 22 )) b( n / 2)) bn
22 T (n / 22 ) 2bn
23 T ( n / 23 ) 3bn
24 T (n / 24 ) 4bn
...
2i T ( n / 2i ) ibn
Note that base, T(n)=b, case occurs when 2i=n. That is, i = log n.
So, T (n) bn bn log n
Thus, T(n) is O(n log n).
Analysis of Algorithms 2
Solving Recurrences by
Substitution: Guess-and-Test
Guess the form of the solution
(Using mathematical induction) find the constants and
show that the solution works
Example
T(n) = 2T(n/2) + n
Guess (#1) T(n) = O(n)
Need T(n) <= cn for some constant c>0
Assume T(n/2) <= cn/2 Inductive hypothesis
Thus T(n) <= 2cn/2 + n = (c+1) n
Our guess was wrong!!
COT 5407 3
Solving Recurrences by
Substitution: 2
T(n) = 2T(n/2) + n
Guess (#2) T(n) = O(n2)
Need T(n) <= cn2 for some constant c>0
Assume T(n/2) <= cn2/4 Inductive hypothesis
Thus T(n) <= 2cn2/4 + n = cn2/2+ n
Works for all n as long as c>=2 !!
But there is a lot of “slack”
COT 5407 4
Solving Recurrences by
Substitution: 3
T(n) = 2T(n/2) + n
Guess (#3) T(n) = O(nlogn)
Need T(n) <= cnlogn for some constant c>0
Assume T(n/2) <= c(n/2)(log(n/2)) Inductive hypothesis
Thus T(n) <= 2 c(n/2)(log(n/2)) + n
<= cnlogn -cn + n <= cnlogn
Works for all n as long as c>=1 !!
This is the correct guess. WHY?
Analysis of Algorithms 6
More Examples
Recall the recurrence equation:
b if n 2
T (n)
2T (n / 2) bn log n if n 2
Guess #2: T(n) < cn log2 n.
T (n) 2T (n / 2) bn log n
2(c(n / 2) log 2 (n / 2)) bn log n
cn(log n log 2) 2 bn log n
cn log 2 n 2cn log n cn bn log n
cn log 2 n
if c > b.
So, T(n) is O(n log2 n).
In general, to use this method, you need to have a good guess
and you need to be good at induction proofs.
Analysis of Algorithms 7
Solving Recurrences: Recursion-tree
method
Substitution method fails when a good guess is not available
Recursion-tree method works in those cases
Write down the recurrence as a tree with recursive calls as the
children
Expand the children
COT 5407 8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Analysis of Algorithms 11