





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






There are two stacks, AAA and BBB. Let:
a=∣A∣,b=∣B∣a = |A|, \qquad b = |B|a=∣A∣,b=∣B∣
The allowed operations are:
Operation Effect Real cost
push-A(d) (^) Push ddd onto AAA 1
push-B(d) (^) Push ddd onto BBB 1
multi-pop- A(k)
Remove min(k,a) \min(k, a)min(k,a) elements from AAA
min(k,a) \min(k, a)min(k,a)
multi-pop- B(k)
Remove min(k,b) \min(k, b)min(k,b) elements from BBB
min(k,b) \min(k, b)min(k,b)
transfer(k) (^) Move up to kkk elements from AAA to BBB Number moved
transfer(k) moves elements only from AAA to BBB, never from BBB to AAA.graphics.stanford
20 points
Assign an amortized cost to each operation using the accounting method. Your amortized costs must ensure that the total credit never becomes negative.
Then use your assignment to prove that any sequence of nnn operations has total actual cost O(n)O(n)O(n).
Use this accounting assignment:
Operation Real cost Amortized cost Credit change
push-A(d) (^1 2) AAA gains 1 credit
push-B(d) (^1 1) No credit change
multi-pop- A(k)
min(k,a) \min(k, a)min(k,a) 1 Uses stored credits
multi-pop- B(k)
min(k,b) \min(k, b)min(k,b)
min(k,b)+1 \min(k, b) + 1min(k,b)+ BBB loses credits
transfer(k) (^) Number moved 1 Uses AAA’s credits to pay for moves
The key idea is that every element pushed onto AAA receives one extra unit of credit. That credit pays for the future work of removing it from AAA, whether that removal happens through multi-pop-A or transfer. This is exactly the accounting idea used in Stanford’s stack example: overcharge a push by one unit to prepay for its eventual pop.graphics.stanford+
Each element in AAA has exactly one credit. multi-pop-A(k) removes at most aaa elements, so it uses at most aaa credits. transfer(k) moves at most aaa elements, so it also uses at most aaa credits. Therefore, the credit associated with stack AAA never becomes negative.
Each of the nnn operations has amortized cost at most:
O(1)+min(k,b)O(1) + \min(k, b)O(1)+min(k,b)
for a multi-pop-B operation. However, a multi-pop-B(k) can remove each element from BBB at most once unless it is later returned by another push. Since each push-B pays for one removal, the total amortized cost remains O(n)O(n)O(n).
Thus:
Operation Real cost cic_ici Change in potential ΔΦi\Delta \Phi_iΔΦi
Amortized cost B(k) (^) \min(k, b)m=min(k,b) 1 m+ 1
transfer(k) mmm, where^ m=min(k,a)^ m = \min(k, a)m=min(k,a) −m-m−m 111
The only operation whose amortized cost is not constant is multi-pop-B(k). But each element removed from BBB must have previously been pushed onto BBB, either directly by push-B or indirectly by transfer. The direct or indirect insertion pays for its later removal.
Therefore, over any sequence of nnn operations, the total amortized cost is O(n)O(n)O(n). Since the potential is always nonnegative and starts at 0 for empty stacks, the total real cost is also O(n)O(n)O(n).graphics.stanford+
15 points
Using the aggregate method, prove that any sequence of nnn operations on the two-stack structure has total actual cost O(n)O(n)O(n).
The aggregate method bounds the total cost of a sequence and divides by the number of operations. Stanford defines it as finding an upper bound T(n)T(n)T(n) on the total cost of nnn operations, then assigning amortized cost T(n)/nT(n)/nT(n)/n to each operation.graphics.stanford
Track each element’s lifetime:
Thus, each element can be involved in only a constant number of paid operations before it leaves the data structure permanently.
If nnn operations are performed, at most nnn elements can be inserted. Each inserted element can generate only O(1)O(1)O(1) total work across transfers and pops. Therefore, the total actual cost is:
O(n)O(n)O(n)
and the amortized cost per operation is:
O(n)n=O(1)\frac{O(n)}{n} = O(1)nO(n)=O(1)
This aggregate argument is the same principle used for the two-stack queue: each element is pushed and popped only a constant number of times.web.stanford+
20 points
For each sequence below, compute the total real cost and the total amortized cost using the potential function:
Φ=∣A∣\Phi = |A|Φ=∣A∣
Assume both stacks begin empty and that each unit of potential pays for one unit of real work.
text push-A(1) push-A(2) push-A(3) transfer(3)
Compute:
text push-A(1) push-A(2) multi-pop-A(1) transfer(10)
Compute:
Final potential:
Φ=∣A∣=0\Phi = |A| = 0Φ=∣A∣=
Although transfer(10) requests ten elements, only one element remains in AAA, so its real cost is 1.graphics.stanford
25 points
A two-stack queue uses an In stack and an Out stack:
enqueue(x) pushes xxx onto In. dequeue() pops from Out if possible. If Out is empty, repeatedly pop from In and push onto Out, then pop from Out.
Prove that any sequence of nnn queue operations has total actual cost O(n)O(n)O(n).
Use the potential function:
Φ=∣In∣\Phi = |\text{In}|Φ=∣In∣
An enqueue performs one push onto In.
Real cost: 111 Potential change: +1+1+ Amortized cost:
1+1=21 + 1 = 21+1=
So enqueue has O(1)O(1)O(1) amortized cost.
If Out is nonempty, dequeue simply pops from Out.
Real cost: 111 Potential change: 000 Amortized cost:
1+0=11 + 0 = 11+0=
So a light dequeue has O(1)O(1)O(1) amortized cost.
If Out is empty and In contains kkk elements, dequeue moves all kkk elements from In to Out, then pops one element from Out.
Real cost: k+1k + 1k+ Potential change: −k-k−k Amortized cost:
(k+1)+(−k)=1(k + 1) + (-k) = 1(k+1)+(−k)=
So even a heavy dequeue has O(1)O(1)O(1) amortized cost.
Every enqueue has amortized cost O(1)O(1)O(1), and every dequeue has amortized cost O(1)O(1)O(1). Therefore, nnn operations have total amortized cost O(n)O(n)O(n).
Because the potential is nonnegative and begins at 0, the total real cost is also O(n)O(n)O(n). This is precisely the two-stack queue analysis presented in Stanford’s amortized-analysis materials.