amortized cost of modified increment function

亡梦爱人 提交于 2020-01-05 07:13:25

问题


So the scenario goes, the increment algorithm, for n bit binary string A[0....n-1], where A[0] is the least significant bit and A[n-1] is the most significant bit, is:

Increment(A,n):
i←0
while i<k and A[i]=1 do
   A[i] ← 0
   i ← i+1
if i < k then
   A[i] ← 1

But the cost to flip a bit at index k is 2^k

I'm lost on trying to prove that the amortized cost of this modified binary increment algorithm is O(logn). No matter how I try to approach, it still seems like the amortized cost would be big O(1), though with a bigger constant.

aggregated analysis of the increment function. if I follow up on this breakdown and multiply by 2^i inside the sigma, since for the cost of flipping the ith bit is 2^i, I get nk for n increments. Which still gives me amortized cost of O(1)

I am not sure what I'm doing wrong here. Intuitively it makes sense for it to be still O(1) since the high cost higher bits just cancels out the low probability of it being flipped.


回答1:


If we increment the counter from 0 up to 2^m, how many times does each bit flip?

Bit 0 flips 2m times. Bit 1 flips 2m-1 times. But 2 flips 2m-2 times, etc...

If we calculate the total costs:

Bit 0 costs 1 * 2m. Bit 1 costs 2*2m = 2m. Bit 2 costs 4*2m-2 = 2m, etc...

Every bit that changes has the same total cost, and there are m+1 bits that change, so the total cost (m+1)2m

If number of increments n = 2m then the amortized cost per increment is

(m+1)2m/n

= ((log2n)+1)*n/n

= 1+log2n



来源:https://stackoverflow.com/questions/59220703/amortized-cost-of-modified-increment-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!