rec04 - 15-451 Rec. 2 1 15-451: Algorithms Recitation 4:...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-451 Rec. 2 1 15-451: Algorithms Recitation 4: Amortized Analysis 1 Bits and Trits Suppose we have a binary counter such that the cost to increment the counter is equal to the number of bits that need to be ipped. We saw in class that if the counter begins at 0, and we increment the counter times, the amortized cost per increment is just (1). Equivalently, the total cost to perform all increments is ( ). Suppose that we want to be able to both increment and decrement the counter. n O n On 1. Show that even without making the counter go negative, it is possible for a sequence of operations starting from 0, allowing both increments and decrements, to cost as much as (log ) amortized per operation (i.e., ( log ) total cost). Solution: Let 2b+1 2b+2 . Use 2b operations to transform the number to a 1 followed by 1 0's. This takes at least 2b work. Then alternate decrementing and incrementing the number for the remaining 2b steps. Each of these operations takes ( 1) work. So, the total work over all these operations is at least (2b ). Thus the amortized cost per operation is at least 2b 4 = (log ). Note: if you assumed was a power of 2, that's ne. 2. To x the problem from part (a), consider the following redundant ternary number system. A number is represented by a sequence of trits, each of which is 0, +1, or 1. The value of the number k 1 0 (where each i is a trit) is de ned to be n n n n n b b b b =n b= n n t ;::: ;t t X1 2 k i =0 ti i : The process of incrementing a ternary number is analogous to that operation on binary numbers. One is added to the low order trit. If the result is 2, then it is changed to 0, and a carry is propagated to the next trit. This process is repeated until no carry results. Decrementing a number is similar. One is subtracted from the low order trit. If it becomes -2 then it is replaced by 0, and a borrow is propagated. The cost of an increment or a decrement is the number of trits that change in the process. Starting from 0, a sequence of increments and decrements are done. Give a clear, coherent proof that with this representation, the amortized cost per operation is (1) (i.e., the total cost for the operations is ( )). Hint: think about a \bank account" or \potential function" argument. Solution: Note that when the number is incremented or decremented, 1's change to 0 or stay the same], 1's change to 0 or stay the same], and 0's change to either 1 or 1 or stay the same]. This means that if $2 is paid each time a digit is converted from a zero, there will be n O n On 15-451 Rec. 2 2 enough money in the bank to pay for converting it back to a zero. (Think of having a separate bank account for each digit.) This means that to bound the amortized cost, we just need to show that each increment or decrement doesn't change many 0's to +1 or 1. Lemma: Each increment or decrement converts at most 1 trit away from 0. Proof: That's because we only do a carry when at 1 becomes a 2 or a 1 becomes a 2. So, changing a 0 to 1 or 1 ends the process. Thus the amortized cost is O(1). ...
View Full Document

This note was uploaded on 11/26/2009 for the course CIS 502 taught by Professor Naver during the Spring '09 term at National Tsing Hua University, China.

Ask a homework question - tutors are online