Analysis of Recursive Algorithms
Analysis of Recursive Algorithms
Analysis of Recursive Algorithms
> +
<
=
2 if ) 2 / ( 2
2 if
) (
n bn n T
n b
n T
Find a closed form for T(n) using iterative
substitution?
Iterative Substitution
Base case occurs when n= 2
i
, i=logn
So, T(n) = nT(1) + bn logn = bn + bnlogn = O(nlogn)
ibn n T
bn n T
bn n T
bn n T
bn n b n T
bn n T n T
i i
+ =
=
+ =
+ =
+ =
+ + =
+ =
) 2 / ( 2
...
4 ) 2 / ( 2
3 ) 2 / ( 2
2 ) 2 / ( 2
)) 2 / ( )) 2 / ( 2 ( 2
) 2 / ( 2 ) (
4 4
3 3
2 2
2
Master Theorem
Many divide-and-conquer recurrence equations have the form:
The Master Theorem:
> +
<
=
d n n f b n aT
d n c
n T
if ) ( ) / (
if
) (
. 1 some for ) ( ) / ( provided
)), ( ( is ) ( then ), ( is ) ( if 3.
) log ( is ) ( then ), ( is ) ( if 2.
) ( is ) ( then ), ( is ) ( if 1.
log
log log
log log
< s
O O
O O
O
+
o o
c
c
n f b n af
n f n T n n f
n n n T n n f
n n T n O n f
a
a a
a a
b
b b
b b
Master Theorem Examples
Example1 :
Solution: log
b
a=2, so case 1 says T(n) is O(n
2
).
Example2:
Solution: : log
b
a=1, so case 2 says T(n) is O(n log
2
n).
Example3:
Solution: log
b
a=0, so case 3 says T(n) is O(n log
n).
n n T n T + = ) 2 / ( 4 ) (
n n n T n T log ) 2 / ( 2 ) ( + =
n n n T n T log ) 3 / ( ) ( + =
Master Theorem Examples
Example4 :
Solution: log
b
a=0, so case 2 says T(n) is O(log n).
Example5:
Solution: log
b
a=2, so case 3 says T(n) is O(n
3
).
Example6:
Solution: log
b
a=3, so case 1 says T(n) is O(n
3
).
1 ) 2 / ( ) ( + = n T n T
3
) 3 / ( 9 ) ( n n T n T + =
2
) 2 / ( 8 ) ( n n T n T + =
Guess-and-Test Method
In the guess-and-test method, we guess a closed form solution
and then try to prove it is true by induction.
Example: Consider the following recurrence:
Guess1: T(n) < cn log n.
Wrong guess: we cannot make this last line be less than cn log n
> +
<
=
2 if log ) 2 / ( 2
2 if
) (
n n bn n T
n b
n T
n bn cn n cn
n bn n cn
n bn n n c
n bn n T n T
log log
log ) 2 log (log
log )) 2 / log( ) 2 / ( ( 2
log ) 2 / ( 2 ) (
+ =
+ =
+ =
+ =
Guess-and-Test Method
Guess 2: T(n) < cn log
2
n.
if c>b
So, T(n) is O(n log
2
n).
In general, to use this method, you need to have a good guess
and you need to be good at induction proofs.
n cn
n bn cn n cn n cn
n bn n cn
n bn n n c
n bn n T n T
2
2
2
2
log
log log 2 log
log ) 2 log (log
log )) 2 / ( log ) 2 / ( ( 2
log ) 2 / ( 2 ) (
s
+ + =
+ =
+ =
+ =