-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Amortized Analysis III #26
Labels
Comments
Answer: Φ3 Explanation:
|
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
Let's imagine we add support to our dynamic array for a new operation PopBack (which removes the last element). Calling PopBack on an empty dynamic array is an error.
PopBack reallocates the dynamically-allocated array to a new array of half the capacity if the size is ≤ the capacity / 4 . So, for example, if, before a PopBack the size were 5 and the capacity were 8, then after the PopBack, the size would be 4 and the capacity would be 8. Only after two more PopBack when the size went down to 2 would the capacity go down to 4.
We want to consider the worst-case sequence of any n PushBack and PopBack operations, starting with an empty dynamic array.
What potential function would work best to show an amortized O(1) cost per operation?
Φ1(h)=2×size−capacity
Φ2(h)=2
Φ3(h)=max(2×size−capacity,capacity/2−size)
Φ4(h)=max(0,2×size−capacity)
The text was updated successfully, but these errors were encountered: