算法导论习题解答 17-4-3

Exercise 17.4-3

假定我们改变表收缩的方式,不是当装载因子小于 1 / 4 1/4 1/4时将表规模减半,而是当装载因子小于 1 / 3 1/3 1/3时将表规模变为原来的 2 / 3 2/3 2/3。使用势函数
Φ ( T ) = ∣ 2 ⋅ T . n u m − T . s i z e ∣ \Phi(T) = |2 \cdot T.num-T.size| Φ(T)=2T.numT.size

证明:使用此策略, T A B L E − D E L E T E TABLE-DELETE TABLEDELETE操作的摊还代价的上界是一个常数。

Answer:

注意到初始时 Φ 0 = ∣ 2 ⋅ 0 − 0 ∣ = 0 \Phi_0 = |2 \cdot 0-0| = 0 Φ0=200=0,所以在第 i i i次操作之前有:
∑ j = 1 i − 1 C j ^ = ∑ j = 1 i − 1 C j + ( Φ j − Φ j − 1 ) = Φ i − 1 − Φ 0 + ∑ j = 1 i − 1 C j = Φ i − 1 + ∑ j = 1 i − 1 C j \sum_{j=1}^{i-1}\hat{C_j} = \sum_{j=1}^{i-1} C_j +( \Phi_{j} - \Phi_{j-1}) = \Phi_{i-1} - \Phi_0 + \sum_{j=1}^{i-1} C_j = \Phi_{i-1} + \sum_{j=1}^{i-1} C_j j=1i1Cj^=j=1i1Cj+(ΦjΦj1)=Φi1Φ0+j=1i1Cj=Φi1+j=1i1Cj

已知 ∀ i \forall i i Φ i ≥ 0 \Phi_i \ge 0 Φi0,故 ∀ i \forall i i
∑ j = 1 i C j ^ ≥ ∑ j = 1 i C j \sum_{j=1}^{i}\hat{C_j} \ge \sum_{j=1}^{i} C_j j=1iCj^j=1iCj

下证 ∑ j = 1 n C j ^ = O ( n ) \sum_{j=1}^{n}\hat{C_j} = O(n) j=1nCj^=O(n),在操作为 T A B L E − D E L E T E TABLE-DELETE TABLEDELETE的情况下,有 n u m i = n u m i − 1 − 1 num_i = num_{i-1} - 1 numi=numi11,当装载因子 α ≥ 1 / 2 \alpha \ge 1/2 α1/2 s i z e i = s i z e i − 1 size_i = size_{i-1} sizei=sizei1,有:
C i ^ = C i + ( 2 ⋅ n u m i − s i z e i ) − ( 2 ⋅ ( n u m i + 1 ) − s i z e i ) = 1 − 2 = O ( 1 ) \hat{C_i} = C_i + (2\cdot num_i - size_i) - (2\cdot (num_i + 1) - size_i) = 1 - 2 = O(1) Ci^=Ci+(2numisizei)(2(numi+1)sizei)=12=O(1)

当装载因子 α < 1 / 2 \alpha < 1/2 α<1/2时,若删除操作不引起缩表,则 s i z e i = s i z e i − 1 size_i = size_{i-1} sizei=sizei1,有:
C i ^ = C i + ( − 2 ⋅ n u m i + s i z e i ) − ( − 2 ⋅ ( n u m i + 1 ) + s i z e i ) = 3 \hat{C_i} = C_i + ( - 2\cdot num_i + size_i) - ( - 2\cdot (num_i + 1) + size_i) = 3 Ci^=Ci+(2numi+sizei)(2(numi+1)+sizei)=3

当装载因子 α < 1 / 2 \alpha < 1/2 α<1/2时,若删除操作引起缩表,则 s i z e i = 2 3 s i z e i − 1 size_i = \frac{2}{3}size_{i-1} sizei=32sizei1,有:
C i ^ = C i + ( − 2 ⋅ n u m i + s i z e i ) − ( − 2 ⋅ ( n u m i + 1 ) + 3 2 s i z e i ) = ( 1 + n u m i ) + s i z e i − 2 ⋅ n u m i + 2 ⋅ n u m i + 2 − 3 2 s i z e i = 3 + n u m i − 1 2 s i z e i \hat{C_i} = C_i + ( - 2\cdot num_i + size_i) - ( - 2\cdot (num_i + 1) + \frac{3}{2}size_{i}) = (1 + num_i) + size_i - 2\cdot num_i + 2\cdot num_i + 2 - \frac{3}{2}size_{i} = 3 + num_i - \frac{1}{2}size_i Ci^=Ci+(2numi+sizei)(2(numi+1)+23sizei)=(1+numi)+sizei2numi+2numi+223sizei=3+numi21sizei

已知缩表的条件为装载因子小于 1 / 3 1/3 1/3时将表规模变为原来的 2 / 3 2/3 2/3。故 n u m i < 1 / 3 ⋅ s i z e i − 1 = 1 / 2 ⋅ s i z e i num_i < 1/3\cdot size_{i-1} = 1/2\cdot size_i numi<1/3sizei1=1/2sizei。因此由上式可得:
C i ^ = 3 + n u m i − 1 2 s i z e i < 3 \hat{C_i} = 3 + num_i - \frac{1}{2}size_i < 3 Ci^=3+numi21sizei<3

综上有 ∀ i \forall i i C i ^ = O ( 1 ) \hat{C_i} = O(1) Ci^=O(1),故操作 T A B L E − D E L E T E TABLE-DELETE TABLEDELETE的摊还代价上界为常数。

相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页