Q) Consider an ordinary binary max-heap data structure with n elements that supports insert and extract-max in $O(log(n))$ worst-case time. Give a potential function $\Phi$ such that the amortized cost of insert is $O(log(n))$ and the amortized cost of extract-max is $O(log(n))$, and show that it works.
solution
The formula of amortized cost is
$a_n = c_n + \Phi(D_n) - \Phi(D_{n-1})$
Where the potential function: $\Phi (D_n) = n log(n)$.
for insert:
$= c_n + \Phi(D_n) - \Phi(D_{n-1}) \leq log(n) + n log(n) - (n-1)log(n-1) \leq 3 log(n+1) = O(log(n))$
So, the amortized cost of insert is $O(log(n))$
extract max:
$a_n = c_n + \Phi(D_n) - \Phi(D_{n-1}) \leq log(n) + n log(n) - (n+1)log(n+1) = log(n) + n(log(n)) - n log(n+1) - log(n+1) < 0$
So, the amortized cost of extract-max is $O(1)$
Assuming this is correct. Could someone explain how they got the potential function?