递归算法时间复杂度分析
等比数列和公式:
Sn=1−qa1(1−qn)
Sn=1−qa1−anq
Sn=q−1qan−a1
example 1
T(n)=2∗T(2n)+n
T(n)=2∗T(2n)+n=n+n+n......+n=n∗log2n=O(n∗log2n)
example 2
T(n)=2∗T(2n)+n2
T(n)=2∗T(2n)+n2=n2+2n2+4n2+8n2......2log2n−1n2=n2∗1−211−(21)log2n=2n2(1−n1)=2n2−2n=O(n2)
Master method
T(n)=a∗T(bn)+f(n)
case 1
Leaves grow faster than f
f(n)=O(nlogba−δ)T(n)=θ(nlogba)
case 2
Leaves grow at the same rate as f
f(n)=θ(nlogba)T(n)=θ(nlogba∗logbn)
case 3
f grows faster than leaves
f(n)=Ω(nlogba+k)T(n)=θ(f(n))
Exercise
T(n)=4∗T(2n)+n
a = 4 b=2
nlogba=n2
符合 case 1
T(n)=θ(n2)