Recurrences and Methods For Solution
Recurrences and Methods For Solution
Recurrences and Methods For Solution
Recurrences
Methods for Solving
Recurrences
Recurrences --T ( n ) aT ( n / b ) f ( n )
Chapter 4
P.2
Example Recurrences
Recurrences --T ( n ) aT ( n / b ) f ( n )
Chapter 4
Substitution method
Recursion-tree method
Master method
P.5
Technicalities
Chapter 4
P.6
Iteration method
Substitution method
Master method
Iteration Method
Example
T(n) = n + 2T(n/2)
Assume: n = 2k
T(n) = n + 2T(n/2)
T(n/2) = n/2 + 2T(n/4)
= n + 2(n/2 + 2T(n/4))
= n + n + 4T(n/4)
= n + n + 4(n/4 + 2T(n/8))
= n + n + n + 8T(n/8)
= in + 2iT(n/2i)
= kn + 2kT(1)
10
= nlgn + nT(1) = (nlgn)
k = lgn & n = 2k
Chapter 4
P.11
Substitution method
Guess a solution
T(n) = O(g(n))
Induction goal: apply the definition of the asymptotic
notation
(strong induction)
Base case?
Example 2
T(n) = T(n-1) + n
Example 3
T(n) = 2T(n/2) + n
Base Case ?
Changing variables
T(n) = 2T( n ) + lgn
Rename: m = lgn n = 2m
T (2m) = 2T(2m/2) + m
Example
T ( n ) 2T ( n / 2 ) n
T (1 ) 1
Guess
Assume
Chapter 4
T ( n / 2 ) c n / 2 log n / 2
P.18
n
T ( n ) 2( c n / 2 log n / 2 ) n cn log n
2
cn log n cn log 2 n cn log n (if c 1.)
Chapter 4
P.19
We guessT ( n ) O( n log n )
Making guess provides loose upper
bound and lower bound. Then
improve the gap.
Chapter 4
P.20
Subtleties
T ( n ) T ( n / 2 ) T ( n / 2 ) 1
Guess T ( n ) O( n )
AssumeT ( n ) cn
T ( n ) c n / 2 c n / 2 1 cn 1 cn
T ( n ) cn b
However, assume
T ( n ) ( c n / 2 b ) ( c n / 2 b ) 1
cn 2 b 1 cn b ( Choose b 1 )
Chapter 4
P.21
Chapter 4
P.22
Chapter 4
P.23
Avoiding pitfalls
T ( n ) 2T ( n / 2 ) n
T (1 ) 1
AssumeT ( n ) O( n )
Hence T ( n ) cn
T (n) 2(c n / 2 ) n cn n O(n)
(Since c is a constant)
Chapter 4
Example 1
W(n) = 2W(n/2) + n2
Total cost:
n
1
1
1
lg n
2
2
2
W (n)
2
i 0
W(n) = O(n2)
2 W (1) n
i 0
nn
i 0
O(n) n
1 1
O ( n) 2n 2
2
26
T (n) 3T ( n / 4 ) (n 2 )
Chapter 4
P.27
Chapter 4
P.28
T (n) cn 2
log 4 n 1
i 0
3 2 3
3
2
cn
cn
...
16
16
16
i
3
log
2
cn
16
log 4 n 1
cn 2 (nlog 3 )
4
(3 / 16)log n 1 2
cn (n log 3 ).
(3 / 16) 1
4
By Equation A.5
T ( n)
log 4 n 1
i 0
i
3
log
2
cn n
16
3
log 3
2
cn
i 0 16
1
cn 2 n log
1 (3 / 16)
16
cn 2 ( n log 3 )
13
4
By Theorem
A.6
O(n 2 )
Chapter 4
P.30
substitution method
We want to Show that T(n) dn2 for some
constant d > 0. using the same constant c > 0
as before, we have
T ( n)
3T ( n / 4 ) cn 2
3d n / 4
cn 2
3d (n / 4) 2 cn 2
3 2
dn cn 2
16
dn 2 ,
P.31
T (n) T (n / 3) T (2n / 3) cn
Chapter 4
P.32
substitution method
T (n) T (n / 3) T (2n / 3) cn
dn lg n dn(lg 3 2 / 3) cn
dn lg n,
Chapter 4
P.33
Masters method
n
T (n) aT f (n)
b
34
Masters method
n
T (n) aT f (n)
b
where, a 1, b > 1, and f(n) > 0
Case 1: if f(n) = O(nlogba -) for some > 0, then: T(n) = (nlogba)
Case 2: if f(n) = (nlogba), then: T(n) = (nlogba lgn)
Case 3: if f(n) = (nlogba +) for some > 0, and if
af(n/b) cf(n) for some c < 1 and all sufficiently large n, then:
regularity conditionT(n) = (f(n))
35
Masters method
36
Chapter 4
P.39
Chapter 4
P.40
Examples
T(n) = 2T(n/2) + n
a = 2, b = 2, log22 = 1
Compare nlog22 with f(n) = n
41
Examples
T(n) = 2T(n/2) + n2
a = 2, b = 2, log22 = 1
Compare n with f(n) = n2
f(n) = (n1+) Case 3 verify regularity cond.
a f(n/b) c f(n)
2 n2/4 c n2 c = is a solution (c<1)
T(n) = (n2)
42
Examples (cont.)
T(n) = 2T(n/2) +
a = 2, b = 2, log22 = 1
Compare n with f(n) = n1/2
f(n) = O(n1-)
Case 1
T(n) = (n)
43
Examples
T(n) = 3T(n/4) + nlgn
a = 3, b = 4, log43 = 0.793
Compare n0.793 with f(n) = nlgn
f(n) = (nlog43+) Case 3
Check regularity condition:
3(n/4)lg(n/4) (3/4)nlgn = c f(n), c=3/4
T(n) = (nlgn)
44
Examples
T(n) = 2T(n/2) + nlgn
a = 2, b = 2, log22 = 1
Compare n with f(n) = nlgn
seems like case 3 should apply
45
Chapter 4