Skip to content

Amortized Analysis III #26

Closed
Closed
@hamidgasmi

Description

@hamidgasmi

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)

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions