- T(n)=2T(n/2+17)+n=O(nlgn)
Substitution method
- T(n)=T(n−1)+n=O(n2) (quick sort worst case (e7.2-1))
- T(n)=T(n−1)+1=O(n) (worst-case of Random in Ramdomized-Quicksort (e7.3-2))
- T(n)=2T(⌊n⌋)+lgn=Θ(lgnlglgn) (4.1)
- T(n)=T(⌈n/2⌉)+1=Θ(lgn) (e4.1-1)
Rucursion-tree method
Changing variables
- Given T(n)=aT(n1/2)+lgqn
- Define: m=lgn⟹2m=n
- Therefore, T(2m)=aT(2m/2)+mq
Master Theorem
T(n)=aT(n/b)+f(n)(a≥1,b>1)
| | |
---|
1. | f(n)=O(nlogba−ϵ) for some constant ϵ>0 | T(n)=Θ(nlogba) |
2. | f(n)=Θ(nlogba) | T(n)=Θ(nlogbalgn) |
3. | f(n)=Ω(nlogba+ϵ) for some constant ϵ>0, and if af(n/b)≤cf(n) for some constant c<1 and all sufficiently large n | T(n)=Θ(f(n)) |
2 -
T(n)=aT(n/b)+Θ(nklogpn)
- Case 1. if logba>k (i.e. a>bk) then:
- Case 2. if logba=k (i.e. a=bk) then:
- (2a) if p>−1, then T(n)=Θ(nklog(p+1)n)=Θ(nlogbalog(p+1)n)
- (2b) if p=−1, then T(n)=Θ(nkloglogn)=Θ(nlogbaloglogn)
- (2c) if p<−1, then T(n)=Θ(nk)=Θ(nlogba)
- Case 3. if logba<k (i.e. a<bk) then:
- (3a) if p≥0, then T(n)=Θ(nklogpn)
- (3b) if p<0, then T(n)=Θ(nk)
Exmaples
- Case 1:
- (a=4,b=2,k=1,p=0) T(n)=4T(n/2)+Θ(n)=Θ(nlog24)=Θ(n2)
- (a=9,b=3,k=1,p=0) T(n)=9T(n/3)+n=Θ(nlog39)=Θ(n2)
- T(n)=4T(n/2)+Θ(n)=Θ(n2)
- T(n)=2T(n/2)+Θ(1)=Θ(n) (best-case of Random in Ramdomized-Quicksort (e7.3-2))
- T(n)=3T(n/2)+Θ(n)=O(nlg3)
- Case 2:
- (2a)
- (a=1,b=32,p=0>−1,k=0) T(n)=T(2n/3)+Θ(1)=Θ(nlog2/31lgn)=Θ(n0lgn)=Θ(lgn) (max-heapify)
- T(n)=2T(n/2)+Θ(n)=Θ(nlgn) (quick-sort best case; merge-sort)
- T(n)=T(n/2)+Θ(1)=O(lgn)
- Case 3:
- (3a)
- (a=1,b=2,k=2,p=0) T(n)=T(n/2)+n2=Θ(n2log0n)=Θ(n2)
- (a=3,b=4,k=1,p=1≥0) T(n)=3T(n/4)+nlgn=Θ(nlgn)
- T(n)=2T(n/3)+Θ(n)=Θ(n)