need to find the amortized cost of a sequence using the potential function method

♀尐吖头ヾ 提交于 2019-12-04 10:17:40

First, forget about the "1 for all other operations" and "exact power of 3" bits for a minute. What if the cost was just 2i when i is an exact power of 2?

Well, suppose we pretend each operation costs four. That is, for each operation, we put four coins into our "IOU pile"... Then we use those coins to "pay" when we hit actual powers of 2. (This is one way of describing the potential function method.)

Step 1: Deposit four coins. Need to pay 2*1 = 2, so our coin pile is at two.

Step 2: Deposit four more coins. Need to pay 2*2 = 4, so our pile is again at two.

Step 3: Deposit four coins. Pile now has six coins.

Step 4: Deposit four coins. Pile now has ten coins, but 4 is a power of 2, so it's time to pay 4*2 = 8, so our pile goes down to two coins again.

Steps 5-7: Deposit four coins each. Total is now 14 coins.

Step 8: Deposit four coins (total = 18), spend 8*2 = 16, once again two coins are left.

It is pretty easy to prove that the steady state here is that we keep depleting our coins down to a constant (2), but we never go below. Therefore the amortized cost is four units per operation.

Now, suppose operation X costs 2i when i is a power of 2 (and zero otherwise). Suppose operation Y costs 3i when i is a power of 3 (and zero otherwise). And suppose operation Z costs 1 except when i is a power of 2 or a power of 3. Observe that your problem is equivalent to performing operation X and Y and Z for every iteration... So if you can figure out the amortized cost of X, Y, and Z separately, you can just sum those to get the overall amortized cost.

I just gave you X; I leave Y and Z as an exercise. (Although I do not believe the final answer is 10. Close to 10, perhaps...)

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