Lect03 06
Lect03 06
Data Structures, Spring 2006 © L. Joskowicz Data Structures, Spring 2006 © L. Joskowicz
Solving recurrence equations The substitution method
The solution to the equation T(n) = 2T(n/2) + n is O(n lg n)
Three ways to solve recurrence equations: for n 2; assume T(1) = 1
• Substitution: guess a bound and use mathematical Prove: T(n) c(n lg n) for c 2
induction to prove the guess correct. Base case: T(2) c 2lg2, which holds for c 2 since T(2) = 3
General case:
• Recursion-tree: convert the recurrence into a tree
Assume that it holds for n/2, that is: T(n/2) 2(cn/2 lg (n/2))
whose nodes represent the costs at each level and
Substitute into the recurrence relation and prove for n:
use bounding summations to solve the recurrence.
T(n) 2(cn/2 lg (n/2) + n
• Master method: apply a theorem for recurrences cn lg (n/2) + n
of the form T(n) = aT(n/b) + nc cn lg n – cn lg 2 + n
where a, b, c are constants. cn lg n – cn + n
cn lg n for c 1
Data Structures, Spring 2006 © L. Joskowicz Data Structures, Spring 2006 © L. Joskowicz
Data Structures, Spring 2006 © L. Joskowicz Data Structures, Spring 2006 © L. Joskowicz
Recurrence equation of master theorem Recursion-tree for the equation
T(n) = aT(n/b) + nc i=0 n a c
n
T(n/b) = aT(n/b2) + (n/b)c c c c c a a
i=1 n n n ... n b
T(n/b2) = aT(n/b3) + (n/b2)c b b b b
a a
T(n/b4) = aT(n/b5) + (n/b4)c c c c c c
i=2 n ... n ... n ... n 2 n
Now substitute: a2 a
b2 b2 b2 b2 b2
T(n) = aT(n/b) + nc ...
= a[aT(n/b2) + (n/b)c] + nc c
... c c
... ...
c
n n n n c
= a[a[aT(n/b3) + (n/b2)c]+ (n/b)c] + nc i=k
bk bk bk
... ... n
ak
bk bk
= akT(n/bk) + nc[1 + a(1/b)c + a2(1/b2)c +…ak–1 (1/bk–1 )c]
k −1 c
n k= logbn, the depth of the recursion
= a k T ( n bk ) + ai k= logbn, is the depth log b n −1
n
c
i =0 bi
of the recursion
n
= Θ(1) Θ( n log b a ) + ai
Data Structures, Spring 2006 © L. Joskowicz Data Structures, Spring 2006 © L. Joskowicz
b log b n i =0 bi
... Θ(1)
( )
i=k Θ(1) Θ(1) Θ(1) Θ(1) ak
log b a +ε
3. If f (n) = Ω n ε > 0 and if a f (n / b) ≤ c f ( n)
log b n −1
for some c < 1 and sufficiently large n, then k= logbn, the depth of the recursion n
Θ( n log b a ) + ai f
T ( n ) = Θ( f ( n ) ) i =0 bi
Data Structures, Spring 2006 © L. Joskowicz Data Structures, Spring 2006 © L. Joskowicz