CPS 130 Homework 17 Amortized Analysis

due Thu June 13th

Write and justify your answers in the space provided.^{1}

1. In this problem we consider two stacks A and B manipulated using the following operations

(n denotes the size of A and m the size of B):

• P ushA(x): Push element x on stack A.

• P ushB(x): Push element x on stack B.

• MultiP opA(k): Pop min{k, n} elements from A.

• MultiP opB(k): Pop min{k, m} elements from B.

• T ransfer(k): Repeatedly pop an element from A and push it on B, until either k

elements have been moved or A is empty. Assume that A and B are implemented using doubly-linked lists such that PushA and PushB, as well as a single pop from A or B, can be performed in O(1) time worst-case.

(a) What is the worst-case running time of the operations MultiPopA, MultiPopB and Transfer?

1Collaboration is allowed, even encouraged, provided that the names of the collaborators are listed along with the solutions. Students must write up the solutions on their own.

(b) Define a potential function Φ(n, m) and use it to prove that the operations have amortized running time O(1).