Chapter 4
Chapter 4
Sun-Yuan Hsieh
謝孫源 教授
成功大學資訊工程學系
Overview
2
1 if n 1
Examples: T n
T n 1 1 if n 1
Solution : T n n.
1 if n 1
T n
2T n / 2 n if n 1
Solution : T n n lg n n.
0 if n 2
T n
T n 1 if n 2
Solution : T n lg lg n.
1 if n 1
T n
T n / 3 T 2n / 3 n if n 1
Solution : T ( n) ( n lg n)
3
Many technical issues:
Floors and ceilings
[Floors and ceilings can easily be removed and
don’t affect the solution to the recurrence.]
Exact vs. asymptotic functions
Boundary condition
4
In algorithm analysis, we usually express both the recurrence and
its solution using asymptotic notation.
E.g.
1 if n 1 ,
T n
2T n / 2 n if n 1 .
1. Guess: T(n) = nlgn + n. [Here, we have a recurrence with an
exact function, rather than asymptotic notation, and the solution
is also exact rather than asymptotic. We’ll have to check
boundary conditions and the base case.]
6
Substitution method (cont’d)
2. Induction:
Bass: n = 1 nlgn + n = 1 = T(n)
Inductive step: Inductive hypothesis is that T(k) = klgk + k for all k <
n.
We’ll use this
ninductive
hypothesis for T(n/2).
T n 2T n
2
n n n
2 lg n
2 2 2
n
n lg n n
2
n lg n lg 2 n n
n lg n n n n.
n lg n n 7
Substitution method (cont’d)
T n 2T n / 2 n
Assume
T n O1 for sufficient ly small n
Express the solution by asymptotic notation:
8
Substitution method (cont’d)
9
Substitution method (cont’d)
For the substitution method:
Name the constant in the additive term
Show the upper (O) and lower (Ω) bounds separately.
Might need to use different constants for each notation
10
1. Upper bound:
Guess: T n dn lg n for some positive constant d. We are given c in the
recurrence, and we get to choose d as any positive constant. It’s OK for d to
depend on c.
Substitution:
T n 2T n / 2 cn
n n
2 d lg cn
2 2
n
dn lg cn
2
Therefore, T(n) = O(n lg n)
dn lg n-dn cn
dn lg n if -dn cn 0 ,
d c
11
2. Lower bound:
Write for some positive constant c.
n 2T n / 2 cn for some positive constant d.
TGuess:
Substitution:
T n dn lg n
T n 2T n / 2 cn
n n
2 d lg cn
2 2
n
dn lg cn
2
Therefore, T(n) = Ω(ndnlg lg
n).n dn cn
Therefore, T(n) = Θ( n lg n )
[For
dn this
lg n particularif recurrence,
dn cn we
0 , can use d = c for
both the upper-bound and lower-bound proofs. That won’t always be the case.]
d c
12
Substitution method (cont’d)
Make sure you show the same exact form when doing a
substitution proof.
Consider the recurrence
T(n) = 8T(n/2) + Θ(n2) .
For an upper bound:
Guess: T(n) ≤ dn3 2
T n 8T n / 2 cn
3
T n 8d n / 2 cn 2
8d n 3 / 8 cn 2
dn 3 cn 2
dn3 doesn' t work!
13
Substitution method (cont’d)
14
Substitution method (cont’d)
15
Recurrence trees
16
Recurrence trees
17
Recurrence trees
cn 2 cn 2
c( n4 ) 2 c( n4 ) 2 c( n4 ) 2 3
16 cn 2
log 4 n
c( 16n ) 2 c( 16n ) 2 c( 16n ) 2 c( 16n ) 2 c( 16n ) 2 c( 16n ) 2 c( 16n ) 2 c( 16n ) 2 c( 16n ) 2 ( 163 ) 2 cn 2
T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) T (1) (n log4 3 )
n log4 3
(d) Total : O(n 2 )
18
Recurrence trees
16 16 16
log 4 n 1 i
3 2
cn n
16
log 4
3
i 0
(3 / 16)log n 1 2
4
cn (nlog 3 ). 4
(3 / 16) 1
19
Recurrence trees
log 4 n 1 i
3 2
T ( n)
16
cn n log
4
3
i 0
i
3 2
cn nlog 3 4
i 0 16
1
1 (3 / 16)
cn 2 n log 4 3
16 2
cn ( nlog 3 ) 4
13
O(n 2 )
20
Recurrence trees
21
Recurrence trees
c(n/3) c(2n/3) cn
There are log3n full levels, and after log3/2n levels, the
problem size is down to 1.
1. Upper bound:
Guess: T(n) ≤ dnlgn.
Substitution:
T(n) ≤ T(n/3) + T(2n/3) +cn
≤ d (n/3) lg (n/3) + d(2n/3) lg (2n/3) + cn
= (d (n/3) lg n – d (n/3)lg3) + (d (2n/3) lgn – d (2n/3) lg (3/2) ) + cn
= d n lg n – d ((n/3) lg3 + (2n/3) lg (3/2)) + cn
= d n lg n – d ((n/3) lg3 + (2n/3) lg3 – (2n/3) lg2) +cn
= d n lg n – d n (lg3 – 2/3) + cn
≤ d n lg n if – d n (lg3 – 2/3) + cn ≤ 0,
c
d ≥
lg 3 2 / 3
Therefore, T(n) = O(n lg n).
Note: Make sure that symbolic constants used in the recurrence (e.g.,c)
and the guess (e.g.,d) are different.
24
Recurrence trees (cont’d)
2. Lower bound:
Guess: T(n) dn lg n.
Substitution: Same as for the upper bound, but replacing ≤ by ≥. End up
needing
c
0d
lg 3 2 / 3
Therefore, T(n) = Ω(n lg n).
Since T(n) = O(n lg n) and T(n) = Ω(n lg n), we conclude that T(n) = Θ(n lg n)
25
Master method (cond’t)
26
Master method (cond’t)
Simple case:
k =0 f (n) = Θ( nlogba ) T(n) = Θ( nlogba lg n)
27
Master method (cond’t)
28
Master method (cond’t)
29
Master method (cond’t)
Examples:
T(n) = 5T(n/2) + Θ(n2)
nlog25 vs. n2
Since log25 – ε = 2 for some constant ε > 0, use Case 1 T(n) =
Θ(nlg5)
30
Master method (cond’t)