Skip to content

Latest commit

 

History

History
22 lines (15 loc) · 1.25 KB

17.1.md

File metadata and controls

22 lines (15 loc) · 1.25 KB

Exercise 17.1-1


If the set of stack operations included a MULTIPUSH operation, which pushes k items onto the stack, would the O(1) bound on the amortized cost of stack operations continue to hold?

Answer

No.The time complexity of such a series of operations depends on the number of push operation. Since one MULTIPUSH needs Θ(k)time, performing n MULTIPUSH operations, each with k elements, would take Θ(kn)time, leading to amortized cost of Θ(k).

Exercise 17.1-2


Show that if a DECREMENT operation were included in the k-bit counter example, n operations could cost as much as theta(nk) time.

Answer

In the worst case, going from 1[k-1 0's] (ie. 1000 -> 0111) takes k flips. Could do any sequence of increment and decrement from 1000 -> 0111 -> 1000 -> 0111 (n times), which is theta(nk).

Exercise 17.1-3


A sequence of n operations is performed on a data structure. The ith operation costs i if i is an exact power of 2, and 1 otherwise. Use aggregate analysis to determine the amortized cost per operation.

Answer

Look at a sequence of n operations. Let k = floor(lg(n)). All operations cost equal to 1 + 2 * (1 - 2^k) / (1 - 2) + n - k - 1 = 2 * 2^k + n - k - 2 = O(n), than each operation cost equal to O(n) / n = O(1).