Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Substitution Method

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Divide-and-Conquer

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?

9/9/08 COT 5407 5


More Examples
In the guess-and-test method, we guess a closed form solution
and then try to prove it is true by induction:
 b if n  2
T (n)  
2T (n / 2)  bn log n if n  2
Guess: T(n) < cn log n.
T (n )  2T (n / 2)  bn log n
 2(c(n / 2) log( n / 2))  bn log n
 cn(log n  log 2)  bn log n
 cn log n  cn  bn log n

Wrong: we cannot make this last line be less than cn log n

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

 Add up each level

 Sum up the levels

Useful for analyzing divide-and-conquer algorithms


Also useful for generating good guesses to be used by substitution
method

COT 5407 8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

9/9/08 COT 5407 9


Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

9/9/08 COT 5407 10


Master Method
Many divide-and-conquer recurrence equations have
the form:
 c if n  d
T (n)  
aT (n / b)  f (n ) if n  d

The Master Theorem:


1. if f (n) is O(n log b a  ), then T (n) is (n log b a )
2. if f (n) is (n log b a log k n), then T (n) is (n log b a log k 1 n)
3. if f (n) is (n log b a  ), then T (n) is ( f (n)),
provided af (n / b)  f (n) for some   1.

Analysis of Algorithms 11

You might also like