Skip to content

Amortized Analysis IV #27

Closed
Closed
@hamidgasmi

Description

@hamidgasmi

Imagine a stack with a new operation: PopMany which takes a parameter, i, that specifies how many elements to pop from the stack. The cost of this operation is i, the number of elements that need to be popped.

Without this new operation, the amortized cost of any operation in a sequence of stack operations (Push, Pop, Top) is O(1) since the true cost of each operation is O(1).

What is the amortized cost of any operation in a sequence of n stack operations (starting with an empty stack) that includes PopMany (choose the best answers)?

(1) O(1) because the sum of the costs of all PopMany operations in a total of n operations is O(n)
(2) O(n) because we could push n−1 items and then do one big PopMany(n−1) that would take O(n) time
(3) O(1) because we can define Φ(h)=size
(4) O(1) because we can place one token on each item in the stack when it is pushed. That token will pay for popping it off with a PopMany
(5) O(n) because we could push n−1 items and then do one big PopMany(n−1) that would take O(n) time

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions