I was reading the algorithms textbook Cormen, Liserson, Rivest and Stein more often than not.
One of the interesting chapter in there is Amortized analysis. Binary counter is a difficult example when it comes to selecting Potential functions. I was wondering what if the counter if a power of 3 with some co-efficients (ex. x1*1 + x2*3 + x3*9 +...).
How does one decide Potential function in this type of case ?
To prove that incrementing a base-3 counter takes constant amortized time, you can use the potential method with the number of 2s in the current value as the potential function.
An increment operation can be divided into 2 parts:
The contiguous 2s at the end of the value are changed to 0.
The first digit to the left of those 2s is incremented.
Step (1) takes work proportional to the number of 2s removed from the state.
Each of those 2s is added by Step(2) from a previous operation, which takes constant time and adds at most one 2.
Let Φ be the number of 2s in the current state.