(叶节点数:
a
l
o
g
b
n
=
(
n
l
o
g
n
a
)
l
o
g
b
n
=
n
l
o
g
b
a
a^{log_bn}=(n^{log_na})^{log_bn}=n^{log_ba}
alogbn=(nlogna)logbn=nlogba)
T
(
n
)
=
Θ
(
n
l
o
g
b
a
)
+
∑
j
=
0
l
o
g
b
n
−
1
a
j
f
(
n
b
j
)
T(n)=\Theta(n^{log_ba})+\sum_{j=0}^{log_bn-1}a^jf(\frac{n}{b^j})
T(n)=Θ(nlogba)+∑j=0logbn−1ajf(bjn)
记
g
(
n
)
=
∑
j
=
0
l
o
g
b
n
−
1
a
j
f
(
n
b
j
)
g(n)=\sum_{j=0}^{log_bn-1}a^jf(\frac{n}{b^j})
g(n)=∑j=0logbn−1ajf(bjn)
T
(
n
)
=
Θ
(
n
l
o
g
b
a
)
+
g
(
n
)
T(n)=\Theta(n^{log_ba})+g(n)
T(n)=Θ(nlogba)+g(n)
主定理的核心是比较
递归树上面那些
f
(
n
)
f(n)
f(n)操作加起来更耗时, 还是最下层所有叶节点的
O
(
1
)
O(1)
O(1)加起来更耗时
情况一:所有叶节点的
O
(
1
)
O(1)
O(1)加起来更耗时
即
f
(
n
)
=
O
(
n
l
o
g
b
a
−
ϵ
)
,
ϵ
>
0
f(n)=O(n^{log_ba-\epsilon}),\epsilon>0
f(n)=O(nlogba−ϵ),ϵ>0
g
(
n
)
=
O
(
∑
j
=
0
l
o
g
b
n
−
1
a
j
(
n
b
j
)
l
o
g
b
a
−
ϵ
)
g(n)=O(\sum_{j=0}^{log_bn-1}a^j(\frac{n}{b^j})^{log_ba-\epsilon})
g(n)=O(∑j=0logbn−1aj(bjn)logba−ϵ)
∑
j
=
0
l
o
g
b
n
−
1
a
j
(
n
b
j
)
l
o
g
b
a
−
ϵ
\sum_{j=0}^{log_bn-1}a^j(\frac{n}{b^j})^{log_ba-\epsilon}
∑j=0logbn−1aj(bjn)logba−ϵ
=
n
l
o
g
b
a
−
ϵ
∑
j
=
0
l
o
g
b
n
−
1
(
a
b
ϵ
b
l
o
g
b
a
)
j
=n^{log_ba-\epsilon}\sum_{j=0}^{log_bn-1}(\frac{ab^\epsilon}{b^{log_ba}})^j
=nlogba−ϵ∑j=0logbn−1(blogbaabϵ)j
=
n
l
o
g
b
a
−
ϵ
∑
j
=
0
l
o
g
b
n
−
1
(
b
ϵ
)
j
=n^{log_ba-\epsilon}\sum_{j=0}^{log_bn-1}(b^\epsilon)^j
=nlogba−ϵ∑j=0logbn−1(bϵ)j
=
n
l
o
g
b
a
−
ϵ
(
b
ϵ
)
l
o
g
b
n
−
1
b
ϵ
−
1
=n^{log_ba-\epsilon}\frac{(b^\epsilon)^{log_bn}-1}{b^\epsilon-1}
=nlogba−ϵbϵ−1(bϵ)logbn−1
=
n
l
o
g
b
a
−
ϵ
n
ϵ
−
1
b
ϵ
−
1
=n^{log_ba-\epsilon}\frac{n^\epsilon-1}{b^\epsilon-1}
=nlogba−ϵbϵ−1nϵ−1
所以
g
(
n
)
=
O
(
n
l
o
g
b
a
)
g(n)=O(n^{log_ba})
g(n)=O(nlogba)
则
T
(
n
)
=
Θ
(
n
l
o
g
b
a
)
+
O
(
n
l
o
g
b
a
)
=
Θ
(
n
l
o
g
b
a
)
T(n)=\Theta(n^{log_ba})+O(n^{log_ba})=\Theta(n^{log_ba})
T(n)=Θ(nlogba)+O(nlogba)=Θ(nlogba)
情况二:一样耗时
即
f
(
n
)
=
Θ
(
n
l
o
g
b
a
log
k
n
)
,
k
≥
0
f(n)=\Theta(n^{log_ba}\log^kn),k\geq0
f(n)=Θ(nlogbalogkn),k≥0
g
(
n
)
=
Θ
(
∑
j
=
0
l
o
g
b
n
−
1
a
j
(
n
b
j
)
l
o
g
b
a
log
k
n
b
j
)
g(n)=\Theta(\sum_{j=0}^{log_bn-1}a^j(\frac{n}{b^j})^{log_ba}\log^k\frac{n}{b^j})
g(n)=Θ(∑j=0logbn−1aj(bjn)logbalogkbjn)
∑
j
=
0
l
o
g
b
n
−
1
a
j
(
n
b
j
)
l
o
g
b
a
log
k
n
b
j
\sum_{j=0}^{log_bn-1}a^j(\frac{n}{b^j})^{log_ba}\log^k\frac{n}{b^j}
∑j=0logbn−1aj(bjn)logbalogkbjn
=
n
l
o
g
b
a
∑
j
=
0
l
o
g
b
n
−
1
(
a
b
l
o
g
b
a
)
j
(
log
n
−
j
log
b
)
k
=n^{log_ba}\sum_{j=0}^{log_bn-1}(\frac{a}{b^{log_ba}})^j(\log n-j\log b)^k
=nlogba∑j=0logbn−1(blogbaa)j(logn−jlogb)k
=
n
l
o
g
b
a
∑
j
=
0
l
o
g
b
n
−
1
(
log
n
−
j
log
b
)
k
=n^{log_ba}\sum_{j=0}^{log_bn-1}(\log n-j\log b)^k
=nlogba∑j=0logbn−1(logn−jlogb)k
g
(
n
)
=
Θ
(
n
l
o
g
b
a
∑
j
=
0
l
o
g
b
n
−
1
log
k
n
)
g(n)=\Theta(n^{log_ba}\sum_{j=0}^{log_bn-1}\log^k n)
g(n)=Θ(nlogba∑j=0logbn−1logkn)
=
Θ
(
n
l
o
g
b
a
⋅
l
o
g
b
n
⋅
log
k
n
)
=\Theta(n^{log_ba}\cdot log_bn\cdot \log^kn)
=Θ(nlogba⋅logbn⋅logkn)
=
Θ
(
n
l
o
g
b
a
log
k
+
1
n
)
=\Theta(n^{log_ba}\log^{k+1}n)
=Θ(nlogbalogk+1n)
T
(
n
)
=
Θ
(
n
l
o
g
b
a
)
+
Θ
(
n
l
o
g
b
a
log
k
+
1
n
)
=
Θ
(
n
l
o
g
b
a
log
k
+
1
n
)
T(n)=\Theta(n^{log_ba})+\Theta(n^{log_ba}\log^{k+1}n)=\Theta(n^{log_ba}\log^{k+1}n)
T(n)=Θ(nlogba)+Θ(nlogbalogk+1n)=Θ(nlogbalogk+1n)
情况三:上面那些
f
(
n
)
f(n)
f(n)操作加起来更耗时
即
f
(
n
)
=
Ω
(
n
l
o
g
b
a
+
ϵ
)
,
ϵ
≥
0
f(n)=\Omega(n^{log_ba+\epsilon}),\epsilon\geq 0
f(n)=Ω(nlogba+ϵ),ϵ≥0且
∃
c
<
1
,
n
0
\exists c<1,n_0
∃c<1,n0,使得
∀
n
>
n
0
,
a
f
(
n
b
)
≤
c
f
(
n
)
\forall n>n_0,af(\frac{n}{b})\leq cf(n)
∀n>n0,af(bn)≤cf(n)
∀
j
≥
1
,
a
j
f
(
n
b
j
)
≤
c
j
f
(
n
)
\forall j\geq 1,a^jf(\frac{n}{b^j})\leq c^jf(n)
∀j≥1,ajf(bjn)≤cjf(n)
所以
∑
j
=
0
l
o
g
b
n
−
1
a
j
f
(
n
b
j
)
≤
f
(
n
)
∑
j
=
0
l
o
g
b
n
−
1
c
j
=
f
(
n
)
1
−
c
l
o
g
b
n
1
−
c
\sum_{j=0}^{log_bn-1}a^jf(\frac{n}{b^j})\leq f(n)\sum_{j=0}^{log_bn-1}c^j=f(n)\frac{1-c^{log_bn}}{1-c}
∑j=0logbn−1ajf(bjn)≤f(n)∑j=0logbn−1cj=f(n)1−c1−clogbn
所以
g
(
n
)
=
O
(
f
(
n
)
)
g(n)=O(f(n))
g(n)=O(f(n))
又
g
(
n
)
=
∑
j
=
0
l
o
g
b
n
−
1
a
j
f
(
n
b
j
)
≥
f
(
n
)
g(n)=\sum_{j=0}^{log_bn-1}a^jf(\frac{n}{b^j})\geq f(n)
g(n)=∑j=0logbn−1ajf(bjn)≥f(n)
所以
g
(
n
)
=
Ω
(
f
(
n
)
)
g(n)=\Omega(f(n))
g(n)=Ω(f(n))
所以
g
(
n
)
=
Θ
(
f
(
n
)
)
g(n)=\Theta(f(n))
g(n)=Θ(f(n))
T
(
n
)
=
Θ
(
n
l
o
g
b
a
)
+
Θ
(
f
(
n
)
)
=
Θ
(
f
(
n
)
)
T(n)=\Theta(n^{log_ba})+\Theta(f(n))=\Theta(f(n))
T(n)=Θ(nlogba)+Θ(f(n))=Θ(f(n))
主定理:
简记: